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!
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.
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
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
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
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