How do I increase clock time

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.

1 Like

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

1 Like

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.

1 Like

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
1 Like

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.