I need help about "MouseEnter"

  1. What do you want to achieve? I want to do a GUI
    that when you hover the mouse on the button, a textlabel appears and then follows the mouse’s position, but when the mouse leaves, it dissapears.

  2. What is the issue? I can’t do it, I know all about MouseEnter, but I can’t really do stuffs that related to the mouse.

  3. What solutions have you tried so far? I tried to do a lot of research about it but I still can’t find any

2 Likes

So first make a LocalScript in that frame and add a few variables (you have to add the paths)

local frame = script.Parent
local label = script.Parent.Label
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local mouseMove = nil

After that we will start our mouse enter event, make the label visible and set the position to the mouse position:

frame.MouseEnter:Connect(function()
    label.Position = UDim2.new(0, mouse.X, 0, mouse.Y - 36)
    label.Visible = true
end)

After that we want to add a mouse move event so that we can keep updating the label position:

mouseMove = mouse.Move:Connect(function()
    label.Position = UDim2.new(0, mouse.X, 0, mouse.Y - 36)
end)

This should hopefully be working, but we want to disconnect the mouseMove event and make the Label invisible once our mouse leaves the frame :

frame.MouseLeave:Connect(function()
    if mouseMove then 
        mouseMove:Disconnect() 
        label.Visible = false
    end
end)

Final code:


local frame = script.Parent
local label = script.Parent.Label
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local mouseMove = nil

frame.MouseEnter:Connect(function()
    label.Position = UDim2.new(0, mouse.X, 0, mouse.Y - 36)
    label.Visible = true
    mouseMove = mouse.Move:Connect(function()
        label.Position = UDim2.new(0, mouse.X, 0, mouse.Y - 36)
    end)
end)

frame.MouseLeave:Connect(function()
    if mouseMove then 
        mouseMove:Disconnect() 
        label.Visible = false
    end
end)

Hope this helps, had to do this all from my head and on mobile so there could be typos and this is untested

3 Likes

oh my gosh. Thanks! It has some mistakes in it… You didn’t make the label dissapearing part. But I fixed it! But thank you sm! It helped me a lot

local frame = script.Parent
local label = script.Parent.TextLabel
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local mouseMove = nil

frame.MouseEnter:Connect(function()
	label.Position = UDim2.new(0, mouse.X, 0, mouse.Y - 36)
	label.Visible = true
	mouseMove = mouse.Move:Connect(function()
		label.Position = UDim2.new(0, mouse.X, 0, mouse.Y - 36)
	end)
end)

frame.MouseLeave:Connect(function()
	if mouseMove then 
		mouseMove:Disconnect()
		label.Visible = false 
	end
end)
2 Likes

No problem, like I said

So yeah, I forgot to make the label visible part. I edited my reply

1 Like