Wait for a specified amount of time in real life

How does one detect when a specified amount of time passes? For example, I jail a person for 30 minutes and I have he has to be 30 minutes in-game. How do I count these 30 minutes? I am sure I wont use

``````wait()
``````

use wait(30*60)
wait accepts seconds as parameter

But if the player leaves the game? I will have to count it from the start?

if you dont want to use datastoreservice yes

``````for i= 1,math.huge do
print(i)
end
``````
1 Like

Also, if I want to display the time left, how would I do that?

use `os.time()` and then add how many second you want in it, but in your case since you want minutes do `os.time()+(60*minute)` and then, save it in a datastore, whenever you want to check if the time is done, get the new `os.time()` and compare it to your saved once, if the new one is greater or equal to the old one means the time runned out, and then you free the player. this would work even when the player leaves,

i donâ€™t know if i explained it clearly, but hereâ€™s more info about os | Roblox Creator Documentation.

3 Likes

Alright, so basically I wrote a script to jail:

``````function JailModule.Jail(plr, Time, reason)
local info = {plr.UserId; Time; reason}
JailDS:SetAsync(info, true)
...
end
``````

Any ideas on how to make it detect when the time goes out? I have no idea how to do that.

i suggest using tick() for this

``````Time = tick() + 8 -- jail time in seconds

if tick() > Time then
--set the prisoner free
end
``````

Thank you, but what is the â€ś+ 8â€ť for? I canâ€™t understand

Example. You want to jail the player for 120 seconds

The current time will be tick()

and the time that prisoner can be free is tick() + 120

1 Like

So I have this script, but as far as I see from the print it doesnâ€™t change the time value at all.

``````function CountTime(plr, Time)
print("start count")
local jail = true
local timeToFree = tick() + Time -- jail time in seconds

while jail do
print(timeToFree)
wait(1)
end
if tick() > timeToFree then
print("unjailed")
JailModule.Unjail(plr)
jail = false
end
end
``````

i think you forgot to put the if statement inside the loop. Should be

``````	while jail do
print(timeToFree)
wait(1)
if tick() > timeToFree then
print("unjailed")
JailModule.Unjail(plr)
jail = false
end
end
``````
1 Like

this wonâ€™t stop since

jail is true and you didnâ€™t chang it to false, until here

but of course this wonâ€™t run since while loop is still running

1 Like

Alright thank you @Lielmaster and @ronald, Iâ€™ll continue working on the script and tell you when I will need more help in terms of saving time when the player leaves the game.

1 Like

just a question, should the player be in game for the time to go down? if so then there are other things you have to do

1 Like

feel free to dm me if you need any help.

1 Like

Yes, heâ€™s supposed to be in game for the time to go, and when he leaves the time saves until the player gets back in game, then continues working, etc.

Wait. So that means it doesnâ€™t count if player is not in game?

1 Like

It does.
in this case you need to save `timeToFree - tick()` instead of `timeToFree`. By doing this, you save the time left before they are free, and when they join in, get the `tick()` and add the number that you saved and that will be the new timeToFree