I created simple clock in Roblox Studio (Lua) and added wait(1)
between while loop. The problem is that it doesn't refresh. It just shows time and it's everything. This is code:
local StarterGui = game:GetService("StarterGui")
while true do
local UniversalTime = DateTime.now():ToUniversalTime()
local Hours = UniversalTime.Hour + 1
local Minutes = UniversalTime.Minute
local Seconds = UniversalTime.Second
if (Minutes < 10) then
Minutes = "0"..Minutes
end
if (Seconds < 10) then
Seconds = "0"..Seconds
end
StarterGui.ScreenGui.Frame.Clock.Text = `{Hours}:{Minutes}:{Seconds}`
wait(1)
end
I don't know what caused the problem and I don't really know how to repair it.
StarterGui
replicates to the client as PlayerGui
. They'll only see changes as the player if you update the value from there.
Keep your clock in your server, and then have the clients listen for updates to that object's value. This way, you've got one source of truth for the time.
Alternatively, if you want to keep using DateTime (which is also fine), you can set up your clock underneath StarterGui
as you do here, but change your definition (and your var name appropriately) so that it will reference the PlayerGui
after it replicates. Understand, though, that by using this method, you accept that you won't use this value for anything besides the clock GUI. It'll be too prone to losing sync to use it for anything sensitive.
local gui = script.Parent -- will reference `PlayerGui` after client replication
while true do
local UniversalTime = DateTime.now():ToUniversalTime()
[. . .]
gui.ScreenGui.Frame.Clock.Text = `{Hours}:{Minutes}:{Seconds}`
wait(1)
end
Side note: I recommend looking into the task library to find better ways of executing this with threading. Specifically, spawn
; a simple change that will lead to better practice.