why do you use tick() and if you replace it with something else it will be bad for you?
I use tick to make the time look as if it was the player local time, so If a player joines the game at 14:50 it will display 14:50 on the clock, It wouldnt be bad but it would be that nice feature for the player to see the clock is same as their local time
Thats not how it works. You are reading tick() from server, which means its getting the time when server started in its own local time, which could be in a different country than the player is located, thats not a local time of your player. If you want to get the real time of the player you should check the time from a local script
oh! you convert the hours, minutes, and seconds to seconds then add it to tick()
an hour has 3600 seconds and a minute has 60 seconds
so you should have these variables
local offsetHours = Hour * 3600
local offsetMinutes = Minutes * 60
local offsetSeconds = Seconds
then add them all together and add to tick
function module.Time(Hour: number, Minutes: number, Seconds: number)
local Profile = module.Profiles[PlayerSaved]
if not Profile then return end
local function time()
local offsetHours = Hour * 3600
local offsetMinutes = Minutes * 60
local offsetSeconds = Seconds
local t = tick() + (offsetHours + offsetMinutes + offsetSeconds)
local hour = math.floor(t/3600) % 24
local min = math.floor(t/60) % 60
local sec = math.floor(t%60)
if min <= 9 and sec <= 60 then
min = "0"..min
sec = "0"..sec
end
workspace.TimeDisplay.Clock.Screen.SurfaceGui.Display.Text = hour..":"..min..":"..sec
end
time()
end
and lastly, on the server script you can put the offset you want
--- Service
local ServerScriptService = game:GetService("ServerScriptService")
--- Module
local Manager = require(ServerScriptService.DataStore.Manager)
task.wait(4)
while true do
Manager.Time(3, 0, 5)
task.wait(1)
end
Yes but its a solo player game.
Server doesnt care if its a solo player or not, tick() gotten from server its not the local time of your player
she’s still right, even if you do it in solo server tick will still use the GMT/UTC time zone instead of your local timezone
I know but if I was to do a local script too it would take more time than I expect, the tick() is doing its job, if the player joins at 15:50 it will display 15:50 on the clock even after, if the time was 15:55 the clock will have increased to 15:55
Its “doing its job” because you are testing it in studio, which means the server and the client its your own computer, thats why the time its the same, test it in a real server, and the server could be away from you which means the tick() will use the local time of that computer/server (well… which rarely will be far away from your location)
Okay, I tested I see what you mean, I guess I won’t have that nice feature then.
I will try this in a moment, thanks
Thats the main point.
You just need a variable that holds the offset of the time you want to add or decrease.
Translate that time into seconds and sum that to the current tick().
As a suggestion, check how to use os.date() format so you can turn it into 00:00:00 style in a easy way
Alright thank you I will check into that too.
what you can do is use a remote function to get the difference between the local time and the UTC time using math.round(tick() - os.time())
in the client then use the difference to set the timezone
to set the timezone, you can check this out
My personal approach to this would be to make a new Time class that allows for offsets. That way you could use it as follows:
local clockTime = Time.new()
clockTime:SetOffsetInSeconds(10805)
while true do
clockDisplay.Text = clockTime:GetFormattedTime()
task.wait(1)
end
This way, you can also decouple the time logic from the profile stuff (why is that in the time function? does that need to be in the time function? If the profiles break, so does the time! that doesn’t make sense!) and the visual effects of the gui changing.
This also creates methods with no side effects, whose output depends only on its input and the state of the object that the method is a member of. Writing “pure” functions (functions that do not change based on any external state and do not have any side effects when called) as much as possible will help a TON with debugging down the road.
Because I’m thinking of saving the clock values in a later time
Part of your issue is treating HOURS:MINUTES:SECONDS as if its the only way to keep track of time. An ISO timestamp may be fine for humans - but if you’re doing math, you want the unit of measurement to be consistent. Convert the ISO timestamp into a Unix timestamp (in seconds), and do your math on that instead.
However, it seems like what you’re trying to accomplish is a clock based on a player’s timezone. Roblox includes a built-in datatype that makes this a breeze. DateTime | Documentation - Roblox Creator Hub
function clockTick()
local dT = DateTime.now()
workspace.TimeDisplay.Clock.Screen.SurfaceGui.Display.Text = dT:FormatLocalTime("LT", "en-us")
end
And here’s one that supports arbitrary offsets.
function clockTick(offsetSeconds:number?)
if not offsetSeconds then offsetSeconds = 0 end
local dT = DateTime.fromUnixTimestamp(os.time() + offsetSeconds)
workspace.TimeDisplay.Clock.Screen.SurfaceGui.Display.Text = dT:FormatLocalTime("LT", "en-us")
end
Fair enough. I would recommend taking that bit out of the time function, though, and doing it elsewhere. That’s because it’s a lot easier to fix, edit, and build upon your code later on when each function does just one job.
(For more info, look up “separation of concerns” or “single responsibility principle”)
I found solution by making my own clock rather than using tick() or os.time(), I can also now save it into the database of the player and the most important thing that works now is I can increase the time to by desired amount with a click of a button.
function module.Time(Hour: number, Minutes: number, Seconds: number)
local Profile = module.Profiles[PlayerSaved]
if not Profile then return end
local Hr
local Min
local Sec
local function time()
local timeString = workspace.TimeDisplay.Clock.Screen.SurfaceGui.Display.Text
Hr, Min, Sec = timeString:match("(%d+):(%d+):(%d+)")
Hr = tonumber(Hr)
Min = tonumber(Min)
Sec = tonumber(Sec)
if Hour or Minutes or Seconds then
Hr = Hr + Hour
Min = Min + Minutes
Sec = Sec + Seconds
else
Sec = Sec +1 -- Increase Seconds by 1 everytime
end
if Sec > 59 then
Sec = 0
Min = Min +1
if Min > 59 then
Min = 0
Hr = Hr +1
if Hr > 23 then
Hr = 0
Min = 0
Sec = 0
end
end
end
local FormatedTime = string.format("%02d:%02d:%02d", Hr, Min, Sec) -- Using string.format to display two digits with leading zeros
print(FormatedTime)
workspace.TimeDisplay.Clock.Screen.SurfaceGui.Display.Text = FormatedTime
end
time()
end
-- How to increase time by desired amount
script.Parent.ClickDetector.MouseClick:Connect(function()
Manager.Time(1,24,53)
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.