Script that deletes extra gears deletes too many gears for whatever reason

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

Thank you for reading.

2 Likes

Every time the script loops it sets the bombCount back to 0

Move the local bombCount outside of the 2nd loop

2 Likes

Exact same issue prevails, no changes whatsoever.

2 Likes

Can I see your updated code? abcd

2 Likes
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
2 Likes

You would return the function with the Destroy() call, which will stop the search :slight_smile:

function removeBomb()
	for i,v in pairs(backpack:GetChildren()) do
		if v:IsA("Tool") then
			return v:Destroy()
		end
	end
end

removeBomb()

And if you want to specifically remove tools, you have to add an if statement to check the name of the tool.

2 Likes

So what exactly are you trying to do?

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)

1 Like

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)
1 Like

So, wait what’s the problem? abcd

1 Like

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?

1 Like

How many scripts do you have that actually edit the bomb?

And do you know that if the bomb is actually given into the player backpack?

And have you checked other scripts to see what’s actually happening to it?

1 Like

Also I would recommend putting
local bomb = game.ReplicatedStorage.Bomb:Clone()

here
if v.Name == “sharker12312” then
bomb.Parent = v.Backpack
end

1 Like

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

1 Like

Can I see the explorer? abcdef

1 Like

I think I’m gonna give this up and just manually input the bomb into my inventory when testing.
Thanks for helping.

1 Like