I'm trying to weld a gun to my player's hands. It has multiple parts, but I don't know how I can do it all at once using a function. Can anyone help me with this? What I mean is like instead of having to create a weld for every part , for example w = Instance.new("Weld"), and doing that multiple (tens) of times, is there a way I can weld the weapon using a single function, and is it possible to animate it using animation editor if I do that?
You can easily do this by iterating through all the parts under the weapon, and welding each of them.
local tool = script.Parent -- make sure the parts are anchored before you run this function on them function weldChildren(parent, p1) -- parent is the parent of the children that you want to weld -- p1 is the part each of the parts is going to be welded tonumber for _, v in pairs(parent:GetChildren()) do if v:IsA("BasePart") then -- make sure its a basepart so it doesn't error local weld = Instance.new("Weld") v.TopSurface = 0 v.BottomSurface = 0 -- make sure they are smooth surfaces to avoid bugs weld.Parent = v weld.Part0 = v weld.Part1 = p1 --CFrame of part1 relative to part0 weld.C0 = v.CFrame:inverse() * p1.CFrame v.Anchored = false -- make sure that the parts are not anchored end end end weldChildren(tool, tool.Handle) -- calling function assuming tool is the tool, and tool.Handle is the part you want all parts to be welded to
And yes, you can animate it using animation editor
Hope it helped, if you have any further questions don't hesitate to ask.