Tool weld keeps breaking

Hello there,

My custom animated sword, is currently breaking after I respawn. I have tried checking the output, putting prints, and using the console inside the game and I still can’t seem to find the problem. If anyone can find an error in one of the scripts please let me know.

Welding Problem (Pictures)

image
This is the normal weld of the sword
image
This is what happens after I respawn

Weld code
wait()
local tool = script.Parent
local handle = tool:WaitForChild("Handle")

local function newc1(c1, p)
	local nc1 = Instance.new("CFrameValue")
	nc1.Value = c1
	nc1.Name = "c1"
	nc1.Parent = p
	return c1
end

local function c1Tool()
		table.foreach(tool:GetChildren(), function(_, part)
		if part:IsA("UnionOperation") or part:IsA("BasePart") then
			local c1 = part:findFirstChild("c1") and part.c1.Value or newc1(part.CFrame:toObjectSpace(handle.CFrame), part)
		end
	end)
end

local function weldTool()
	table.foreach(tool:GetChildren(), function(_, part)
		if part:IsA("UnionOperation") or part:IsA("BasePart") then
			local c1 = part:findFirstChild("c1") and part.c1.Value or newc1(part.CFrame:toObjectSpace(handle.CFrame), part)
			
			local nW = Instance.new("ManualWeld", handle)
			nW.Part0 = handle
			nW.Part1 = part
			nW.C0 = CFrame.new()
			nW.C1 = c1
			
			part.Anchored = false
		end
	end)
end

tool.AncestryChanged:connect(function(child, parent)
	if not handle:findFirstChild("ToolWeld") then
		weldTool()
	end
end)

c1Tool()
weldTool()
If you want to test for yourself

S&S Testing - Roblox

There is some other code inside my tool which I am willing to give if someone things they can find the problem.

This is what appears in my console after resetting the first time. Resetting a second time, does not print the errors and the tool functions completely fine. Humanoid not being a valid member of Model. In a “Core_Client” script.

I’m also gonna take the time to point out, you should not be using Deprecated pieces of code in your scripts. Such as :connect() and :findFirstChild. :Connect() and :FindFirstChild() should be used respectively. Same with :Preload(). :PreloadAsync() should be used instead.

Edit: What probably seems to be happening is if you’re using :FindFirstChild() to find the humanoid in a local script, isn’t the best thing to do. :WaitForChild() should generally always be used in Local Scripts.

1 Like

So should I replace the part in my script where it has ‘:FindFirstChild()’ to ‘WaitForChild()’?

If its a local Script, I’d highly recommend doing so.
It also states on the Developer Hub and the Wiki that you should use :WaitForChild in Local Scripts as not everything replicates in an instant like on the server.

2 Likes

Yes it is a Local script. While the rest of the scripts in my sword are normal. And it is for filtering enabled. I sent you the local core script via dms incase you want to check it out.

Also I will look into changing those parts of my code.

Thanks again for replying. I appreciate it.

1 Like

Again huge thanks for fixing the tool. I’ve been dealing with this glitch since mid May and now it is finally resolved.

1 Like