I've made a module for my library that will help me script transparency fade animations, and this is what I got:

------------------
--[DECLARATIONS]--
------------------

----/[SERVICES]\----
local runService = game:GetService("RunService")

----/[MAIN]\----
local guiModule = {}

---------------
--[FUNCTIONS]--
---------------

--Fades the gui's transparency.
--gui: the gui that we will be faded.
--t: the amount of times the transparency will change (so this can be the "time")
---it will take to fade.
--BGTrans: The target BackgroundTransparency.
--TTrans: The target TextTransparency.
--TSTrans: The target TextStrokeTransparency.
function guiModule:FadeGuiTransparency(gui, t, BGTrans, TTrans, TSTrans)
	--Save the current transparencies of the gui contents
	local oldBGTrans = gui.BackgroundTransparency
	local oldTextTrans = gui.TextTransparency
	local oldTextSTrans = gui.TextStrokeTransparency
	
	--Fade the Transparency of the gui
	for i = 1, t do
		--Fade the background
		gui.BackgroundTransparency = oldBGTrans + (BGTrans-oldBGTrans)/t*i
		
		--Fade the text
		gui.TextTransparency = oldTextTrans + (TTrans-oldTextTrans)/t*i
		gui.TextStrokeTransparency = oldTextSTrans + (TSTrans-oldTextSTrans)/t*i
		
		--Delay
		runService.RenderStepped:wait()
	end
end

------------------
--[FINALIZATION]--
------------------
return guiModule


It works, but there's a problem. If I call the method for guiA, and call this method again for guiA when the previous method is still running, the gui might "Flash" and have a weird transition (though the transparency doesn't seem to go off), like in this example:

local repStore = game:GetService("ReplicatedStorage")
local guiMod = require(repStore.lib.GuiModule)
local button = script.Parent

												  
spawn(function() guiMod:FadeGuiTransparency(button, 1000, 0, 0, 1) end)
wait()
spawn(function() guiMod:FadeGuiTransparency(button, 1000, 1, 1, 1) end)

So, how would I interrupt an instance of FadeGuiTransparency from running when this same instance is called for the same "gui"?

1 comment

0

Debounce.