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

How to get a player's local time? / GetService / Roblox API

Asked by 5 years ago

Hi, now ... I'm looking for a way to get a player's local time, either from their computer or mobile phone.

First I thought that I need a "GetService" service. But I don't know

I do not have any example script. Sorry

I need your help please.

How can I learn more about the services? and What is "Roblox API"

I Speak spanish

2 answers

Log in to vote
6
Answered by
Link150 1355 Badge of Merit Moderation Voter
5 years ago
Edited 5 years ago

I'd like to go further and expand on Kiriot's answer.

os.time() returns UTC time, which is (roughly) the same accross all computers around the world at the same time, regardless of time zones.

tick() returns local time; that is, time as set by the underlying computer's configuration, which seems to be what you are looking for.

Both functions return a "timestamp", which is a number representing an amount of seconds elapsed since some earlier date. Furthermore, they are UNIX timestamps. UNIX is an old operating system that laid the foundations for a great deal of things in the world of computer science. For one, it established a standard starting date and time for timestamps, being the 1st of january 1970, at midnight.

As indicated by Kiriot, tick() and os.time() only return timestamps, which aren't exactly human-readable. If you wish to display time in HH:MM:SS format, you may do it like so:

local function formatTime(t)
    local t = t or tick()

    local hours = math.floor(t / 3600) % 24
    local mins = math.floor(t / 60) % 60
    local secs = t % 60

    return string.format("d:d:d", hours, mins, secs)
end

This little handy formatTime() function can either accept a timestamp to format as argument, or it can generate one on its own based on the current local time.


As for your second question, the Roblox API is the set of features added to Lua by Roblox. This includes all of its variables, functions, and object classes.

The Roblox Wiki's "API Reference" section is essentially a big documentation of everything added by Roblox. You'll want to use that to know how to use Roblox's features properly:

There's also an official Lua reference manual that serves both as an introductory book of sorts to the language as well as a list all of the features of Lua:

Hope this helped you, have fun coding. :)

0
Doesn't os.time() return the local time aswell though? Amiaa16 3227 — 5y
0
Nope. It's Universal Coordinated Time. Link150 1355 — 5y
0
Isn't d supposed to be %d User#24403 69 — 5y
0
Looks like that's an issue with the website. I indeed write "% 02d", with no spaces. Link150 1355 — 5y
0
thanks "Greetings from Argentina" :) proPVPgamers_YT 40 — 5y
Ad
Log in to vote
1
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

The tick() and os.time() functions return the player's unix time when used in a LocalScript. If you want to get the time in h:m:s format, you will need to convert it from the unix timestamp.

Roblox API is stuff such as workspace, GetService, etc. in roblox lua. Basically anything which isn't in normal lua.

GetService() returns the service you specify. There are many services such as Workspace, Players, ReplicatedStorage and so on. Here is the list of all services, although most of them you probably won't use soon.

0
Please clarify that Roblox API stuff like the method GetService are made in Lua and thus are normal Lua, just a method, not a new language. User#25115 0 — 5y
1
Except you're wrong because they're made in C++, not in Lua. If you want to verify it yourself, do string.dump(game.GetService) Amiaa16 3227 — 5y
0
^ My bad. User#25115 0 — 5y

Answer this question