Help with tweening a UDim2

Hello! I’ve been trying to create a system where the size of a bar will change depending on how many players are on a team. However, the bar will either become absolutely huge or really tiny. I’ve tried all that I can think of, including UISizeConstraints, using UDim2.new instead of fromScale(), and trying lots of different things including math functions I thought might help.

I know that it’s a problem with the maths used to get the scale components, but I just can’t seem to figure it out. Any help is appreciated!

local function updateBarSize()
    --'originalRedPlayers' and 'originalBluePlayers' are set to the players on the teams at the start of each round
	task.wait(0.5)
	local bluePlayers = #teamBlue:GetPlayers()
	local redPlayers = #teamRed:GetPlayers()
	local redPercentage = tonumber(math.floor(redPlayers / originalRedPlayers * 100))
	local bluePercentage = tonumber(math.floor(bluePlayers / originalBluePlayers * 100))
	if redPercentage == 0 then
		redPercentage = 1
	end
	if bluePercentage == 0 then
		bluePercentage = 1
	end
	local redTween = tweenService:Create(redBar, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out),
		{Size = UDim2.fromScale(redPercentage, 1)}
	)
	local blueTween = tweenService:Create(blueBar, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In),
		{Size = UDim2.fromScale(bluePercentage, 1)}
	)
	redTween:Play()
	blueTween:Play()
	
	if redPlayers == 1 then
		redLabel.Text = #teamRed:GetPlayers().." Player left"
	else
		redLabel.Text = #teamRed:GetPlayers().." Players Left"
	end
	
	if bluePlayers == 1 then
		blueLabel.Text = #teamBlue:GetPlayers().." Player left"
	else
		blueLabel.Text = #teamBlue:GetPlayers().." Players Left"
	end
end

Explorer placement:
image

Can you send a screenshot how the ui looks?

1 Like

Your issue is here. A scale UDim goes from 0-1 and above you’re multiplying by 100 when the x / y is already between 0-1. Remove the * 100 from both lines.

I’ve already tried doing that and it hasn’t worked. I’ll give it another go. This is how the UI currently looks in-game:

Are you sure you tried exactly what I said before? If it’s going over then the values must be over 1. It should be like this. Also you do not need a tonumber() as they are already numbers.

local redPercentage = math.floor(redPlayers / originalRedPlayers))
local bluePercentage = math.floor(bluePlayers / originalBluePlayers))

I just added the tonumber() for no reason… I’m not sure why I left it there. I’ll get rid of it. Also, if it helps, I am keeping the size values decimal (e.g. scaled down from {1, 0}, {1, 0} to keep it the same size on all devices.

I tried what you suggested and it turned out like this:

Print out both values and see what they return. It’s possible that x is bigger than y thus giving higher numbers than 1 (e.g 2/1 and not 1/2 because 2>1)

These are the sizes of both bars:
image
Just an idea, but it might be to do with the decimal scaling value, the max size being the size of the outer frame.

The values are fine, as long as they under 1 they will always be a percentage of the parent size. Do you have a uilistlayout in the same parent or is the blue bar placed on a different x?

All of it is parented to one frame. The red bar seems to be going to the maximum size it can, the size of that frame. I’m just gonna try dividing it by 2 and see what happens. Inside each bar, there is a UICorner, UIStroke, and a UIGradient, which have no effect on the size.

What about the PlayerDisplay textlabel, is that centered and does it have a background?

That’s centered and working fine. It seems diving by 2 has helped a lot - It’s not tweening, but it’s not going bigger than the frame either.

Update: The size is fine, it’s staying within the size it should. Just trying to fix the tween now.

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