Why doesn't the player get kicked?

Hello,

I have been having trouble with a script. So when a certain gui is open for at least 1 minute the player should be kicked for AFK farming. But for some reason the script hasen’t been working and the output and console doesn’t give me any errors.

Here is the script:

game.Players.PlayerAdded:Connect(function(player)
local count = Instance.new("NumberValue")
count.Name = "Count"
count.Value = 0
count.Parent = player


if script.Parent.Enabled == true then
	repeat
		count.Value = count.Value +1
		wait(1)
	until
	count.Value == 60
else 
	count.Value = 0
end
end)

game.Players.PlayerAdded:Connect(function(player)
local countAmount = player:WaitForChild("Count")
if
	countAmount.Value == 60 then
	player:Kick()
	print(player.Name.." Was kicked for afk farming")
end
end)

Right now, when the script checks countAmount.Value == 60 it only checks it once, and that’s right when the player joins. At that point in time, it’d still be 0. Now, you could check repeatedly in the second PlayerAdded, however I assume you plan for countAmount to be reset somewhere else, so it’d probably be simpler to just kick the player from the counter itself, like this:

game.Players.PlayerAdded:Connect(function(player)
    local count = Instance.new("NumberValue")
    count.Name = "Count"
    count.Value = 0
    count.Parent = player
	
	while player.Parent == game.Players do -- This will continue running so long as the player is ingame
		if script.Parent.Enabled == true then
			count.Value = count.Value + 1
			if count.Value == 60 then
				-- The count has reached 60 through some means or another
				player:Kick("Please don't AFK farm!")
			end
		else
			count.Value = 0
		end
		wait(1)
	end
end)
3 Likes

Thank you so much this helps a lot! :grin:

1 Like