So I made a fade-in / out effect for a button but if you were to spam it, it ends up glitched out like this, at a half faded out transparency.


When I decided to put some print statements in, I found that this problem happens when the fade function runs twice. I did try using a debounce but it didn’t seem to do anything.
The Fade Function:
local function FadeBuildFunc(FadeType)
print("oh no")
if FadeType == "out" then
while BuildToggleButton.TextTransparency < 1 do
wait(0.025)
BuildToggleButton.TextTransparency = BuildToggleButton.TextTransparency + 0.1
end
elseif FadeType == "in" then
while BuildToggleButton.TextTransparency > 0 do
wait(0.025)
BuildToggleButton.TextTransparency = BuildToggleButton.TextTransparency - 0.1
end
end
BuildDebounce = false
end
The Button Function:
BuildToggleButton.MouseButton1Click:Connect(function()
if not BuildDebounce then
BuildDebounce = true
if BuildButtonStatus == "Build" then
FadeBuildFunc("out")
print("RAN-1")
BuildButtonStatus = "Exit"
local GreyFrameColor = Color3.new(0.298039, 0.298039, 0.298039)
HoverFunc(BuildToggleFrame, GreyFrameColor)
local RedTextColor = BuildRedColor
BuildToggleButton.TextColor3 = RedTextColor
BuildToggleButton.Text = "Exit"
FadeBuildFunc("in")
print("RAN-2")
elseif BuildButtonStatus == "Exit" then
FadeBuildFunc("out")
print("RAN-3")
BuildButtonStatus = "Build"
local GreyFrameColor = Color3.new(0.298039, 0.298039, 0.298039)
HoverFunc(BuildToggleFrame, GreyFrameColor)
local BlueTextColor = BuildBlueColor
BuildToggleButton.TextColor3 = BlueTextColor
BuildToggleButton.Text = "Build"
FadeBuildFunc("in")
print("RAN-4")
end
end
end)
Console:

(From the top to the red line is normal clicking speed, everything after the red line is when I began to click faster and it broke)
Edit: I also noticed that sometimes when clicking the “Build” text will show up as red instead of the correct color, and the “Exit” will show up as blue sometimes before changing to it’s correct color.
Any help would be appreciated, also if you need to see it in game the link is here.