I made a LocalScript in the StarterPack that deletes extra gears from the player’s inventory, so that they only have one. I also tried this in a normal script. Judging by the output, the gear IS put in the player’s inventory (by another script I have) but by the time I load in, it’s gone (supposedly by this script.)
Even though I’ve programmed it to only delete gears from the backpack when there are more than one. It seemingly deletes the gear regardless. I also checked server-side, the gear is not in the player’s backpack on the server either.
Here’s the code in the aforementioned local script:
while wait(0.5) do
for i, v in pairs(backpack:GetChildren()) do
local bombCount = 0
if v:IsA("Tool") then
bombCount += 1
if bombCount > 1 then
v:Destroy()
bombCount -= 1
end
end
end
end
local players = game.Players
local backpack = script.Parent
local plr = backpack.Parent
local chr = plr.Character
local bombCount = 0
while wait(0.5) do
for i, v in pairs(backpack:GetChildren()) do
if v:IsA("Tool") then
bombCount += 1
if bombCount > 1 then
v:Destroy()
bombCount -= 1
end
end
end
end
If you’re trying to keep the player having 1 tool at all times you can just use .ChildAdded
local players = game.Players
local backpack = script.Parent
local plr = backpack.Parent
local chr = plr.Character
local Count = 0
local function DeleteExtra()
Count = 0
for _, Tools in pairs(Backpack:GetChildren) do
if Tools:IsA("Tool") then
Count += 1
if Count >= 2 then
Tool:Destroy()
-- Count == 1 or -= 1
end
end
end
end
DeleteExtra()
plr.Backpack.ChildAdded:Connect(function(Child)
DeleteExtra()
end)
This way you don’t need to use while wait (Which u shouldnt use when u dont have to)
I suppose the problem is not with this code then, because mines and yours achieves the same result
This is the script that gives the tool (I know it’s username specific, don’t worry about that):
local bomb = game.ReplicatedStorage.Bomb:Clone()
local players = game.Players
players.PlayerAdded:Connect(function(plr)
print(plr.Name)
for i, v in pairs(players:GetPlayers()) do
if v.Name == "sharker12312" then
bomb.Parent = v.Backpack
end
end
end)
This is the modified extra gear removal:
local players = game.Players
local backpack = script.Parent
local plr = backpack.Parent
local chr = plr.Character
local function bombRemoval()
local bombCount = 0
for i, v in pairs(backpack:GetChildren()) do
if v.Name == "Bomb" then
bombCount += 1
if bombCount >= 2 then
v:Destroy()
bombCount -= 1
end
end
end
end
backpack.ChildAdded:Connect(function(child)
bombRemoval()
end)
The moment the game loads; the tool seems to get deleted.
Look, the “giver” code is going to give the bomb (from replicated storage) to my player’s backpack.
The deleter code makes sure there isn’t extra (this is important for my game)
I’m getting errors from scripts inside the bomb when the game starts (even though scripts don’t run in Replicated Storage) but when I look in my inventory (or the player’s backpack via the server pov) the bomb is not there. The errors are usually openable but it says “source of script not found” indicating that the gear has been deleted.
I’m not really sure what the problem is, now, either, unfortunately. I was sure it was the script that deletes the bombs, but all the modifications yield the same results.
I even deleted the code that is supposed to delete extra bombs, it still has the same results. Should I open a new thread?
I don’t know what you mean by edit the bomb, but I have two scripts and one local script inside the bomb gear itself that coordinates its functioning.
One script gives the bomb to my player (I have pasted its code above your post) and the other one (that I made this post off of) deletes bombs if there are two or more.
Bomb Scripts:
Script 1 makes the bomb explode after a few seconds
Script 2 passes over that specific bomb held by the player (me) to a player that I touch with the bomb
Local Script 1 forces the player to equip the bomb when the player unequips it (this one is definitely not the problem)
P.S: I know that the giving script runs because my username is printed in the output
P.S: The rest of the errors say that they’re attempting to sync something with “nil”- these all worked before, but it seems that they no longer know what the player or character is