I’d like to make a “Time Played Gui” similar to this one:
But i don’t know how to start. If someone could help me with this, thank you! ;D
I’d like to make a “Time Played Gui” similar to this one:
But i don’t know how to start. If someone could help me with this, thank you! ;D
is the time played gui also going to be on a leaderboard for everyone to see or is it only seen by player/client?
You can just use tick() in a loop to get seconds spent in game every x times, format it into minutes then hour(s).
It is going to be Client only. Not making a leaderboard
ah ok! You can do this as a base for time displayed in seconds:
local Player = game:GetService(“Players”)
local TimePlayedUI = script.Parent --make sure it is parented to a Textlabel instance for it to work
local Time = 0
while true do --looping using while loop
task.wait(1)
Time += 1
TimePlayedUI.Text = "Time Played: "… Time
end
note: make sure the TimePlayedUI is a Textlabel or textbutton based on the function you are looking for.
This will certainly work and work well, but it bugs the perfectionist inside of me a bit. The reason being that there will be drift (though likely negligible). (and there will technically always be drift because clocks aren’t perfect and time is strange anyways, this is just less drift)
It’s because you will never get exactly 1 second waits when you call task.wait() it will always be off by a small amount. As such it’s more accurate to do something like
--your stuff
local Player = game:GetService(“Players”)
local TimePlayedUI = script.Parent --make sure it is parented to a Textlabel instance for it to work
local Time = 0
--My stuff
local startTick = tick()
local function getSecondsPlayed()
return tick() - startTick
end
--now we use your loop again with a small modification
while true do --looping using while loop
task.wait(1)
Time = getSecondsPlayed()
TimePlayedUI.Text = "Time Played: "… math.floor(Time) --We don't need decimals
end
oh that is true, thanks for correcting me!
Well, I had a little bit of free time and I decided to make that by myself. And i did this:
local Players = game:GetService("Players")
local playersGui = Players.LocalPlayer.PlayerGui
local TimePlayedTextLabel = playersGui.TimePlayed:WaitForChild("TimePlayedTextLabel")
local Seconds
local Minutes
local Hours
local s = 0
local m = 0
local h = 0
while true do
task.wait(1) -- Put this with 0.001 if you want to make a Speedrun Timer
s = s + 1 -- Seconds
if s <= 9 then
Seconds = "0" .. s
else
Seconds = tostring(s)
end
if m <= 9 then -- Minutes
Minutes = "0" .. m
else
Minutes = tostring(m)
end
if s == 60 then
s = 0
m = m + 1
end
if h <= 9 then -- Hours
Hours = "0" .. h
else
Hours = tostring(h)
end
if m == 60 then
m = 0
h = h + 1
end
print(h, m, s)
TimePlayedTextLabel.Text = "Timeplayed:" .. Hours .. ":" .. Minutes .. ":" .. Seconds
end
This is probably not the best code, but it works exactly for what I need!
Be free to give me some feedback!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.