How to tween a gui bar by a percentage

How can i make a gui bar tween by a percentage, like for example in my game i made it so that you collect 5 bones, and then i want a progress bar so when i pick up one bone the gui goes up by 20% because 20% x 5 = 100. So it needs to go up in 20s. If you have anything that helps with this topic, please comment it because anything helps.

Just do some basic math:
If maximum is 5 then 20% would be 5 times 0.2 so you can do

local Max = 5
local CurrentBone = 0


SomeEventThatGivesBone:Connect(function()
    if Max > CurrentBone then
        CurrentBone += Max * 0.2
    end
end)

This is just an example of using percentage. For your tweening you can use this as an example.

Use percentage of bone/max multiplied by the GUI bar length:

new_bar_length = (boneCount / maxBoneCount) * maximum_bar_length;

Then tween from the current_bar_length to the new_bar_length.

1 Like

How can i tween it? From the current to the new?

If it’s a bar make sure the frame’s AnchorPoint is 0.5, 0 (applies to horizontal bar, otherwise use 0, 0.5 and edit the tween). Then, use TweenService. You can do it like:

local TweenService = game:GetService("TweenService")
local bar: Frame -- where your bar is

local barInfo = {
	["Max"] = 5,
	["Current"] = 0,
}

local tweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	-- These three are up to your liking
	0,
	false
)

local tween = TweenService:Create(bar, tweenInfo, {
	["Size"] = UDim2.new((barInfo.Current / barInfo.Max) * 1, 0, 1, 0)
        -- You would want to do UDim2.new(1, 0, (barInfo.Current / barInfo.Max) * 1, 0)
        -- ...in the event of a vertical bar
})

function collectedBone()
	barInfo.Current += 1
	tween:Play()
end

Ok THANKS, i will try it out asap, ill let u know if it works

it doesnt work, this is what happens

Can I see how your progress bar (the green progress) is parented within the PlayerGui? Or, well, the entire progress bar UI, as well as the code.

No its fine, i got it to work, but thanks for the help, i used ur code to get it to work. (: