Clone Part on Tool Equipped

Hello, when a player equips the tool, I want to clone a part from ServerStorage onto their character, but my script doesn’t seem to be working. Does anyone know what’s going on?

local tool = script.Parent

tool.Equipped:Connect(function()
	local clone = game.ServerStorage.Trolly:Clone()
	clone.Parent = tool.Parent
	clone.WeldConstraint.Part1 = tool.Parent.HumanoidRootPart
end)

tool.Unequipped:Connect(function()
	tool.Parent.Trolly:Destroy()
end)
1 Like

Try to also set the position-------char limit

1 Like

The problem is that it doesn’t clone the trolly.

1 Like

Errors in the output?
Possible cause is that the script you’re using is a LocalScript, ServerStorage can’t be accessed locally.

1 Like

I’m using a server script, and it doesn’t print any errors in the output.

1 Like

Ah, explains. .Equipped only fires locally. You need to use a LocalScript and a RemoteEvent.

Example:
LocalScript:

local tool = script.Parent
local remote = script.Parent.RemoteEvent

tool.Equipped:Connect(function()
    remote:FireServer(true)
end)

tool.Unequipped:Connect(function()
    remote:FireServer(false)
end)

ServerScript:

local remote =  script.Parent.RemoteEvent

local clone

remote.OnServerEvent:Connect(function(_, toggle)
    if clone then
        clone:Destroy()
        clone = nil
    end

    if not toggle then return end

    clone = game:GetService("ServerStorage").Trolly:Clone()
    clone.Parent = script.Parent.Parent
    clone.WeldConstraint.Part1 = script.Parent.Parent.HumanoidRootPart
end)

Parent the LocalScript, ServerScript and RemoteEvent to the tool.

Typed it here so it took long and also I didn’t test.

This isn’t working, it says RemoteEvent is not part of the tool so I replaced local remote = script.Parent.RemoteEvent to local remote = script.Parent:WaitForChild("RemoteEvent") but now it doesn’t print anything.

Tested it out myself; worked flawlessly.

My environment (just had to modify the RemoteEvent declaration line):

Server Script:

local tool = script.Parent
local remote = tool:FindFirstChild("RemoteEvent") or tool:WaitForChild("RemoteEvent")
local clone

remote.OnServerEvent:Connect(function(_, toggle)
	if clone then
		clone:Destroy()
		clone = nil
	end

	if not toggle then return end

	clone = game:GetService("ServerStorage").Trolly:Clone()
	clone.Parent = tool.Parent
	clone.WeldConstraint.Part1 = tool.Parent.HumanoidRootPart
end)

Local Script:

local tool = script.Parent
local remote = tool:FindFirstChild("RemoteEvent") or tool:WaitForChild("RemoteEvent")

tool.Equipped:Connect(function()
	remote:FireServer(true)
end)

tool.Unequipped:Connect(function()
	remote:FireServer(false)
end)

Explorer:
image

LocalScripts cannot access server sided services, ServerStorage in your case, so just place that part you want to clone in your ReplicatedStorage and when it gets equipped, clone it from there.

Oh, now I realise why it wasn’t working on my side, I had RequiresHandle enabled and I don’t have a handle for mine. Now I just need to position it correctly when it gets cloned.

I want to do something like this: From an earlier topic I made.

1 Like

I ended up not using this technique and I’ve found a much better solution, but thank you guys for the help anyway. :smile: