Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Does anyone know how to overcome the error: attempt to compare UDim2?

The title is self-explainatory. The line of code:

59 | 		until StaminaBar.Size > UDim2.new(0, 350, 0, 50) or UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)

I'm trying to get the stamina to not "overflow" when a person rapidly fires a Input into the script, so I'm using a > instead of ==, does anyone know how to make this work, please send a full example of code, it makes it easier to understand.

1 comment

0

If anyone wants to know how it can "overflow" for the time being, their is a wait(2) for when a player lets go of the button, and since its a GUI, it duplicates ontop of each other. their is a loop that adds +1 to a variable, so when it duplicates, it passes the mark.

1 answer

✓ Accepted answer

Since UDim2 values are table values, you need to compare the X and Y separately. To put simply:

(this.X > that.X) and (this.Y > that.Y)

Since this is a horizontal bar, you don't need to compare the Y, meaning you can shorten it to:

this.X > that.X

Don't forget to specify which property we are checking X on (and also if its offset or scale).

StaminaBar.Size.X.Offset > 350

3 comments

0

Although the bar does go slightly over still, thats just because lua is stupid, and the roblox engine isn't the best. However. This solution still works. Thank you :D

0

I honestly still prefer taking out the wait because it does go over with the wait, and since I'm striving for a 0 bugged script, I might want to take out the wait().

0

I should mention, you can use variables to store the internal stamina values and just use the UI to represent it visually. Also, you can set the size property to fill the bar if it ovefills. Or maybe math.clamp(), but then again, variables are nicer.

This discussion is preserved as it was. New answers, comments, and votes are closed.