Tool isn't destroying from all players

What the code should do is delete the tool from all players after x amount of time. My code:

local plrs = {}

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player)
	end
end
---
for i, player in pairs(plrs) do
	if player then
		character = player.Character
		if character then

			local BlowDryer = game.ServerStorage.Maps.BDMap1.BlowDryer:Clone()
			BlowDryer.Parent = player.Backpack
			
		end
	end
end

wait(29)
for i, player in pairs(game.Players:GetPlayers()) do
	character = player.Character

		if player.Backpack:FindFirstChild("BlowDryer") then
			player.Backpack.BlowDryer:Destroy()
		end

	if character:FindFirstChild("BlowDryer") then
		character.BlowDryer:Destroy()
		end

	end

The problem which I’m getting is that it’s deleting for some players but not all. Bit confused…

Are there any errors? Do you notice a pattern for when it doesn’t work, like if they equip the tool it doesn’t delete?

No there is no errors but I’m getting bug reports that it destroys from some players but not all.

Maybe try printing through all the Players first? I also went ahead & created a simple variable for sanity purposes, making it a bit easier to reference the Gear Check:

for i, player in pairs(game.Players:GetPlayers()) do 
    print(player)
	character = player.Character

    local BlowDryer = player.Backpack:FindFirstChild("BlowDryer") or character:FindFirstChild("BlowDryer")

    if BlowDryer then
        print(player.Name.."'s Blowdryer has been removed!")
        BlowDryer:Destroy()
    end

end

Other than that, I’m unsure where the issue could be lying

Maybe the reason is that you dont return the information of the table and you use too the loop, try this:

for i, player in pairs(game.Players:GetPlayers()) do
      if player then
       local char = player.Character
      if char then
        local BlowDryer = game.ServerStorage.Maps.BDMap1.BlowDryer:Clone()
          BlowDryer.Parent = player:WaitForChild("Backpack")
          wait(29)
           if player.Backpack:FindFirstChild("BlowDryer") or char:FindFirstChild("BlowDryer") then
                  BlowDryer:Destroy()
        end 
   end
end

That would work, but since you’re encasing the wait() inside the loop it’ll wait every 29 seconds to remove 1 of the Player’s Blowdryers which I don’t think is what the OP wants

If you’re confused on what I mean, here’s an example:

local Players = #game.Players:GetPlayers() --Say we have 10 players

for Loop = 1, Players do
    wait(29)
    Player.Name = "Potato Head" --Every 29 seconds, 1 of the Player's Names will be changed into "Potato Head" and will keep looping until a total of 29 * 10 seconds has been reached
end

oh yeah, I understand thanks :grinning:

is that after the 29 second wait?

Yeah, your full updated script should look something like this hopefully:

local plrs = {}

for i, player in pairs(game.Players:GetPlayers()) do
	if player then
		table.insert(plrs,player)
	end
end
---
for i, player in pairs(plrs) do
	if player then
		character = player.Character
		if character then
            print("Giving Blowdryer")
			local BlowDryer = game.ServerStorage.Maps.BDMap1.BlowDryer:Clone()
			BlowDryer.Parent = player.Backpack
			
		end
	end
end

wait(29)
for i, player in pairs(game.Players:GetPlayers()) do 
    print(player)
	character = player.Character

    local BlowDryer = player.Backpack:FindFirstChild("BlowDryer") or character:FindFirstChild("BlowDryer")

    if BlowDryer then
        print(player.Name.."'s Blowdryer has been removed!")
        BlowDryer:Destroy()
    end

end
1 Like

Nevermind, a kind person Dmed me on discord and helped me with the code! Tysm for the help guys! :slight_smile: