Creating a tool from a model: welding issue

I’m attempting to create a tool from a model, being the handle its PrimaryPart.
My current code is:

local model = IM.getModel(IM.get(item.ID)) -- This just gets the model

local handle = model.PrimaryPart:Clone()
handle.Name = "Handle"
handle.Anchored = false
handle.CanCollide = false
handle.Parent = tool
-- Loops the model and welds any non-primary part to the tool handle
for i, v in pairs(model:GetChildren()) do 
	if model.PrimaryPart ~= v then
		local c = v:Clone()
		c.Anchored = false
		c.CanCollide = false
		c.Parent = tool
		
		local weld = Instance.new("Weld")
		weld.Part0 = handle
		weld.Part1 = c
		weld.Parent = handle
	end
end
return tool -- Code is inside a function

This code works fine when the Model only has one part, but when it has more than one, if the player equips the tool (I do it via humanoid:EquipTool(tool), from the client) it gets teleported to (I believe) somewhere near the original position of the handle.

The complete code process is:

Server creates welded tool -> Sends to player backpack -> Client equips from backpack to humanoid

I’m using humanoid:EquipTool(tool) on the client to equip the tool
I’ve been trying to search up a solution but I don’t really see my issue with the code, so I was thinking maybe you guys have any ideas?

Thanks a lot in advance!

If the model remains in the same place it usually suggests there’s at least one part that is still anchored.

in the for loop, instead of using ‘GetChildren’, try ‘GetDescendants’.

If the problem persists - take a look at the tool in the explorer window and see if you can find any anchored parts, or turn on the View Constraints / Welds mode and see if your tool is somehow being welded to anything other than the handle.

If the model remains in the same place it usually suggests there’s at least one part that is still anchored.

The model does not remain in the same place, but the player gets teleported (or flung) to the original model position, I believe

in the for loop, instead of using ‘GetChildren’, try ‘GetDescendants’.

There are no parts inside other parts in the model

If the problem persists - take a look at the tool in the explorer window and see if you can find any anchored parts, or turn on the View Constraints / Welds mode and see if your tool is somehow being welded to anything other than the handle.

image

(Yeah, I tried changing Weld to WeldConstraint to see if that can fix anything, but didn’t)
Both Handle and Leaves are un-anchored and non-collideable. The weldconstraint shows:

image

Edit: added chain of events to main post, added more info [I’m using humanoid:EquipTool(tool) on the client to equip the tool]

(Server creates welded tool -> Sends to player backpack -> Client equips from backpack to humanoid)

You can solve this with Welds as you’ve tried, here’s how I weld my tools in preparation for the Player to equip them from one of my weapon scripts:

local weldPart = function(Handle, Part)
	local Weld = Instance.new('Weld');
	Weld.Part0=Handle
	Weld.Part1=Part
	local CJ = CFrame.new(Handle.Position);
	local C0,C1=Handle.CFrame:inverse()*CJ,Part.CFrame:inverse()*CJ;
	Weld.C0=C0
	Weld.C1=C1
	Weld.Parent=Part
end;

weldObject = function(Object)
	if Object:IsA('Tool') then
	    local Handle = Object:FindFirstChild('Handle');
	    for _, Instance in pairs(Object:GetChildren()) do
		    if (Instance:IsA('Part') or Instance:IsA('UnionOperation') or Instance:IsA("MeshPart")) and Instance.Name~='Handle' then
		        WeldPart(Handle,Instance)
		        Instance.Anchored = false
		    end 
        end
	    Handle.Anchored = false
	end 
end

So in your case you can do this after all the Parts are inside the tool, (not the model):

local toolToWeld = DEFINE_TOOL_LOCATION
weldObject(toolToWeld)

Haven’t tried that code yet, but will try now, still, I’ve found one of the sources of the problem, which is…
The size of the parts.
If I have a model with 2 parts like this:

image

The player manages to grab the model successfully:

image

But if the two parts are bigger, such as:

image

Then the player just bugs out!

image

The code for the server is:

local SS = game:GetService("ServerStorage")
local model = SS.Model
local tool = Instance.new("Tool")
local moo = model:Clone()

local handle = moo.PrimaryPart:Clone()
handle.Name = "Handle"
handle.Anchored = false
handle.CanCollide = false
handle.Parent = tool

for i, v in pairs(moo:GetChildren()) do
	if v ~= moo.PrimaryPart then
		local p = v:Clone()
		p.Anchored = false
		p.CanCollide = false
		p.Parent = tool
		local w = Instance.new("WeldConstraint")
		w.Part0 = handle
		w.Part1 = p
		w.Parent = handle
	end
end

repeat wait() until #game.Players:GetChildren() > 0
for i, v in pairs(game.Players:GetChildren()) do
	repeat wait() until v:FindFirstChild("Backpack")
	tool.Name = "Tool" .. "a"
	tool.Parent = v.Backpack
end

And for the client,

local SG = game:GetService("StarterGui")
SG:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local player = game.Players.LocalPlayer
repeat wait() until player.Character
repeat wait() until player.Backpack:FindFirstChild("Toola")
repeat wait() until player.Character:FindFirstChild("Humanoid")
for i=1, 5 do
wait(1)
print(i)
end
player.Character.Humanoid:EquipTool(player.Backpack.Toola)

I have no clue why the size has anything to do with anything, but it does!

Solved the issue setting the non-handle parts to “Massless” to true.
No idea what this even is.
Solved ¿?

This happens because if the parts are bigger than the player, the parts become the Root Part. What you discovered is a newly added feature. You can also solve this by setting the RootPriority of the tool parts lower than the RootPriority of the Player Character.
You can learn more here

3 Likes

Thanks a lot! Now I understand what’s going on :slight_smile:
Didn’t know about that feature yet haha :smiley:

1 Like