How to kick player when timer hits 0?

Hi. I made a basic script that does a timer and I wanted to kick you when the timer ends.
Here is the script:

local time = 400
for i = 1,400 do
wait(1) 
time = time - 1 
	
script.Parent.Text = tostring(time) 
end
local timer = 400

while task.wait(1) do
    timer -= 1

    script.Parent.Text = tostring(timer)
end

player:Kick(reason)
2 Likes

will that kick the player when the timer hits 0? ill make my own variables dont worry

1 Like

Yeah, the player:Kick(reason) line will only be able to run once the while loop is finished

The above post keeps going on forever with no stop – this has a break

local timer = 400
while task.wait(1) do
timer -= 1
if timer == 0 then
player:Kick(reason)
break
end
end
2 Likes

This is wrong – it’ll keep going on forever with no stop.

1 Like
isnt break used inside a loop? will this script work?


 local function onCharacterRemoving(char)

 if timer == 0 then

 local player = game.Players:GetPlayerFromCharacter(char)

 player:Kick("Sorry")
 
end


end
1 Like

ok yes sorry i just woke up the above code will FINALLY work

1 Like

True, didn’t notice that

local timer = 400

while task.wait(1) and timer > 0 do
    timer -= 1

    script.Parent.Text = tostring(timer)
end

player:Kick(reason)
2 Likes

there needs to be context to it, does it spam onCharacterRemoving?

1 Like

only when the timer reaches 0 i belive but i havent tested it

1 Like

…can you test it, my script, and Orbular3’s script?

1 Like

orbulars script and urs both work, but i end up with an error

local Player = game:GetService("Players")

Player:Kick("Yes")
1 Like

game:GetService("Players") returns the service “Players”, not an actual player.

3 Likes

how would i end this then?

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
		player:Kick("nil")
1 Like

If by that you mean the function, then use return

1 Like
local timer = 400

while (timer > 0) do
    timer -= 1
    wait(1)
end

player:Kick(reason)

@Orbular3 Even simpler solution.

1 Like

k thanks for your help i finally got the kick working
image

1 Like
local player = game.Players.LocalPlayer

for i = 400, 0, -1 do
	task.wait(1)
end

player:Kick("Timer ended!")
local player = game.Players.LocalPlayer

for i = 400, 0, -1 do
	script.Parent.Text = tostring(i)
	task.wait(1)
end

player:Kick("Kicked!")