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