Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't the rest of my tool get held?

I have a gun. I'm currently just trying to get the character to hold it when ti is equipped. Here is the script I have:

script.RemoteEvent.OnServerEvent:connect(function(plr)	
	local handle = script.Parent.Handle
	print("Equipped")
	local rarm = script.Parent.Parent["Right Arm"]
	local weld = Instance.new("Weld")
	weld.Part0 = rarm
	weld.C0 = rarm.CFrame:inverse()
	weld.Part1 = handle
	weld.C1 = handle.CFrame:inverse()
	local modelparts = script.Parent.Handle:GetChildren()
	for i, v in pairs(modelparts) do
		if v.ClassName == "Tool" then
			local gunweld = Instance.new("Weld")
			weld.Part0 = handle
			weld.C0 = handle.CFrame:inverse()
			weld.Part1 = v
			weld.C1 = v.CFrame:inverse()
			print("welded something")
		end
	end
end)

The handle is welded just fine. It's currently visible, so I can see the small part in my hand. But the rest of the gun just falls through the floor and doesn't even go remotely near the handle.

1 answer

On line 10, it looks like you're getting the children of a part, expecting parts inside a part. I'm not sure how that works because I've never tried it, but that might be an issue. Are you sure the parts are under the Handle part and not all in one place under the model along with the Handle part? Make sure all the parts are under the same model, and none inside eachother and try this

script.RemoteEvent.OnServerEvent:connect(function(plr)  
	local handle = script.Parent.Handle
	print("Equipped")
	local rarm = script.Parent.Parent["Right Arm"]
	local weld = Instance.new("Weld")
	weld.C0 = rarm.CFrame:toObjectSpace(handle.CFrame) --This might weld the handle away from the arm depending on how your script works, if the handle isn't attached to the arm then try commenting this line and try again
	weld.Part0 = rarm
	weld.Part1 = handle
	weld.Parent = rarm --You forgot to parent the welds
	local modelparts = script.Parent:GetChildren()
	for i, v in pairs(modelparts) do
		if v ~= handle and v:IsA("Part") then --This was checking if the parts were "Tool"s, which they are not
			local weld= Instance.new("Weld") --This variable was "gunweld" instead of "weld" when you were trying to use the "weld" variable in the following lines
			weld.C0 = handle.CFrame:toObjectSpace(v.CFrame) --toObjectSpace is a better alternative to CFrame:inverse(), as it only requires one C offset property to be occupied instead of both.
			weld.Part0 = handle
			weld.Part1 = v
			weld.Parent = handle
			print("welded something")
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.