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