Broken tool detection script

Hello devs!

i was trying to make a tool detection script.

but when someone has the tool it kicks all players instead of the player.

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	for _, tool in ipairs(player.Backpack:GetChildren()) do
		for _, child in ipairs(tool:GetChildren()) do
			if child.Name == "PlantBomb" then
				player:Kick("Patched..")
				return
			end
		end
	end
end)

Do all of the players have this tool?

game.Players.LocalPlayer is available only on the client, on the server it’s set to nil.

ipairs loops through the index, not the values.

I don’t wanna end up spoon feeding you code but:

  • Use the game.Players.PlayerAdded to get the player.
  • Use pairs instead of ipairs.

I’m not sure why it’s kicking everyone, it shouldn’t even work
If you run :Kick() on the client, you can only kick that specific player, it’s been set up this way so that one player can’t just kick another player.

No, ipairs does not loop through index only. ipairs is for arrays, pairs is for tables.

1 Like

Run the player kicking logic from a server script instead, like how Giant put it to you. My gut tells me the error stems from the script replicating to each client, which is possible, seeing as how you may be referencing every player with one player variable.

Optimisation Tip
for _, tool in pairs(player.Backpack:GetChildren()) do
   if tool:FindFirstChild(“PlantBomb”,true) then 
      —- Kick them
   end
end