Tool will not work when cloned?

A similar problem like this occured to me. And then after a while, it just simply disappeared. I continued working on my game until I tested the cloning almost a month later. And… it broke again.

To sum it up, when the tool is in the starter pack, it works perfectly fine and works as intended. When it is cloned to be put in the backpack, it does not work at all. Using print statements, I’ve narrowed the problem to be the result of none of the tool’s events firing.
Here’s two videos to reinforce my explanation:


Here is the script for the cloning:

local cd = script.Parent:WaitForChild("ClickDetector")
cd.MouseHoverEnter:Connect(function()
	script.Parent.BillboardGui.Enabled = true
end)
cd.MouseHoverLeave:Connect(function()
	script.Parent.BillboardGui.Enabled = false
end)
cd.MouseClick:Connect(function(plr)
	local medkit = game.ServerStorage["Pistol"]:Clone()
	medkit.Parent = plr.Backpack
	script.Parent:Destroy()
end)

If you want the script for the pistol, ask me for it but I don’t think it’s very important.

2 Likes

Cloning tools into Backpack through a LocalScript will cause them to not function anymore. Try cloning it on the server.
(I’m assuming you’re using one, the videos aren’t loading on my side)

2 Likes

You could try using Humanoid:EquipTool to see if that gets the events to fire as expected.

3 Likes

The tool is being cloned from ServerStorage so it’s likely that something else is going on.

2 Likes

When I use equip tool, the tool doesn’t even equip.

cd.MouseClick:Connect(function(plr)
	local medkit = game.ServerStorage["Pistol"]:Clone()
	medkit.Parent = plr.Backpack
	plr.Character.Humanoid:EquipTool(medkit)
	script.Parent:Destroy()
end)
1 Like

It looks like Humanoid:EquipTool only works if the tool is parented to a Backpack. You should check that one exists before trying to use it. I would also recommend doing the same thing for Humanoid.

EDIT: if this doesn’t work then please make sure you’re cloning from ServerStorage from a Script and not a LocalScript (server containers are only accessible from a Script).

I tested this code in an empty baseplate and it worked fine:

cd.MouseClick:Connect(function(player)
	if not player.Character then return end
	if not player:FindFirstChildOfClass("Backpack") then return end
	if not player.Character:FindFirstChildOfClass("Humanoid") then return end
	
	local humanoid = player.Character.Humanoid
	if humanoid.Health > 0 then
		local tool = ServerStorage.Tool:Clone()
		tool.Parent = player.Backpack
		humanoid:EquipTool(tool)
	end
end)
2 Likes

Once again, it cloned the tool into the backpack and worked in that aspect but the pistol itself did not work when cloned.
What is even weirder is if you don’t clone the pistol and just place it in the starterpack, it works completely as intended.
Could it be that local scripts inside serverstorage that are moved to the player’s backpack just don’t work?
But I added a print statement at the beginning of one and it printed.
Also, the cloning script is a serverscript.

2 Likes

I had a LocalScript inside of the Tool when I was testing it and all of the events fired as intended.

I might have to move everything to an entirely new game and see if that works.

1 Like

Could you share some of the code for the LocalScript(s) that aren’t working?

2 Likes
print("aA")
local remaining = game.ReplicatedStorage.Remaining
local reloadrt = game.ReplicatedStorage.Reload
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local plr = game:GetService("Players").LocalPlayer
local tool = script.Parent
local playergui = plr.PlayerGui
local firerate = 0.5
local debouncereload = false
local uis = game:GetService("UserInputService")
local debounce = false
local maxammo = 16
local value = plr.Bulletpack.Bullet
local char = plr.CharacterAdded:Wait()
local ammo_out = false
local rt = game.ReplicatedStorage.Shoot
local ammo = 16
script.Parent.Equipped:Connect(function()
	print("aaa")
	screengui1 = game.ReplicatedStorage.AmmoGui:Clone()
	screengui1.Parent = playergui
	screengui1.Enabled = true
	text = screengui1.Frame.Ammo
	text.Text = tostring(ammo) .. "/" .. maxammo
	if value.Value <= ammo then
		text.Text = tostring(value.Value) .. "/" .. maxammo

	end

end)
script.Parent.Unequipped:Connect(function()
	print("Aaa")
	screengui1:Destroy()
	print("op")
end)
script.Parent.Activated:Connect(function()
	print("bbb")
	rt:FireServer(mouse.Target, script.Parent, mouse.Hit.Position)
	if debounce == false then 
		ammo -=1
		debounce = true
		task.wait(firerate)
		debounce = false
	if value.Value == 0 then
		text.Text = "Out of Ammo"
		return
	end
	if ammo == 0 then
			text.Text = "Reloading"
		debounce = true
		task.wait(2)
			ammo = 16
			debounce = false
		if ammo >= value.Value then
			text.Text = value .Value.. "/" .. maxammo
		end
		if value.Value > ammo then
			text.Text = ammo .. "/" .. maxammo
		end
	end
	if value.Value >= ammo then
		text.Text = ammo .. "/" .. maxammo
	end
	if ammo > value.Value then
		text.Text = value.Value .. "/" .. maxammo
	end
end
end)

It’s a bit messy, I know

Like I said, this script works fine when the tool isn’t cloned.

1 Like

This looks like the cause to me, it would explain why it only happens sometimes. You shouldn’t always wait for the event to fire because they might already have a character, in that case, it won’t ever fire (the script will yield forever waiting for their character despite it already existing).

The fix would be to assign it to the character or wait for it if it doesn’t exist:

local character = plr.Character or plr.CharacterAdded:Wait()

However, you shouldn’t need this because the server code ensures they have a character. You can assume they will always have a character when the LocalScript first runs.

3 Likes

That was indeed the problem! Thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.