Is this script well written or should it need some adjustments???
Basically what I intended to do is to make the TextLabel show 4 digits no matter the Value length so it would look something like this:
“0025”
“0250”
“2500”
function getScore()
local TextLabel = script.Parent
local Value = game.Players.LocalPlayer.leaderstats.Points.Value
local length = #tostring(Value)
if length == 4 then
TextLabel.Text = Value
elseif length == 3 then
TextLabel.Text = "0"..Value
elseif length == 2 then
TextLabel.Text = "00"..Value
else
TextLabel.Text = "000"..Value
end
end
local Score = game.Players.LocalPlayer.leaderstats.Points
Score.Changed:Connect(getScore)
function getScore()
local TextLabel = script.Parent
local Value = game.Players.LocalPlayer.leaderstats.Points.Value
local formattedValue = string.format("%04d", Value)
TextLabel.Text = formattedValue
end
local Score = game.Players.LocalPlayer.leaderstats.Points
Score.Changed:Connect(getScore)
In this modified version, the string.format function is used to convert the value to a string with a fixed width of 4 digits, adding leading zeros if necessary. The %04d format specifies a four-digit integer with leading zeros as needed. This ensures that the TextLabel will always display the value with exactly four digits.
Solution that works with decimals (e.g. 3.5 → 03.5):
local Score = game:GetService("Players").LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Points")
local TextLabel = script.Parent
function DisplayScore()
local NumberOfDigits = 4
local Value = tostring(Score.Value)
TextLabel.Text = string.rep("0", NumberOfDigits - #Value)..Value
end
Score.Changed:Connect(DisplayScore)