Um, so I've seen this in a couple of places and I'm wondering what exactly it does?
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.
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.
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!")
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?