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

Positioning a GUI button using scale and mouse position?

Hello!

Currently I'm working on a game that will use only buttons. You will be able to place a button at the mouse position and I want it to be in the same position for every device.

I think it is possible, but I didn't find the right idea. Any suggestion on how to do that? Currently, I'm trying to set the button's position to UDim2.new(0.5, Mouse.X / 2, 0.5, Mouse.Y / 2), which gives a weird result.

1 answer

✓ Accepted answer
UDim2.fromScale(Mouse.X / ScreenGui.AbsoluteSize.X, (Mouse.Y - 36) / ScreenGui.AbsoluteSize.Y)

I think something like this will work since Mouse.X is a number between 0 and screen width, ScreenGui.AbsoluteSize.X is the actualy screen width, so dividing Mouse.X by this number will always return you a number from 0 to 1, that is the Scale position that you need. I subtract 36 from Mouse.Y since there is the topbar offset added automatically, not sure if that's an issue for you. You should also subtract Mouse.X with half of the button's offset Size to make it centered.

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