I want a rig to play an animation, which involves holding a weapon. I made the animation in Moon Animator 2 and used the built in Easy Weld to weld the tool to the rig I animated on. The tool itself was “cleaned”, so it’s technically just a model.
I need the animation to play on a rig different to the one I made the animation on, which means the tool is not welded beforehand
The animation can play just fine, but the rig is not holding the tool. I need to somehow weld the tool to the rigs hand at runtime, and I don’t really know how.
Looking at the motor6d of the rig I made the animation on, it has some pretty specific properties. I could definitely store these somewhere and when the weld is being created just copy those values, but I’d possibly have to manually store motor6d properties for every weapon in the game which I do not want to do.
There’s tutorials out there on how to animate with tools/props, but the ones that end up actually playing the animation through code always just play it on the rig already welded with the tool.
This module lets you attach any tool to any rig at runtime using a stored relative CFrame.
I used it to test with. Should still work.
ModuleScript
--ModuleScript in ReplicatedStorage
local ToolAttacher = {}
function ToolAttacher.Attach(tool, rig, handName, relativeCFrame)
local hand = rig:FindFirstChild(handName)
if not hand then return end
tool.PrimaryPart = tool.PrimaryPart or tool:FindFirstChildWhichIsA("BasePart")
if not tool.PrimaryPart then return end
local motor = hand:FindFirstChild("ToolMotor")
if motor then motor:Destroy() end
local newMotor = Instance.new("Motor6D")
newMotor.Name = "ToolMotor"
newMotor.Part0 = hand
newMotor.Part1 = tool.PrimaryPart
newMotor.C0 = relativeCFrame
newMotor.C1 = CFrame.new()
newMotor.Parent = hand
end
return ToolAttacher
Usage
--ServerScript in ServerScriptService
local ToolAttacher = require(game.ReplicatedStorage.ToolAttacher)
local rig = workspace:WaitForChild("Rig")
local tool = workspace:WaitForChild("Weapon")
local handCFrame = CFrame.new(0,0,0) * CFrame.Angles(0,0,0) -- replace with your saved relative CFrame
ToolAttacher.Attach(tool, rig, "RightHand", handCFrame)