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…
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