77 lines
2.7 KiB
Luau
77 lines
2.7 KiB
Luau
--!strict
|
|
-- Script in ServerScriptService.
|
|
-- Third-person rifle mount: while a character holds the SniperRifle, the tool's RightGrip weld is
|
|
-- switched off and the rifle is welded to the UpperTorso instead, at the TPGun* offset from the
|
|
-- attributes of StarterPlayerScripts.SniperViewmodel (the same values the ViewmodelTuner edits).
|
|
-- Done on the server so every client (and the physics owner) agrees on how the rifle is attached;
|
|
-- the arms are bent onto the rifle on each client by the ThirdPersonRifle LocalScript.
|
|
|
|
local Players = game:GetService("Players")
|
|
local RunService = game:GetService("RunService")
|
|
local StarterPlayer = game:GetService("StarterPlayer")
|
|
|
|
local TOOL_NAME = "SniperRifle"
|
|
local WELD_NAME = "TPRifleWeld"
|
|
local config = StarterPlayer:WaitForChild("StarterPlayerScripts"):WaitForChild("SniperViewmodel")
|
|
|
|
local function attr(name: string): number
|
|
return (config:GetAttribute(name) :: number?) or 0
|
|
end
|
|
local function gunPose(): CFrame
|
|
return CFrame.new(attr("TPGunX"), attr("TPGunY"), attr("TPGunZ"))
|
|
* CFrame.Angles(0, math.rad(attr("TPGunYawDeg")), 0)
|
|
* CFrame.Angles(math.rad(attr("TPGunPitchDeg")), 0, 0)
|
|
* CFrame.Angles(0, 0, math.rad(attr("TPGunRollDeg")))
|
|
end
|
|
|
|
local mounted: { [Model]: Weld } = {}
|
|
|
|
local function setGrip(character: Model, enabled: boolean)
|
|
local hand = character:FindFirstChild("RightHand") or character:FindFirstChild("Right Arm")
|
|
local grip = hand and hand:FindFirstChild("RightGrip")
|
|
if grip and grip:IsA("JointInstance") and grip.Enabled ~= enabled then
|
|
grip.Enabled = enabled
|
|
end
|
|
end
|
|
|
|
RunService.Heartbeat:Connect(function()
|
|
local seen: { [Model]: boolean } = {}
|
|
for _, p in Players:GetPlayers() do
|
|
local character = p.Character
|
|
local tool = character and character:FindFirstChild(TOOL_NAME)
|
|
local handle = tool and tool:FindFirstChild("Handle")
|
|
local torso = character and (character:FindFirstChild("UpperTorso") or character:FindFirstChild("Torso"))
|
|
if character and tool and tool:IsA("Tool") and handle and handle:IsA("BasePart") and torso and torso:IsA("BasePart") then
|
|
seen[character] = true
|
|
local weld = mounted[character]
|
|
if not weld or weld.Parent ~= torso or weld.Part1 ~= handle then
|
|
if weld then
|
|
weld:Destroy()
|
|
end
|
|
local w = Instance.new("Weld")
|
|
w.Name = WELD_NAME
|
|
w.Part0 = torso
|
|
w.Part1 = handle
|
|
w.C0 = gunPose()
|
|
w.Parent = torso
|
|
mounted[character] = w
|
|
weld = w
|
|
end
|
|
setGrip(character, false) -- the tool system re-creates it on every equip
|
|
local pose = gunPose()
|
|
if (weld :: Weld).C0 ~= pose then
|
|
(weld :: Weld).C0 = pose
|
|
end
|
|
end
|
|
end
|
|
for character, weld in mounted do
|
|
if not seen[character] then
|
|
mounted[character] = nil
|
|
weld:Destroy()
|
|
if character.Parent then
|
|
setGrip(character, true)
|
|
end
|
|
end
|
|
end
|
|
end)
|