Hello! I’m wondering how I could use os to create a global wait time (30 minutes), then fire a RemoteEvent. Any help is appreciated! (I don’t even know how to use os sadly.)
You’d create a checkpoint 30 minutes ahead by taking os.time() and adding 30 minutes (1800 seconds) then on some interval check if you’ve reached it (by comparing to what os.time() is now). I’ve had best performance by checking every minute, and if it’s closing in on the timer (say 2 minutes out) I’ll make a new thread to count it down more accurately.
How would I start that though? I have absolutely NO experience whatsoever in this sadly.
What do you mean by a global wait time?
As for creating a timer with os.time:
local function onFinished()
print("Timer finished!")
-- TODO: Add code to execute when the timer finishes.
end
local heartbeat = game:GetService("RunService").Heartbeat
local function startTimer()
local endTime = os.time() + 30*60
local connection
connection = heartbeat:Connect(function()
if os.time() > endTime then
onFinished()
connection:Disconnect()
end
end)
end
Here’s a quick example of waiting 10 seconds with some debug output to show what’s happening:
local currentTime = os.time()--current time on the server
local goal = os.time() + 10 -- 10 seconds in the future
while task.wait(1) do--check the time every second...
currentTime = os.time()--update currentTime so we can compare/output it
if currentTime>=goal then--if the servers' time is equal or greater than the timer we set, we've reached the goal
print("Goal reached!")
break--exit the loop
else--otherwise, we have to wait longer.
print(goal-currentTime.."...")--this will display how many seconds remain
end
end
Should this be Local or Server?
How can I get this to repeat? charrr
Repeat how? Keep counting down again? Instead of breaking the loop you could reassign goal = os.time() + 10 and it will keep counting.
Ah, thank you so much!
charr
Just for reference, this code shouldn’t be used. This code can be simplified to task.wait(10) print("Goal reached!")
. There isn’t a reason to use ‘os.time’ in this case, since it yields like the normal task.wait
function.
If you want something that doesn’t yield your code you should create a connection to an event that fires continuously, like heartbeat or renderstepped.