Debounce Help for Toggling Gui

Hello Guys!
I need help with fixing some Debounce on this module script:

--//Services
local TS = game:GetService("TweenService")
local LS = game:GetService("Lighting")

--//Lighting
local GuiBlur = LS.GuiBlur

--//Variables

--Tweening
local OpenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.In)
local CloseInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)

--//Module Functions
local Functions = {}

local CurrentOpenedFrame = {
	MFrame = nil,
	OpenPosition = nil,
	ClosePosition = nil,
	CustomEnterInfo = nil,
	CustomExitInfo = nil,
}

local ToggleStatus = {}
local Debounces = {}

function Functions.OpenMFrame(MFrame, OpenPosition, ClosePosition, CustomEnterInfo, CustomExitInfo)
	if Debounces[MFrame] == nil then
		Debounces[MFrame] = true

		if CustomEnterInfo == nil then
			CustomEnterInfo = OpenInfo
		end
		if CustomExitInfo == nil then
			CustomExitInfo = CloseInfo
		end

		if CurrentOpenedFrame.MFrame ~= nil then
			Functions.CloseMFrame(CurrentOpenedFrame.MFrame, CurrentOpenedFrame.ClosePosition)
		end

		local PositionalTween = TS:Create(MFrame, CustomEnterInfo, {Position = OpenPosition})
		local BlurTween = TS:Create(GuiBlur, CustomEnterInfo, {Size = 56})
		MFrame.Visible = true
		
		local ScreenGui = MFrame:FindFirstAncestorWhichIsA("ScreenGui")
		ScreenGui.DisplayOrder += 1

		PositionalTween:Play()
		BlurTween:Play()
		BlurTween.Completed:Wait()
		CurrentOpenedFrame.MFrame = MFrame
		CurrentOpenedFrame.OpenPosition = OpenPosition
		CurrentOpenedFrame.ClosePosition = ClosePosition
		CurrentOpenedFrame.CustomEnterInfo = CustomEnterInfo
		CurrentOpenedFrame.CustomExitInfo = CustomExitInfo
		Debounces[MFrame] = nil
		ToggleStatus[MFrame] = true
	end
end

function Functions.CloseMFrame(MFrame)
	if Debounces[MFrame] == nil then
		Debounces[MFrame] = true
		local PositionalTween = TS:Create(MFrame, CurrentOpenedFrame.CustomExitInfo, {Position = CurrentOpenedFrame.ClosePosition})
		local BlurTween = TS:Create(GuiBlur, CurrentOpenedFrame.CustomExitInfo, {Size = 0})
		
		local ScreenGui = MFrame:FindFirstAncestorWhichIsA("ScreenGui")
		ScreenGui.DisplayOrder -= 1
		
		PositionalTween:Play()
		BlurTween:Play()
		task.spawn(function()
			BlurTween.Completed:Wait()
			CurrentOpenedFrame.MFrame = nil
			CurrentOpenedFrame.OpenPosition = nil
			CurrentOpenedFrame.ClosePosition = nil
			CurrentOpenedFrame.CustomEnterInfo = nil
			CurrentOpenedFrame.CustomExitInfo = nil
			MFrame.Visible = false
			Debounces[MFrame] = nil
			ToggleStatus[MFrame] = nil
		end)
	end
end

function Functions.ToggleMFrame(MFrame, OpenPosition, ClosePosition, CustomEnterInfo, CustomExitInfo)
	if ToggleStatus[MFrame] == nil then
		Functions.OpenMFrame(MFrame, OpenPosition, ClosePosition, CustomEnterInfo, CustomExitInfo)
		return ToggleStatus[MFrame]
	elseif ToggleStatus[MFrame] == true then
		Functions.CloseMFrame(MFrame)
		return ToggleStatus[MFrame]
	end
end

return Functions

This Module script handles toggling the Main Frames of each gui. The Debounces Table handles actual gui debounces. The ToggleStatus Table handles if to open or close.

Now I was trying to see if this can sustain very fast switching between Guis, and sadly it broke with this error:
image
And it breaks it permanently and pops 2 Guis:


Here is a video if you want more details of the error:

External Media

Sadly idk what is happening here. How can I fix this?
Thanks for stopping by! :smile:

1 Like

Firstly,are u firing the player client()?

Secondly,although debounce might work

Here is the debounce script

Local de = falsr

If de == false theb
De = true
–your code
Task.wait(0.5)
De = false

1 Like

This this the line that is creating the error?
local PositionalTween = TS:Create(MFrame, CurrentOpenedFrame.CustomExitInfo, {Position = CurrentOpenedFrame.ClosePosition})
The error suggests that CurrentOpenedFrame.CustomExitInfo is nil.

2 Likes

idk what you mean by the player client…
The main outlining function is the ToggleFrame(), and its called by local scripts and handlers in StarterGui. For example here is how I call the functions from another Handler:

local function ToggleGui()
	local Status = CommonUtilities.ToggleMFrame(MFrame, QueueFrameOpenPos, QueueFrameClosePos)
	if Status == true then
		--Callbacks
	end
