An integral part of my game is that the time matches the players real life time, all the time. For example, if it’s 10 AM for me the time in game should match that, but at the same time if a different player’s in real-life time is say 3PM then the game time should be 3PM for them exclusively.
If it’s possible could someone point me to any documention and/or provide anything to help me achieve this? Many thanks!
Interesting question, the first way that came into my mind was the public ip address that roblox allows, meaning you might know the country/public region then set the time accordingly, but this will be an issue, as for example the time is different in Los Angeles and New York but they are both from the United States.
I think what you can try to use is os.date() or os.time() to achieve this, from what I know, these functions work on the client to retrieve the local system time of the player’s device.
You can learn about them in the documentation, also use a localscript.
An example code is:
local timeTable = os.date("*t")
local hour = tonumber(timeTable.hour)
local minute = tonumber(timeTable.min)
local sec = tonumber(timeTable.sec)
local timeString = string.format("%02d:%02d:%02d", hour, minute , sec)
print(timeString)
you can set the hour and minute in roblox on client side using this by changing the clock property of lighting (if I remember correctly).
I could be wrong about os.date() and os.time(), as I have rarely used them but let me know if its true and works! Hope this helps!
Hey thank you for responding, this does indeed work and prints my real time in the output. I case now that I would need to loop this to update the in-game time now?
Thank you, all of you. Seeing as the OS.date method is outdated I managed to get the time cycle working using the DateTime API, this is the code I have that works for anyone in the future.
while true do task.wait(1)
local timestring = DateTime.now():FormatLocalTime("LTS", "zh-cn")
LightingService.TimeOfDay = timestring
end