Cannot get the mouses correct position

You can write your topic however you want, but you need to answer these questions:

  1. The mouses position is not EVEN close to the correct position. Here is my code: local connection = runService.RenderStepped:Connect(function()
    local mousePos = UIS:GetMouseLocation()

     textLabel.Position = UDim2.new(mousePos.X, 0, mousePos.Y,0)
     warn(mousePos.X, mousePos.Y)
    

    end)

All variables are defined elsewhere in the script. The mouse position is returning something weird like this: Mouse X: 34, Mouse Y 513.

2 Likes

Try replacing mousePos to PlayerHere:GetMouse()

I’ve already tried that, It ends up with the same result.

1 Like

How are you sure that the position is wrong

1 Like

I am attempting to move the textlabels position to the mouse but its ending up off the screen because the position X and Y is over 1. Which is off the screen.

Divide X and Y individually with ViewportSize of the camera to get a scale from 1 to 0 (X=X/Camera.ViewportSize.X Y=Y/Camera.ViewportSize.Y)

2 Likes

How do I get the ViewPortSize (Sorry I am not very good with UI)

1 Like

You are setting the Position’s Scale X and Y instead of the Offset. The Mouse’s position is based on AbsolutePosition (Offset). You could change it by setting the Position to Offset instead of Scale.

Udim2.new(0, mousePos.X, 0, mousePos.Y)
3 Likes

This also works

1 Like

Thanks guys! I really appreciate the help!

1 Like

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