Mouse Position isn't setting right

Hello!

So this is my code,

local Player = game.Players.LocalPlayer

local Mouse = Player:GetMouse()

while wait() do

script.Parent.Position = UDim2.new(0,Mouse.X, 0,Mouse.Y)

end

This isn’t setting the position right. It sets it at least 300 pixels away from the cursor.
Thanks!

1 Like

The topbar imposes an inset of 36 pixels, so you will want to account for that. It might also be a better idea to use UserInputService instead since the Mouse object is legacy.

local UserInputService = game:GetService("UserInputService")
local GuiService = game:GetService("GuiService")

UserInputService.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        
    end
end)

Then in the if block you will want to subtract the inset from the mouse location.

local mouse_location = UserInputService:GetMouseLocation() - GuiService:GetGuiInset()

Then you position it accordingly.

script.Parent.Position = UDim2.new(0, mouse_location.X, 0, mouse_location.Y)

Thank you! But it still sets the mouse position where It was before,

Wait, it now works! Thank you!