GetMouseLocation Example Code Broken

The example code snippet on the page for UserInputService:GetMouseLocation is broken.

This snippet attempts to move a GUI to the mouse location. However, the value returned by GetMouseLocation is a Vector2, while all GUI objects expect a UDim2 for their Position property.

This results in the following error being produced every frame:

image

Affected article:
https://developer.roblox.com/en-us/api-reference/function/UserInputService/GetMouseLocation

2 Likes

Thank you for reporting this! I’ve just added it to our todo backlog.

Update: the code sample has been fixed - the change should be visible soon. For anyone searching for this problem, change line 7 from this:

    gui.Position = mouseLocation

to this:

    gui.Position = UDim2.new(0, mouseLocation.X, 0, mouseLocation.Y)
4 Likes

Since you’re using 0 values for both scales, I’d assume fromOffset is also appropriate to use here? Possibly even canonical?

gui.Position = UDim2.fromOffset(mouseLocation.X, mouseLocation.Y)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.