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

Any way to get the player's mouse position?

I'm making a game with a custom mouse, and I don't want to use mouse.Icon because it's broken. Is there anyway to detect when the mouse is moved, then update the frame's position to the mouse's position?

1 answer

✓ Accepted answer

You can use the Mouse's .Move signal to run code respectively when the Cursor's position changes. Using Mouse.Y and Mouse.X in conjunction, we can get the current UDim2 position on screen, and assign it to our customary Cursor.


To actively hide the main Cursor, we can use UserInputService's MouseIconEnabled property to make it invisible


RemoveMouseLocalScript

local UserInputService = game:GetService("UserInputService")
UserInputService.MouseIconEnabled = false

CustomCursorLocalScript

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Cursor = script.Parent

Mouse.Move:Connect(function()
	local MousePos = UDim2.new(0, Mouse.X, 0, Mouse.Y)
	Cursor.Position = MousePos
end)

Note:

Ensure you set the AnchorPoint of the customary Cursor GUI to .5,.5, so we can center it.

7 comments

0

Moved is not a valid member of PlayerMouse?

0

Apologies, Move*

0

The event is called "Move" not "Moved". Also, it says "Udim2 was expected ,got vector2, which isn't a surprise, but I don't know how to fix this.

0

Ah, right. My bad once again. I just finished a late assignment so I’m a tad bit tired at the moment.

0

Try the code once more

0

Figured it out. Problem was the mouse.x and mouse.y was in the wrong order.

0

That is embarrassing;) Happy to have helped

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