How to check the position of an UI?

Hello,

So for a script I am programming I need to know the position of a UI. For some reason the code I am running does not seem to work. I am unsure why, does anyone have any ideas how I can fix/do this?

This is the code that checks currently:

The above code does not want to run though. I checked on google and could not find anything on how to check the position.

Try this https://devforum.roblox.com/t/how-do-i-check-if-the-distance-between-2-guis-is-a-number/1129725

Edit: what was mentioned above won’t work

That above post does not seem related, but ok…

Are you sure Distance is less than 12.5 studs?

1 Like

Whoops! I thought the thing above was about distance between guis.

Make sure that the distance is accurate and the position is also accurate.

You can also try absolute position

To check if the Positon and distance are accurate try this


if Distance < 12.5 and plr.PlayerGui.ScreenGui[“Register Start Screen”].Position == UDim2.new(0.288,0,0.235,0) then

print(“both statements are true, this means that the code should run”)
-—do whatever else here

else
print(Distance)
print(plr.PlayerGui.ScreenGui[“Register Start Screen”].Position)

—see if the values are true or false compared to the if statements

end

That code likely won’t work, since they’re already doing that.

Most likely, but what if it was an error in position or distance? that code was just meant to check if the if statements were true (because the code they provided should work)

I checked the distance works through a print it just does not print when the position is at the position.

After trying to replicate the problem I think I found the issue.

Whenever you try to print the position, it is not the exact position. (I believe this is a floating point error.)

Try to do this


local function normalizeUDim2(udim2)
    return UDim2.new(
        math.round(udim2.X.Scale * 1000) / 1000,
        udim2.X.Offset,
        math.round(udim2.Y.Scale * 1000) / 1000,
        udim2.Y.Offset
    )
end

local Position = normalizeUDim2(yourPosition)

if Position == UDim2.new(0.288,0,0.235,0 then
—code here
end

It does not seem to work. I just don’t understand why it’s not working…

hmm. Another way I tried (which probably which won’t work in most cases) is that I printed the position of the GUI right before the if statements ran. Then I changed the if statements to the printed position.