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()
Thank you all in advance.
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()
Thank you all in advance.
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
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.
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
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
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
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.
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
feel free to dm me if you need any help.
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?
It does.
30charactwersssssssssssssss
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