# How to convert the value of a NumberValue in the Player to Hours:Minutes:Seconds in a TextButton

Hello Developers,
I added a NumberValue into the Player, also added a Datastore for it (works fine).
When the Player joins I added a loop that the value returns -1 until its 0. If 0 it goes Back to 10
Here is the Script for this:

``````game.Players.PlayerAdded:Connect(function(Player)
while true do
repeat
wait(1)
Player:WaitForChild("FreeSkips").Time.Value -= 1
until Player:WaitForChild("FreeSkips").Time.Value == 0
Player:WaitForChild("FreeSkips").Value +=1
Player:WaitForChild("FreeSkips").Time.Value = 10
end
end)

``````

I also added a TextButton in StarterGui. The Text of the TextButton should be the Text of the Value of the NumberValue. I also tried converting the seconds into Hours:Minutes:Seconds, but this function doesnāt works. Is there a way to fix this? Any help would be appreciated!
Hereās the local Script:

``````local textLabel = script.Parent

local function formatTime(Time)

local hours = math.floor(Time / 3600)

local minutes = math.floor((Time - hours * 3600)/60)

local seconds = math.floor(Time - 3600 * hours - minutes * 60)

return string.format("%02d:%02d:%02d", hours, minutes, seconds)

end

local Time = game.Players:WaitForChild("FreeSkips").Time

while true do

textLabel.Text = formatTime(Time)

end
``````
``````local function formatTime(Time)

local hours = math.floor(Time / 3600)

local minutes = math.floor(Time/60)%60

local seconds = math.floor(Time) % 60

return string.format("%02d:%02d:%02d", hours, minutes, seconds)

end
``````

The % is the modulus function, this makes essentially makes values repeat like a clock, hour % 12 for example would go 0,1,2,3,4,5,6,7,8,9,10,11, 0,1,2,3,4,ā¦ etc. . it sort of repeats each value, hence we have to repeat minutes and seconds from 60 since seconds are 0-59 like minutes, and hours are the final time value we can have as many as those as we want, if you wanted to add a day function you could do:

``````local function formatTime(Time)
local days = math.floor(Time/(3600*24))

local hours = math.floor(Time / 3600)%24

local minutes = math.floor(Time/60)%60

local seconds = math.floor(Time) % 60

return string.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds)

end
``````
1 Like

Did you forget a .Value after Time?

1 Like

Oh my. I hope this will fix the error. What a beginner mistake. I will see if it worked!

local textLabel = script.Parent

local function formatTime(Time)

local hours = math.floor(Time / 3600)

local minutes = math.floor((Time - hours * 3600)/60)

local seconds = math.floor(Time - 3600 * hours - minutes * 60)

return string.format("%02d:%02d:%02d", hours, minutes, seconds)

end

local Time = game.Players:WaitForChild(āFreeSkipsā).Time
while true do

textLabel.Text = formatTime(Time.Value)

end

Also using wait(1) doesnāt always correlate to real time if there is any lag that can cause wait(1) to actually be longer than 1 second.
Try this info as well Keeping Time.

1 Like

still doesnāt worksā¦ (The Value is in the Player and the Value of this Value is getting changed by a Server Script so it saves when you leave. The Text should just copy the Value of the Value and convert it into Hours:minutes:Seconds (for example if the value is 68 seconds the text of the button would be 00:01:08)

The value being changed on the server should replicate on the client.

Just curious, is the āTimeā Value meant to be in the Player rather than the players service, since you reference it as game.Players.Time rather than game.Players.SpecificPlayer.Time

2 Likes

Oops made another mistake.

``````local textLabel = script.Parent

local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

local Time = game.Players.LocalPlayer:WaitForChild("FreeSkips").Time.Value

while true do
textLabel.Text = formatTime(Time)
end``````

still doesnāt worksā¦ thatās strangeā¦
EDIT: Thanks to @nicemike40 and @TheEternalKittenLord for helping me out. I thought I had to add the .Value thing to the Time location, but I had to add it to formatTIme(Time.Value)

Try this

``````local textLabel = script.Parent

local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

local Time = game.Players.LocalPlayer:WaitForChild("FreeSkips")
Time.Changed:Connect(function()
textLabel.Text = formatTime(Time.Value)
end)
``````
1 Like

Yes, writing a variable as the value of an numbervalue will the set the value to the number, so when the value of the numbervalue changes the variable doesnt change as its pointing to the actual number, not the value of the numbervalue, for example if you have an numbervalue that with value 20

then local a = numbervalue.Value is equivilant to local a = 20, so when value changes a is still just 20.

2 Likes

Script timeout: exhausted allowed execution time
comes in the output when I delete the function task.wait(1). Is there a way to make it so that it directly changes, so you donāt have to wait 1 second?

``````local textLabel = script.Parent

local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

local Time = game.Players.LocalPlayer:WaitForChild("FreeSkips")
Time.Changed:Connect(function()
textLabel.Text = formatTime(Time.Value)
end)
``````

try this (i put it above, i think you missed the message)

1 Like

I tested this and it is kinda lagging. Only every 5-10 Seconds it changes and this only 1 timeā¦

``````local textLabel = script.Parent

local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

local Time = game.Players.LocalPlayer:WaitForChild("FreeSkips").Time

while true do
wait()
textLabel.Text = formatTime(Time.Value)
end
``````

idk why but this is working somehow. When I use task.wait(1) it breaks somehowā¦

Hmm, thatās weird I quickly set up the same system and it seems to work fine for me. Let me send a gyazo of my scripts.

https://gyazo.com/a903558371638f89c80f476c4b039117

(Sorry that I quickly went through the scripts, gyazo only allows for 7 seconds per gif so I had to show the timer was functioning without lag)
Server (ServerScriptService):

``````local Players = game:GetService('Players')
local RunService = game:GetService('RunService')

local Connections = {}

local t = Instance.new('NumberValue')
t.Name = 'Time'
t.Parent = plr
local c = RunService.Heartbeat:Connect(function(dt)
t.Value += dt
end)
Connections[plr.UserId] = c
end)
Players.PlayerRemoving:Connect(function(plr)
Connections[plr.UserId]:Disconnect()
end)
``````

Client (In the textlabel):

``````local Time = game.Players.LocalPlayer:WaitForChild('Time')
local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
Time.Changed:Connect(function()
script.Parent.Text = formatTime(Time.Value)
end)
``````

Let me edit the serverscript to reduce lag with all the runservice connections.

1 Like