Displaying information on TextLabel following Mouse

  1. What do you want to achieve? Keep it simple and clear!

I want to make a textLabel that displays the textbuttons/frames(items) when hovering over them and that follows the mouse, kinda like adopt me!
https://gyazo.com/6521ca76a3fe58a2b7261bfde539ca99

  1. What is the issue? Include screenshots / videos if possible!

I tried to achieve that with the script below; the only problem is that when i move out my mouse position out of the TextButtons area, the TextLabel is still here.

https://gyazo.com/fed39d34bafdd8d30c9fe62d632f06c7

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I did tried to look up the DevForum, i only found about the GetGuiObjectsAtPosition, which im not used to it yet

this is the UI; the white square is the frame that’ll follow the mouse, the textlabel’s parent is the square.
image

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")
local textLabel = script.Parent.handle.textLabel

runService.RenderStepped:Connect(function()
	textLabel.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y)

	local guisAtPosition = player.PlayerGui:GetGuiObjectsAtPosition(mouse.X, mouse.Y)

	for _, gui in pairs(guisAtPosition) do
		if gui:FindFirstChild("item") then
			textLabel.Text = gui:FindFirstChild("item").Value
			textLabel.Visible = true
		elseif gui:FindFirstChild("face") then
			textLabel.Text = gui:FindFirstChild("face").Value
			textLabel.Visible = true
		end
	end
end

I also tried to add this after the last elseif statement, but now, the textLabel doesnt display at all

else 
	textLabel.Visible = false

Simple, just add a debounce.
Enable it when the mouse cursor is hovering the button if its hovering then set that debounce value to true.

Example:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local runService = game:GetService("RunService")
local textLabel = script.Parent.handle.textLabel

local isHovered = false

some_button.MouseEnter:Connect(function()
     isHovered = true
end)

some_button.MouseLeave:Connect(function()
    isHovered = false
end)

runService.RenderStepped:Connect(function()
	textLabel.Parent.Position = UDim2.new(0,mouse.X,0,mouse.Y)

	local guisAtPosition = player.PlayerGui:GetGuiObjectsAtPosition(mouse.X, mouse.Y)

	for _, gui in pairs(guisAtPosition) do
		if gui:FindFirstChild("item") then
			textLabel.Text = gui:FindFirstChild("item").Value
			textLabel.Visible = isHovered
		elseif gui:FindFirstChild("face") then
			textLabel.Text = gui:FindFirstChild("face").Value
			textLabel.Visible = isHovered
		end
	end
end

NOTE: THIS IS JUST AN EXAMPLE
So you have to replace some_button with a actual button.

How can i achieve that with frames, MouseEnter/MouseLeave isn’t a function of Frames

Hi, It is.
image
As you can see it says “GuiObject” and the frame is a gui object.

There’s another way!
You can use Frame.InputBegan.

https://developer.roblox.com/en-us/api-reference/event/GuiObject/MouseEnter

1 Like

ok i changed the entire code and just used MouseEnter and MouseLeave inside a for loop instead

Did it worked, any issue in output?

Yes it worked, no issues in output , thanks a lot! never tought about using only MouseEnter and MouseLeave to be honest, i always try to complicate things lol

1 Like