Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Which do I use, os.time() or tick()?

Asked by 5 years ago

Which one would I be using if I was trying to do something like an hour cooldown? How exactly would I go about doing this as well?

Thanks for any help/suggestions.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

os.time() and tick() are almost the same, os.time() returns the UNIX Epoch time, which is the time since:

Unix time (also known as POSIX time or UNIX Epoch time) is a system for describing a point in time. It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds.

The main difference between them is that tick() returns local time and may vary from server to server, os.time() is best used to calculate differences in time out of a single server.

How would you use this to set a cooldown? Simple, take the difference between os.time() at two points in time. We can use a variable to store the first point in time then subtract it with the current point in time.

-- Time when script first runs
local oldTime = os.time() -- example: returns 1000000 (for simplicity)

-- Note: There are other ways to do this but a simple while loop could explain this pretty well.
while true do

    local currentTime = os.time()
    if currentTime - oldTime >= 3600 then -- check if the difference is greater than or equal to 3600 seconds (1 hour)
        print("TIME IS UP!!")
    end

    wait() -- don't forget this
end

I'm not sure what kind of cooldown you were looking for, the above example is definitely not practical so if you could specify a certain situation I might be able to help further.

Also, usually a wait(seconds) would do for a simple cooldown.

0
Great explanation! The kind of cooldown I was thinking of was like one of those for mobile games, where when you play a certain amount of games, you wouldn't be able to play for another certain amount of time before being able to again. MmC00KIESmM 5 — 5y
0
TLDR: If it was going to be like a mobile game energy system, then how would I use os.time() MmC00KIESmM 5 — 5y
0
Hmm, you would probably save the first os.time() in a datastore so that when they log off and log in again, you can call os.time() again, get the current time and deduct it from the last time you saved in the datastore to get the time since they logged off because time runs on even when the game is off. If that was what you were asking. GoldAngelInDisguise 297 — 5y
Ad

Answer this question