Ad
Log in to vote
7

What does the tick function in ROBLOX do? [closed]

Asked by
FrEeZeX 35
7 years ago

Um, so I've seen this in a couple of places and I'm wondering what exactly it does?

Locked by adark and Articulating

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
5
Answered by 7 years ago

Update: ROBLOX has actually added a way to get proper unix time natively! The os.time function will return proper UNIX time in UTC.


There are two time functions in ROBLOX.

tick returns the current server time in "Unix Time Format". It is not proper Unix time. It's a very large number, and this number is affected by timezones as servers in ROBLOX are misconfigured and return the wrong time, based on the timezone that they are in.

You can divide this number out and get the current time, then compensate for timezones by adding up to 12 or subtracting up to 11. This number could be slightly different on the client than on the server, as it queries the operating system for the time, and is not synchronized across the network.

time is a function in ROBLOX that returns the game time. This is the number of seconds that the server has been running, and is synchronized across the network, meaning it will be the same in local scripts as it is in server scripts. If you are doing anything that doesn't require accurate real-life time or saving it outside of the game instance, then this is the preferred method.

How to get proper UNIX time

If you would like to get the proper, true UNIX time that is not affected by timezones, you will have to use HttpService. I have set up a resource URL here, free to anyone to use:

http://scriptinghelpers.org/resources/unix_time

You can use this code to retrieve the proper UNIX time:

function unix_time()
    local http = game:GetService("HttpService")
    if not http.HttpEnabled then
        error'Please enable HttpService'
    end
    local time_str = http:GetAsync("http://scriptinghelpers.org/resources/unix_time", true)
    return tonumber(time_str)
end

This function will return nil/false if there is an error.

Here is another version which should be faster on all subsequent calls:

function unix_time()
    local http = game:GetService("HttpService")
    if not _G.__cache_time then
        if not http.HttpEnabled then
            error'Please enable HttpService'
        end
        local time_str = http:GetAsync("http://scriptinghelpers.org/resources/unix_time", true)
        if not tonumber(time_str) then
            error("An error occurred while fetching the time")
        end
        _G.__cache_time = tonumber(time_str)
        _G.__fetched_time = tick()
    end
    local cache = _G.__cache_time
    local fetched = _G.__fetched_time

    return (tick() - fetched) + cache
end

This answer has been updated as the information on the ROBLOX Wiki Article was actually wrong, and the tick function does, in fact, not return proper unix time.

Ad
Log in to vote
2
Answered by
jobro13 980 Moderation Voter
7 years ago

A VERY important note on tick() used on servers is that it returns server time (UNIX time, from January 1st, 1970, 00:00:00 - in seconds passed since then) on THAT timezone. I needed to explain and convince people a thousand times that we cannot use tick() to get the relative time between to visits. If you join at a server in America and right after that in United Kingdom the time difference will be several hours, as it's later in United Kingdom.

tick() is used to get time differences in script. With tick(), you can for example measure how much time an operation exactly costs;

start_time = tick()
for i = 1,1000 do
game.Workspace.BasePlate.BrickColor = BrickColor.palette(math.random(0,63))
end
print("This operation took: "..tick() - start_time.."s!")
0
And here we go again. Please, check out this: http://www.roblox.com/TICK-TEST-place?id=144371954. Read the description please for more info. You can also test this yourself. We cannot use tick() to get relative visit times, as tick() does NOT return the same value on every server - it can have hours of difference! jobro13 980 — 7y
1
No offense on this though: a lot of people think they can use tick() to get relative visit times. I got into this problem when I tried to setup an in-game updater which had to add content every 24 hours. I noticed that the countdown clock sometimes showed 7 hours, and on a new server 4 hours (for example). I only used server tick() on there - that got me into the fact that ticks() cannot be used t jobro13 980 — 7y
1
That's incorrect. Unix time is based on UTC time. Read about it here (http://en.wikipedia.org/wiki/Unix_time). It will always be the same on all servers. There can be a slight difference on the client side if it is configured incorrectly, because in LocalScripts, it's based on the client's operating system. User#11893 186 — 7y
0
I know what it has to do, but on roblox, it doesn't. Please join my test place so I can point it out for you. jobro13 980 — 7y
0
We were arguing about different things. The number that tick returns isn't actually unix time. Unix time itself is not affected by timezones, though the number that tick returns actually is affected by timezones. User#11893 186 — 7y