Time format system shows error but works fine

Hello there! I have tried making a time format system (seconds to hours, min etc.) It works fine, but i get this annoying error which i think could break something

Screenshot 2024-01-02 134344

I have tried formatting the function in another way, but that didn’t help.

Here’s the code if you wish!

local plr = game.Players.LocalPlayer
local plrTime = plr.leaderstats.Time

local formatted_time_string

local timeLabel = script.Parent

local function Format(int)
	return string.format("%02i", int)
end

local function formatTime(seconds)
	local minutes = (seconds - seconds%60)/60
	seconds -= minutes * 60
	local hours = (minutes - minutes%60)/60
	minutes -= hours * 60
	local days = (hours - hours%24)/24
	hours -= days * 24
	
	formatted_time_string = Format(days) .." days, ".. Format(hours) .." hours,".. Format(minutes) .." minutes, ".. Format(seconds) .. " seconds"
end

local function changeLabel(value)
	formatTime(value)
	timeLabel.Text = formatted_time_string
end

changeLabel(plrTime.Value)

plrTime.Changed:Connect(changeLabel(plrTime.Value))
2 Likes
plrTime.Changed:Connect(changeLabel) --> args from .Changed passed automatically

Edit. @SavAlt17

Connect accepts a function and passes the values an event provides automatically (just like PlayerAdded passes Player instance.

Writing plrTime.Changed:Connect(changeLabel(plrTime.Value)) would provide Connect with whatever changeLabel returns (it would call changeLabel).

(Side suggestion)

Make sure plrTime is always available.

local plrTime = plr:WaitForChild("leaderstats"):WaitForChild("Time")
1 Like

the changeLabel function needs a value becase the code wont know what number to format, so it wouldn’t work, sorry.

1 Like

Thank you so much @fellow_meerkat, turns out I never knew that about the connect function, thanks! :slight_smile:

edit @fellow_meerkat, it worked first time, when I play again, i get this error:
Screenshot 2024-01-02 140014

Deleting plrTime.Value, the code defines it as nil for some reason.

second edit @fellow_meerkat, turns out i forgot to remove the brackets from the code, I left it with the bracket!

plrTime.Changed:Connect(changeLabel())

Thanks and sorry for such a confusing time!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.