How to have accurate bar size?

Hello I’m currently making a leveling system but this bar if not clamped goes past it since the numbers go as high as 2200. but if I clamp it it still looks like its at the end even if its halfway?

Script:

local player = game.Players.LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local leaderstats = player:WaitForChild("leaderstats")
local MaxXp = player:WaitForChild("MaxXP")
local level = leaderstats:WaitForChild("Level")
local Xp = leaderstats:WaitForChild("XP")

wait(1)

local progress = math.clamp(Xp.Value / MaxXp.Value, 0, .563) -- Keep between 0 and 1

script.Parent.Parent.Progress.Text = Xp.Value .. "/" .. MaxXp.Value
script.Parent.Size = UDim2.new(progress, 0, .037, 0)

Xp.Changed:Connect(function(value)

	script.Parent.Size = UDim2.new(progress, 0, .037, 0)

	script.Parent.Parent.Progress.Text = tostring(value) .. "/" .. tostring(MaxXp.Value)
end)

image:

please help!

Why does comment says the opposite as to what it does?Im so confused now
image
It clamps numbers between 0 and 0.563 but comment says that it clamps between 0 and 1…???
What?

2 Likes

Oh yeah sorry about that orginally it was one but i changed it to .563 because thats the size I want to keep it in between or else it goes past. Plus I forgot to update the comment

That’s because it is.

Say that the Max XP was 100 and your current XP was 60. 60 / 100 = 0.60, which goes over the threshold of 0.563 but is clamp to not show that.

If you want it to scale with the GUI’s size, you should multiply it by the max size.

local FrameSize = 0.563 * (Xp.Value / MaxXp.Value)
script.Parent.Size = UDim2.new(FrameSize, 0, .037, 0)

Xp.Changed:Connect(function(CurrXP)
	local NewFrameSize = 0.563 * (CurrXP / MaxXp.Value)
	script.Parent.Size = UDim2.new(NewFrameSize, 0, .037, 0)
	script.Parent.Parent.Progress.Text = tostring(CurrXP) .. "/" .. tostring(MaxXp.Value)
end)
1 Like

Tysm! It works perfectly. I don’t know if I ever would’ve figured that out :sweat_smile:

1 Like

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