Attempt to compare UDim2 <= UDim2

I’m trying to compare 2 UDim2 values to check if one is smaller than the other, however this is not working. All help is appreciated.

while wait() do
	script.Parent.MiddleBar.SurfaceGui.CallingAt.Position = UDim2.new(1, 0, 0, 0)
	repeat
		script.Parent.MiddleBar.SurfaceGui.CallingAt.Position = script.Parent.MiddleBar.SurfaceGui.CallingAt.Position - UDim2.new(0.005, 0, 0, 0)
		wait()
	until script.Parent.MiddleBar.SurfaceGui.CallingAt.Position <= UDim2.new(-1, 0, 0, 0)
end

I think comparing UDim values isn’t possible. A solution would be to write your own function that breaks the UDim into it’s x, y components and compare them individually(that way you have control over how the comparison is made).

Also most of the time you don’t have to compare UDim or Color3 values because they can be broken down into smaller problems with easier solutions, for example:

local CallingAt = script.Parent.MiddleBar.SurfaceGui.CallingAt
while task.wait() do
	CallingAt.Position = UDim2.new(1, 0, 0, 0)
	repeat
		CallingAt.Position -= UDim2.new(0.005, 0, 0, 0)
		task.wait()
	--only check the scale x, since you are only changing that
	until CallingAt.Position.X.Scale <= -1
end
2 Likes

You can’t compare UDim2 values because they are table values. You need compare each component seperately:

until script.Parent.MiddleBar.SurfaceGui.CallingAt.Position.X.Scale<=-1
8 Likes