Trying to make a TextLabel follow the Mouse Cursor

So, here’s what I want:
I have a GUI, and there’s a regular button in it. When I hover over the button, a TextLabel will appear (It will describe the function and the purpose of that button), and that TextLabel is supposed to follow your mouse cursor. When you leave the button’s area, the TextLabel disappears, simple as that.

I tried doing this, but for some reason, the TextLabel won’t appear at all, and there are no errors in the Developer Console. Can someone help me understand what’s wrong?

(It’s a LocalScript, if it wasn’t obvious)

This is what I have:

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

local button = script.Parent
local label = script.Parent.TextLabel

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

button.MouseEnter:Connect(function()
	label.Visible = true
end)

button.MouseLeave:Connect(function()
	label.Visible = false
end)

function follow()
	if label.Visible == true then
		label.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
	else
		return
	end
end

mouse.Move:Connect(follow)

I will appreciate all kinds of help. Thanks.

3 Likes

If it’s an ImageButton, try using a TextButton instead.
Also, to clean up your code a little:

function follow()
	if not label.Visible then return end
	label.Position = UDim2.fromOffset(mouse.X,mouse.Y)
end

Just use this very easy script:

local Player = game.Players.LocalPlayer
local Character = Player.Character

local Mouse = Player:GetMouse()

local Button = script.Parent

function Move()
	Button.Position = UDim2.fromOffset(Mouse.X - 50 ,Mouse.Y - 50)
end

Mouse.Move:Connect(Move)
3 Likes