Tool Parts Not Being Welded Properly

I have a script that is welding all the parts inside a tool together. The script doesn’t work most of the time, but sometimes it does for some reason. (It has like a 5% chance of working)
Here is the function:

function WeldTool(tool)
	local children = tool:GetChildren()
	
	local parts = {}
	
	for _, child in pairs(children) do
		if child:IsA('BasePart') then
			table.insert(parts, child)
		end
	end
	
	local primaryPart = tool.PrimaryPart or parts[math.random(1, #parts)]
	
	for _, part in pairs(parts) do
		if part.Name ~= primaryPart.Name then
			local weld = Instance.new('WeldConstraint')
			weld.Part0 = part
			weld.Part1 = primaryPart

			weld.Name = primaryPart.Name.. 'To'.. part.Name..'Weld'
			weld.Parent = tool
		end
	end
end

When I call this function, the parts inside the tool don’t get welded to each other.
If you can see anything wrong with it, please tell me! Thanks!

What happens when it welds improperly?

The parts aren’t attached to eachother and they fall off.

I think the issues comes from the weldconstraint being parented to the tool, try parenting it to the primarypart instead.

that didn’t work, the parent of the weldconstraint shouldn’t matter

Try swapping the part order maybe? the code seems like it should work, verify if weld are actually created or not.

they are created, and I checked the part0 and part1 and it was set correctly. The parts just fall apart though… Do you have any ideas on whats wrong?

The Script seems to work as intended, but if you have Parts that are Parented to other Parts, you should check those Parts as well, which seems to be the issue.

I don’t have any parts parented to other parts, they are all parented to the tool.

So This Part seems to be the Issue, If you are choosing a Random Part that has the Same Name as Another Part, the Script would Exclude that Part because the if statement is saying to only accept Parts that dont have the Same Name, So I told the Script to Remove the Random Part from the table so It doesnt get confused with others, which It seemed to fix the issue, but I’m not sure if we have the same issue.

	local primaryPart = tool.PrimaryPart or parts[math.random(1, #parts)]
	
	if parts[primaryPart] then -- if PrimaryPart Exists in the table (which It should)
		table.remove(parts, parts[primaryPart]) -- removes the PrimaryPart
	end

	for _, part in pairs(parts) do -- iterating through Parts
        -- basically the same welding code without if statement
		local weld = Instance.new('WeldConstraint')
		weld.Part0 = part
		weld.Part1 = primaryPart

		weld.Name = primaryPart.Name.. 'To'.. part.Name..'Weld'
		weld.Parent = tool
	end

The script you sent wont work.

You cannot index an array like so:
parts[primaryPart]
since its an array and has number indexes.

table.remove takes in a number as a second argument, so use:
table.remove(parts, table.find(parts, primaryPart))

1 Like

good point, and just like @Synarx said:

But that isn’t the issue, because the WeldConstraint gets created, the part0 gets set correctly, and so does the part1, but the 2 parts in the tool still fall apart.

is there a reason you don’t weld them before you start the game? like just welding them in explorer and then not relying on a script to do it for you every time you play

Good idea! I just tried welding them before the game and it doesn’t work either for some reason…? Same issue, the parts arent attached to eachother even though there is a weld

check so both part0 and part1 is set, also make sure those parts are the correct parts and not some cloned parts parented to nil. You can check by hivering over part0/1 and making sure the path is something like workspace.Tool.Part and not just like Tool.Part ← this would mean that its parent to nil

They are both set to the correct parts and it still doesnt weld properly

are the welds enabled? Also make sure the welds are properly set when in game and that there are no joint breaking instance breaking the tool, like explosions

They are enabled. I will get back to you in a minute im gonna try something

The way I am making this tool is cloning a model from replicated storage, and converting it from a model to a tool by cloning all it’s descendants and placing them in a new tool. Would this have anything to do with the weld breaking? (I am trying with a premade weld inside the model)

and it does have a handle that you weld the model to?
from my understanding the problem is the model falls apart like you drop a lego set and it just shatters, right? in that case, premade welds like weld constraints would work best