end

Now since I don’t want any custom TweenInfo, I leave it be when I call the function. But in the module, it already replaces the nil TweenInfo for the defaulted one when calling the OpenMFrame():

if CustomEnterInfo == nil then
			CustomEnterInfo = OpenInfo
		end
		if CustomExitInfo == nil then
			CustomExitInfo = CloseInfo
		end

When you close the MFrame, it uses the CurrentOpenedFrame Table and grabs the ExitInfo out of there before setting it to nil again below.

Yes Correct.
Idk why its nil, I mean maybe it has something to do with Debounce?

EDIT:
I’m going to remove the task.spawn() and see if that even works…
image
Edit2: It doesn’t work.

I did some print statements before closing and after closing, here is what I got:
image
The last print statement before the error says that before closing, the CurrentOpenedFrame table is nil for some reason, even though there has to be data for the gui before clearing it again.
I hope this helps :pray:

It could be that BlurTween.Completed:Wait() is waiting too long, and setting CurrentOpenedFrame.CustomExitInfo to nil when it’s not supposed to. Try removing the wait line and see if that works.

Ok ill try
(30 ccccchhhhhhhhaaars)

I tried removing it and yes it did work.
Thanks for letting me know, I will mark this as a solution for now…

Also can you explain why that really happens?
Updated Module:

--//Services
local TS = game:GetService("TweenService")
local LS = game:GetService("Lighting")

--//Lighting
local GuiBlur = LS.GuiBlur

--//Variables

--Tweening
local OpenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.In)
local CloseInfo = TweenInfo.new(0.25, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)

--//Module Functions
local Functions = {}

local CurrentOpenedFrame = {}

local ToggleStatus = {}
local Debounces = {}

function Functions.OpenMFrame(MFrame, OpenPosition, ClosePosition, CustomEnterInfo, CustomExitInfo)
	if Debounces[MFrame] == nil then
		Debounces[MFrame] = true

		if CustomEnterInfo == nil then
			CustomEnterInfo = OpenInfo
		end
		if CustomExitInfo == nil then
			CustomExitInfo = CloseInfo
		end

		if CurrentOpenedFrame.MFrame ~= nil then
			Functions.CloseMFrame(CurrentOpenedFrame.MFrame, CurrentOpenedFrame.ClosePosition)
		end

		local PositionalTween = TS:Create(MFrame, CustomEnterInfo, {Position = OpenPosition})
		local BlurTween = TS:Create(GuiBlur, CustomEnterInfo, {Size = 56})
		MFrame.Visible = true

		local ScreenGui = MFrame:FindFirstAncestorWhichIsA("ScreenGui")
		ScreenGui.DisplayOrder += 1

		PositionalTween:Play()
		BlurTween:Play()
		CurrentOpenedFrame.MFrame = MFrame
		CurrentOpenedFrame.OpenPosition = OpenPosition
		CurrentOpenedFrame.ClosePosition = ClosePosition
		CurrentOpenedFrame.CustomEnterInfo = CustomEnterInfo
		CurrentOpenedFrame.CustomExitInfo = CustomExitInfo
		Debounces[MFrame] = nil
		ToggleStatus[MFrame] = true
	end
end

function Functions.CloseMFrame(MFrame)
	if Debounces[MFrame] == nil then
		Debounces[MFrame] = true
		print("Before Close: ", CurrentOpenedFrame)
		local PositionalTween = TS:Create(MFrame, CurrentOpenedFrame.CustomExitInfo, {Position = CurrentOpenedFrame.ClosePosition})
		local BlurTween = TS:Create(GuiBlur, CurrentOpenedFrame.CustomExitInfo, {Size = 0})

		local ScreenGui = MFrame:FindFirstAncestorWhichIsA("ScreenGui")
		ScreenGui.DisplayOrder -= 1

		PositionalTween:Play()
		BlurTween:Play()
		for i, v in pairs(CurrentOpenedFrame) do
			CurrentOpenedFrame[i] = nil
		end
		MFrame.Visible = false
		Debounces[MFrame] = nil
		ToggleStatus[MFrame] = nil
		print("After Close: ", CurrentOpenedFrame)
	end
end

function Functions.ToggleMFrame(MFrame, OpenPosition, ClosePosition, CustomEnterInfo, CustomExitInfo)
	if ToggleStatus[MFrame] == nil then
		Functions.OpenMFrame(MFrame, OpenPosition, ClosePosition, CustomEnterInfo, CustomExitInfo)
		return ToggleStatus[MFrame]
	elseif ToggleStatus[MFrame] == true then
		Functions.CloseMFrame(MFrame)
		return ToggleStatus[MFrame]
	end
end

return Functions

When the function is fired once, it plays the blur tween and waits for it to finish before setting the data to nil. If it’s fired twice, the first wait isn’t stopped, causing all the data to be set to nil in the middle of the next tween.

1 Like

ah ok thanks man for the help!

also thanks to @ogether123

1 Like

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