How to get the mouse coordinates inside the Text button?

  1. What do you want to achieve? My goal is to get the mouse coordinate inside the TextButton and then check if the mouse is on the right or left side for it to do the action.

  2. What is the issue? The problem is when I try to get the mouse coordinates inside the text button it gives the same output.

  3. What solutions have you tried so far? I’ve tried using the while loop to update the position but it just keeps repeating the same coordinate “-1,-1”.

Output:
output

This is the code I made so far.

local ts = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local ClickDetector1 = script.Parent.Car_Body.CTEST1.Frame.TextButton

local mouseLocation = UserInputService:GetMouseLocation() - ClickDetector1.AbsolutePosition


ClickDetector1.MouseEnter:Connect(function()
	while true do
		print(mouseLocation)
		wait(0.5)
	end
end)

ClickDetector1.MouseLeave:Connect(function()
end)

Use this.

local mouse = game.Players.LocalPlayer:GetMouse()
local positionrelative = Vector2.new(mouse.X, mouse.Y) - ClickDetector1.AbsolutePosition
print(positionrelative) --> #, #

This will work, however I noticed that in your code, you set this variable already. But only once, so this value stays the same forever. Set this variable inside your while loop inside the MouseEnter event.

It’s still not working with the changes but it creates an error that I don’t understand.

Code with the changes:

local ts = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

local mouse = game.Players.LocalPlayer:GetMouse()

local ClickDetector1 = script.Parent.Car_Body.SurfaceGui.Frame.TextButton


ClickDetector1.MouseEnter:Connect(function()
	while true do
		local positionrelative = Vector2.new(mouse.X, mouse.Y) - ClickDetector1.AbsolutePosition
		print(positionrelative) --> #, #
		wait(0.5)
	end
end)

ClickDetector1.MouseLeave:Connect(function()
end)

There is no LocalPlayer on a server script. You must use a LocalScript to get the player’s mouse data.