How to kick a player after time played

Hello developers

I’m working on a game and i want to kick the players if they play the game for 10minutes, im not a scripter so i dont know how, i searched on youtube and on the forum but didn’t find a solution. If you are a scripter please tell me how to do it.
Thanks

1 Like

What I would do is create a while true loop and then using a variable do like a count down system where in the variable you have the amount of seconds (in this case it would be 600 seconds) and then just take one every loop if you are not at 0 yet.

Here is some code that should work (server script):

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local CountDown = 20 -- Seconds they have before getting kicked (set at 10 mins)
	while true do
		wait(1)
		if CountDown ~= 0 then
			CountDown -= 1
		else
			plr:Kick("Time is up (10 mins has past)!") -- You can change the kick message inside of the string
			break
		end
	end
end)

I would recommend learning how to code if your trying to make a game and don’t plan to hire someone to do it!

2 Likes

change countdown to

local CountDown = [minutes] * 60

that way you don’t have to check how many seconds is in x minutes

1 Like

Simple:

local Player = game.Players.LocalPlayer

task.wait(600)
Player:Kick("You waited 600 seconds (10 minutes)")
1 Like

Why? There is no real reason why you would require it for this piece of code. I was thinking about doing it but 10 minutes is legit only 600 seconds so is not a large number and you can easily calculate minutes in seconds.

1 Like

I would not massively recommend doing this in a local script, mainly because someone could if they wanted to exploit they could remove the script and stay in-game. This may not be an issue with what this user is doing but if the game is based around that you get kicked out in 10 minutes it kinda then defeats the whole point of the game.

By the way I would recommend doing what @kdopdaux1803 did rather then a loop like I did but just inside of a server script after the user gets detected cuz thinking about it the loop is pointless and takes up more resources then required.

Use this code rather then the one I sent before:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)

task.wait(600)

plr:Kick("You waited 600 seconds (10 minutes)")

end)
1 Like

Why?

It’s a LocalScript, because if it’s a ServerScript it says that Kick is nil, so I rather do a LocalScript instead of a ServerScript.

I do not want Players.PlayerAdded:Connect(function(plr) or else it will not work. It will just surpass the line and dodges the PlayerAdded.

1 Like

You’d rather do a LocalScript instead of a LocalScript :face_with_raised_eyebrow: ?

2 Likes

Oh yeah, true let me fix this.

Edit: Fixed.

2 Likes

Your point makes no sense? What do you mean, if it is a server script it will say that kick is nil? If your trying to do game.Players.LocalPlayer inside of the server script of course it is going to say nil because you can’t get the local player of a server.

Why not? There is nothing wrong with detecting when a player is added and is used lots of times in Roblox game code. All you do is detect when the player is added via the PlayerAdded event, connect it up with a function and then use the player parameter you get from the event to kick the user out when they have waited 10 minutes.

Like I said already, because exploiters can edit and delete scripts that are on the client meaning that someone could just delete the script and stay in the game longer then 10 minutes. Exploits however cannot access the server directly (the reason why we use remote events and stuff to interact with the server → client and client → server; the thing which causes clients not to be able to access the server from client is the filtering enabled which is designed exactly to stop exploiters doing things such as this.

1 Like

Oh, so I did this (Yes, it’s my Button Simulator game but whatsoever, it’s fine.):

while task.wait() do
	script:Clone()
	script.Parent.LocalScript5:Clone()
	script.Parent.Parent.StarterCharacterScripts.walkToBest:Clone()
	script.Parent.Parent.StarterCharacterScripts.LocalScript:Clone()
	script.Parent.Parent.StarterCharacterScripts.LocalScript2:Clone()
	script.Parent.Parent.StarterCharacterScripts.LocalScript3:Clone()
	script.Parent.Parent.StarterCharacterScripts.buttonSound:Clone()
end

Oh wait, it, just… crashed my Studio.

1 Like

Well it may have crashed studio cuz you make lots of different instances quickly over and over again.

1 Like

thx you helped me , and im learning how to code but im not good, but thx

Could you try my script?

1 Like

okay sure why not imma try it rn :slight_smile:

You could also use tick().

Like this:

local PlayerTimerAdded = nil
local player = nil
game.Players.PlayerAdded:Connect(plr)
player = plr
PlayerTimerAdded = tick() + amountoftimehere
end)
game:GetService("RunService").Heartbeat:Connect(function()
if player and PlayerTimerAdded and math.round(tick() - PlayerTimerAdded) == 0 then
player:Kick("time limit xd")
end
end)

You get the point. Tick is just the amount of seconds passed after January 1st 1980 smth, so adding like 20 seconds to it is just the current time + 20 seconds passed. So when the current time is the tick + number, it kicks the player. Dont really know if this is optimized but yea.

2 Likes

You have to wait 10 minutes until it works.

Also, I forgot to change the text of the kick to Time's up ! 10 minutes (600 seconds) has scrolled up !

1 Like

Are you sure about this?

Tick just made my Studio go crash into to while do loop, it’s instantaneous, like the while true do loop.

1 Like

Whats the code u wrote on it?

aaaaa

1 Like

I wouldn’t recommend using tick because it’s planned to be deprecated in the future.

1 Like