Extremely "hacky" leaderstats update code

So I wanted to update a GUI to keep up to date with a players leaderstat data.
Heres my code:

local formatNumber = (function (n)
	n = tostring(n)
	return n:reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
end)

local bytes = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Bytes")
local currentNumber = bytes.Value

bytes:GetPropertyChangedSignal("Value"):Connect(function()
	local value = Instance.new("IntValue")
	value.Value = currentNumber
	currentNumber = bytes.Value
	
	game:GetService("TweenService"):Create(value, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),{
		Value = bytes.Value
	}):Play()
	
	value.Changed:Connect(function()
		bytesIcon:setLabel(formatNumber(value.Value).." Bytes")
	end)
	
	wait(1)
	value:Destroy()
end)

What it looks like:


forgot to mute spotify :weary:

2 Likes

You don’t need to use values or TweenService. You can use the out quad easing style function:

local current = -- the current value
local goal = -- the desired value

local start = os.clock() -- starting the timer
local duration = -- duration of the "tween"

local function easeOutQuad(x)
    return 1 - (1 - x) * (1 - x)
end

while true do
    local text = easeOutQuad((os.clock() - start) / duration) -- Getting the value.
    TextLabel.Text = tostring(text)
    --[[
        (os.clock() - start) / duration is just to calculate the percentage of time passed
        relative to the duration. It can be interpreted like this: [(ELAPSED TIME) / DURATION]
    --]]
    game:GetService("RunService").RenderStepped:Wait()
end
5 Likes

Interesting, I’ll take a look into this!