How to get a mouse position over a gui

So this is pretty simple, but I’m not very familiar with it. I have a gui and I want a textlabel to appear when a player hovers over a frame. I want the textlabel’s position to be the mouse’s position for as long as the mouse is in the frame, but then when the mouse leaves the frame, I want it’s visible to be set to false. I have a local script inside of my frame, and there are no errors, but for some reason the textlabel isn’t showing up at the mouse’s position. When I tried to print out when it entered and left, it printed out fine though. Here’s the local script inside of the frame:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ui = script.Parent
local text = ui.UIName

ui.MouseEnter:Connect(function()
	text.Position = UDim2.new(mouse.X, 0, mouse.Y, 0)
	text.Visible = true
end)

ui.MouseLeave:Connect(function()
	text.Visible = false
	text.Position = UDim2.new(0, 0, 0.5, 0)
end)
4 Likes

If I am understanding what you are asking, what you could do is inside of your .MouseEnter function is add something along the lines of:

game.UserInputService.InputChanged:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
       local xOffset = mouse.X - ui.AbsolutePosition.X - (text.AbsoluteSize.X / 2)
       local yOffset = mouse.Y - (ui.AbsolutePosition.Y)
       text.Position = UDim2.new(0, xOffset, 0, yOffset) 
    end
end)

Whenever the mouse leaves, your .MouseLeave should just make it not visible.
Mess around with the above code, and see what works for you, + or - some extra values for the offsets if you need to.

1 Like