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
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.
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)
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.