Trouble with mouseover text and textbuttons

Hey. I am making a system where whenever you hover on a text button, it should show a frame with info on the mouse. Here is the module script so far:

local module = {}

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local frame = player.PlayerGui:WaitForChild("HoverText"):WaitForChild("Frame")

local function updatePos()
	frame.Position = UDim2.fromOffset(mouse.X, mouse.Y)
	frame.Visible = true
end

function module.hoverText(text, ui)
	ui.MouseEnter:Connect(function()
		frame.BoostAmt.Text = text
		while wait() do
			updatePos()
			-- how can i break it here if it isnt in anymore?
		end
	end)
end

return module

So the problem is that I don’t know how to break the while wait loop. I have tried using ui.MouseLeave after the ui.MouseEnter but that only made it invisible for a milisecond before returning to visible since the while wait loop is still active. If you have any ideas, lmk. Thanks.

I usually have a variable like local mouseIsHoveringOverUI = false and set it to true when the mouse is hovering and false when it isn’t. Then in the while loop I break it when the value is now false…

1 Like

You can use .MouseLeave to set a variable that will stop the loop.

Code:

local module = {}

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local frame = player.PlayerGui:WaitForChild("HoverText"):WaitForChild("Frame")

local function updatePos()
	frame.Position = UDim2.fromOffset(mouse.X, mouse.Y)
	frame.Visible = true
end

function module.hoverText(text, ui)
	local isHovering = false
	
	ui.MouseEnter:Connect(function()
		isHovering = true
		
		frame.BoostAmt.Text = text
		
		while task.wait() and isHovering do
			updatePos()
		end
	end)
	
	ui.MouseLeave:Connect(function()
		isHovering = false
	end)
end

return module
1 Like

I always use this:

task.wait()
script.Parent.Active = true

local t = Instance.new("TextLabel")
local gui = script:FindFirstAncestorWhichIsA("ScreenGui")

t.Text = script.Text.Value

t.Size = UDim2.fromOffset(100, 0)
t.TextWrapped = true
t.ZIndex = 2

t.BackgroundTransparency = 1
t.TextColor3 = Color3.new(1, 1, 1)
t.TextSize = 12
local i = 0
local scr = Instance.new("ScrollingFrame")
scr.AnchorPoint = Vector2.new(1, 1)
scr.BorderSizePixel = 0
scr.CanvasSize = UDim2.new()
scr.AutomaticSize = Enum.AutomaticSize.X

scr.BackgroundTransparency = 0.5
scr.BackgroundColor3 = Color3.new()
scr.ScrollBarThickness = 5
scr.VerticalScrollBarInset = Enum.ScrollBarInset.ScrollBar
scr.ScrollingEnabled = false

scr.Parent = gui
t.Parent = scr
repeat
	t.Size += UDim2.fromOffset(0, 1)
	i+=1
	if i==100 then
		task.wait()
		t.Size+=UDim2.fromOffset(50, 0)
		i = 0
	end
until t.TextFits
scr.Size = UDim2.fromOffset(math.min(t.AbsoluteSize.X, 200), math.min(t.AbsoluteSize.Y, 150))
scr.AutomaticCanvasSize = Enum.AutomaticSize.Y



function visible()
	scr.Visible = true
	t.Visible = true
	local timeleft = 1
	while timeleft > 0 do
		timeleft -= task.wait()
		if scr.Visible == false then return end
	end
	local dt = 0
	local cpos = Vector2.zero
	while scr.Visible do
		dt = task.wait()
		local newpos = Vector2.new(0, (cpos.Y + (dt * 30)))
		if newpos.Y >= scr.AbsoluteCanvasSize.Y-scr.AbsoluteWindowSize.Y then
			task.wait(2)
			newpos *= Vector2.xAxis
		end
		cpos = newpos
		scr.CanvasPosition = scr.CanvasPosition:Lerp(cpos, dt * 10)
	end
end
function invisible()
	t.Visible = false
	scr.Visible = false
end

script.Parent.MouseEnter:Connect(visible)
script.Parent.MouseLeave:Connect(invisible)

game:GetService("UserInputService").InputChanged:Connect(function(obj)
	if not t.Visible then return end
	if obj.UserInputType==Enum.UserInputType.MouseMovement or obj.UserInputType==Enum.UserInputType.Touch then
		local p = obj.Position
		scr.Position = UDim2.fromOffset(p.x, p.y)
	end
end)

invisible()
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.