UserInputService:GetMouseLocation() doesn't work properly

I tried to make a script that make a circle appear in the position of the mouse pointer when I click on the screen but the circle appear far at right / under the pointer.
It get worse the further the pointer is from the left hand top corner.
Can anyone help me to fix this?

StarterGui = game:GetService("StarterGui")
Players = game:GetService("Players")
StarterGui = game:GetService("StarterGui")
UserInputService = game:GetService("UserInputService")

player = Players.LocalPlayer
frame = script.Parent.Frame

function CreateFrame(pos)
	frame.Position = UDim2.fromOffset(pos.X, pos.Y)

	frame.Visible = true
	task.wait(0.2)
	frame.Visible = false
end

UserInputService.InputBegan:Connect(function(input, processed)
    if processed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local mousePos = UserInputService:GetMouseLocation()
		CreateFrame(mousePos)
    end
end)

2 Likes

Could you send a screenshot of your script

I’d make sure the AnchorPoint of the frame is set to Vector2.new(0.5, 0.5) and IgnoreGuilnset is set to true in the ScreenGui

1 Like

I already tried both but they didn’t work.

can I see what the explorer looks like?

That’s very strange… I tried it on my own using your script and everything worked perfectly fine… Here’s the file and a video if you want to analyze
ClickingUI thing.rbxl (60.3 KB)

1 Like

I just found what caused this! There is another script which put an UIScale in the ScreenUI when the game starts so it multiplied the position of the frame.

oh lol
you can mark your post as the solution if everything checks out

You need to subtract GuiService:GetInset().Y for it to get the proper mouse position

As @Zer0Wit has mentioned, if your ScreenGui has the property IgnoreGuiInset disabled, then you must subtract GuiService:GetInset().Y from pos.Y to position the circle correctly. The reason why your circle is displayed wrongly while being a few pixels away from the current mouse location is because your mouse location, obtained from UserInputService:GetMouseLocation() ignores the GUI inets, meaning, the location is relative to the top-left of your screen, ignoring the GUI insets. Therefore, when you try to set the position of your frame, your position of your frame, because of ScreenGui.IgnoreGuiInsts set to false, it is relative to the top-left, slightly bottom of the screen.

If you set your ScreenGui.IgnoreGuiInsets to true, then you don’t have to subtract the Y value of the GuiInsets from the position.