Local Script not correctly detecting values

Hello all,

I am attempting to make a script that will display a players in-game play time on a GUI - in a nutshell the script SHOULD loop and update the text, however the text defaults to 0:00 or 0:01. Any attempts to print the current value will make it print zero.

The script below is a local script, located in a TextLabel.

local label = script.Parent
local player = game.Players.LocalPlayer

local value1 = player.leaderstats.Hours.Value
local value2 = player.leaderstats.Minutes.Value

repeat wait(0.5)
	print (value1)
		
	label.Text = ""..value1..":0"..value2..""
		
	until value2 >= 10	
repeat wait(0.5)
	
	label.Text = ""..value1..":"..value2..""
until value2 <= 1

I would assume the issue at hand is the values it’s tracking, and not the script itself, however the clock is handled on the server-side, the main script being located in ServerScriptService.

There’s probably a better way to handle something like this, this is just what I slapped together!

You immediately get the value property, meaning it wont care about future changes to taht property. Just have the values store the instances and then get the value property there

local label = script.Parent
local player = game.Players.LocalPlayer

local value1 = player.leaderstats.Hours
local value2 = player.leaderstats.Minutes

repeat wait(0.5)
	print (value1)
		
	label.Text = ""..value1.Value..":0"..value2.Value..""
		
until value2.Value >= 10	

repeat wait(0.5)
	label.Text = ""..value1.Value..":"..value2.Value..""
until value2.Value <= 1

Although this feels like a weird way to do what is needed. I think it’s easier to have a function that listens for when the value is changed and update the text using string formatting

Example

local function updateText()
	label.Text = string.format("%d:%02d", value1.Value, value2.Value)
end

updateText()

value1.Changed:Connect(updateText)
value2.Changed:Connect(updateText)

%0d2 means that it wants a whole number and if then umber is less than 10 (2 digits), it’ll add a 0, so no need to check for that yourself

I see, admittedly I feel a little stupid for not realizing my mistake after seeing what it was haha. I’ll look into using the Changed event, it’s certainly a lifesaver in this case.
Thank you so much!

1 Like