I have a GUI where each time I hover over a button, it highlights it by creating a little bar beneath it, which gets removed when the mouse leaves, however if you spam wipe your mouse over all the buttons, the bar sometimes stays.
https://gyazo.com/895f9844329d794c111d04d5e0218566
The code which handles the creation and tweening,
local options = script.Parent.Options for i,v in pairs(options:GetChildren()) do if v.Selectable == true and v:IsA("TextButton") then v.MouseEnter:Connect(function() print(v.Name) frame = Instance.new("Frame") frame.Visible = false frame.Parent = options frame.Size = UDim2.new(0, 0,0.015, 0) frame.BackgroundColor3 = Color3.fromRGB(255,255,255) frame.BorderSizePixel = 0 frame.Position = UDim2.new(0.191,0,v.Position.Y.Scale + 0.13,0) frame.Visible = true frame:TweenSize(UDim2.new(0.645,0,0.015, 0),"Out","Linear",0.3) end) v.MouseLeave:Connect(function() frame:Destroy() end) end end
How can I add to my code to make sure that this doesn't occur? Thanks.
local options = script.Parent.Options for i,v in pairs(options:GetChildren()) do if v.Selectable == true and v:IsA("TextButton") then v.MouseEnter:Connect(function() if options:FindFirstChild("Frame") Then options.Frame:Destroy() -- This Should Work print(v.Name) frame = Instance.new("Frame") frame.Visible = false frame.Parent = options frame.Size = UDim2.new(0, 0,0.015, 0) frame.BackgroundColor3 = Color3.fromRGB(255,255,255) frame.BorderSizePixel = 0 frame.Position = UDim2.new(0.191,0,v.Position.Y.Scale + 0.13,0) frame.Visible = true frame:TweenSize(UDim2.new(0.645,0,0.015, 0),"Out","Linear",0.3) end) v.MouseLeave:Connect(function() frame:Destroy() end) end end
When you hover over a new button and there is still a frame it will delete it otherwise that line of code is ignored.