I want to make a panel which shows how long the server has been up in Days, Hours, Minutes and Seconds.
I’ve attempted to do this but it doesn’t work as the script starts when the player joins and the timer is different for each player depending on when they joined.
I don’t really have any idea how to have the same time for every player no matter when they join.
I would go with the approach of once the Server starts running, keep a variable that holds the current Unix time (using tick()).
Every 1 second, update the board’s display text based on tick() - startTick and keep in mind you will have to convert the calculated seconds into your wanted format.
That would probably work the same as workspace.DistrubtedGameTime and it does show the correct time for every player but then it won’t show it like 1 Minute and 5 Seconds, instead it’d be like 1 Minute and 65 Seconds as I’m doing it like this,
while task.wait(1) do
Label.Text =
string.format("%02d", tostring(math.floor((workspace.DistributedGameTime / 86400)))).." Days "..
string.format("%02d", tostring(math.floor((workspace.DistributedGameTime / 3600)))).." Hours "..
string.format("%02d", tostring(math.floor((workspace.DistributedGameTime / 60)))).." Minutes "..
string.format("%02d", tostring(math.floor(workspace.DistributedGameTime))).." Seconds "
end
To have every player see the same time on the board, you’d have to use a regular script instead of a local script. It’s fine to update the label under a regular script because this is a SurfaceGui parented to a part in the workspace. If it was under a ScreenGui under StarterGui, it’d be different.
Edit: Just read this part, which I seemed to ignore the first time around lol. It’s just a formatting issue then if you’re seeing “1 minute 65 seconds”
I have the SurfaceGui parented in StarterGui, I tried parenting the SurfaceGui to the part which it appears on but it doesn’t update, same with having the script in ServerScriptService. Right now I’m using the Adornee to show it on the part.
This is also something I overlooked O_O. Your server script should be under ServerScriptService or if you want, put it under the SurfaceGui that displays the time elapsed in the workspace.
This is the result I got with using a server Script under ServerScriptService and a part in workspace with a SurfaceGui under it to display the time.
Code:
local RunService = game:GetService("RunService")
local serverAgeDisplay = workspace:WaitForChild("ServerAgeDisplay")
local serverStart = tick()
local lastCheck = tick()
local floor = math.floor
local function formatTime(num: Number)
local days = floor(num/86400)
local hours = floor(num/3600)
local minutes = floor(num/60)
local seconds = num % 60
return {Days=days, Hours=hours, Minutes=minutes, Seconds=seconds}
end
task.spawn(function()
RunService.Heartbeat:Connect(function()
if tick() - lastCheck >= 1 then
lastCheck = tick()
-- Update the board
local timeElapsed = formatTime(floor(tick()-serverStart))
serverAgeDisplay.SurfaceGui.AgeLabel.Text = timeElapsed.Days .. " Days " .. timeElapsed.Hours .. " Hours " .. timeElapsed.Minutes .. " Minutes " .. timeElapsed.Seconds .. " Seconds"
end
end)
end)
Something to note is I didn’t check for if there was only 1 of any given value to remove the ‘s’ so it’s not plural. i.e, if only 1 minute has passed it’ll still say “1 minutes” instead of “1 minute.”
--this can run on both server and client
--format function to maintain zeros(for example 0:2:5:10 -> 00:02:05:10)
function f(text)
return string.format("%02i", text)
end
function toText(value)
--a day has 86400 seconds
local days = math.floor(value/86400)
value %= 86400
local hours = math.floor(value/3600)
value %= 3600
local minutes = math.floor(value/60)
value %= 60
local seconds = value
return f(days).." Days "..f(hours).." Hours "..f(minutes).." Minutes "..f(seconds).." Seconds"
end
--check every second
while task.wait(1) do
--seconds since the server started
local seconds = workspace.DistributedGameTime
local text = toText(seconds)
print(text)
end
Perhaps you could create a value in the server stoarage for the time then set the text lable in the surface gui to be the same text as the value of the value in the server stoarge.