Help on a Duration Bar System

Hi guys, so recently I’ve run into this issue where I want a bar to scale on the x axis to 0, but the issue is that the time Isn’t entirely right. I’m not too sure on how to word this properly but I have this tick() called gamestarttime and as I run this while loop decreasing the duration, once it ends I subtract the tick() with the previous gamestarttime tick(). But it usually prints a number that is far greater than that of what i wanted it to.
image

Originally I wanted to make it around 3 seconds, but over here, it’s 3.3 seconds. Now the problem is much bigger I believe if I had a bigger duration value. Now, I could have a subtraction value of 1,
image
but it looks choppy, I’ve also tried to use tweenservice, i’ve but it has this weird slowing down in the middle and still, the late value persists, please let me know on how I can solve this!

P.S: Because I don’t really know on how I should word this, I’m not entirely sure on how I should find a search similar to this, but I did try searching things like duration bar and stuff, but I don’t think anything that was useful to me came. If there’s a proper post that will help me understand this, please let me know! :grinning:

local screengui = script.Parent

local bar = screengui:WaitForChild("Bar")

local mainbar = bar:WaitForChild("MainBar")

wait(5) -- waiting for the client to load

local gamestarttime = tick()

local duration = 3

local maxduration = 5

while wait() do
	duration -= 0.03
	mainbar.Size = UDim2.new(duration/maxduration, 0, 1, 0)
	if duration <= 0 then
		break
	end
end

print(tick()-gamestarttime)

Also, here’s an example of how it looks.
image
This is just a testing frame for something I might make in the future. :grinning:

I have made something that is actually pretty similar to something I had wanted therefore I’ll mark it as solution. But if there’s a better way to approach this, please do let me know!

local screengui = script.Parent

local bar = screengui:WaitForChild("Bar")

local mainbar = bar:WaitForChild("MainBar")

wait(5) -- waiting for the client to load

local gamestarttime = tick()

local duration = 3

local maxduration = 5

local renderstepped

renderstepped = game["Run Service"].RenderStepped:Connect(function(delta)
	duration -= delta
	mainbar.Size = UDim2.new(duration/maxduration, 0, 1, 0)
	if duration <= 0 then
		renderstepped:Disconnect()
		print(tick()-gamestarttime)
	end
end)

It might be better if you used :TweenSize on the mainbar instead

https://developer.roblox.com/en-us/api-reference/function/GuiObject/TweenSize

This code might work:

local screengui = script.Parent

local bar = screengui:WaitForChild("Bar")

local mainbar = bar:WaitForChild("MainBar")

wait(5) -- waiting for the client to load

local duration = 3
local beforeTween = os.clock() -- time before the tween

guiObject:TweenSize(
	UDim2.new(0, 0, 1, 0), -- The desired size at the end
	Enum.EasingDirection.In, -- not really important
	Enum.EasingStyle.Linear, -- keeps a constant rate of change 
	duration, -- in how many seconds you want the tween to finish
	true, -- overrdes other tweens
	function()
        print("The tween took exactly: "..tostring(os.clock-beforeTween)) -- the time the tween took to finish
    end
)