Hello. I am making a script and it does require a lot of debounce so it doesn't run everytime. But I just noticed that im going to have to keep on writing db1 = true db2 = true ... so on. Is there a way to make this more effiecient with a for loop maybe?
local cookies = script.Parent.Parent.CookieGui.Cookies local frame = script.Parent.Frame local folder = script.Parent.AchNotifications local db1 = true cookies.Changed:connect(function() if cookies.Value >= 1 and db1 == false then frame.Ach1.Visible = true folder.Ach1:TweenPosition(UDim2.new(0.41,0,0.725,0),"Out","Quad",2,true,nil) wait(6) folder.Ach1:TweenPosition(UDim2.new(1.5,0,0.725,0),"Out","Quad",2,true,nil) wait(3) folder.Ach1:Destroy() db1 = true end end)
Please don't help me with anything else other than the part I asked you to do. The rest of the script is fine.
You only have to write the debounce once for this:
local cookies = script.Parent.Parent.CookieGui.Cookies local frame = script.Parent.Frame local folder = script.Parent.AchNotifications local db1 = true cookies.Changed:connect(function() if not db1 then return end db1 = false if cookies.Value >= 1 then frame.Ach1.Visible = true folder.Ach1:TweenPosition(UDim2.new(0.41,0,0.725,0),"Out","Quad",2,true,nil) wait(6) folder.Ach1:TweenPosition(UDim2.new(1.5,0,0.725,0),"Out","Quad",2,true,nil) wait(3) folder.Ach1:Destroy() end db1 = true end)
You have two mistakes: You got your debounce wrong! It should initially be declared false, you should check if its false and if so declare it true, and at the end of the code declare it false again.
local cookies = script.Parent.Parent.CookieGui.Cookies local frame = script.Parent.Frame local folder = script.Parent.AchNotifications local debounce = false cookies.Changed:connect(function() if not debounce and cookies.Value >= 1 then debounce = true frame.Ach1.Visible = true folder.Ach1:TweenPosition(UDim2.new(0.41,0,0.725,0),"Out","Quad",2,true,nil) wait(6) folder.Ach1:TweenPosition(UDim2.new(1.5,0,0.725,0),"Out","Quad",2,true,nil) wait(3) folder.Ach1:Destroy() debounce = false end end)
However, if you want the code to only run once, the most efficient way is to disconnect the event:
local cookies = script.Parent.Parent.CookieGui.Cookies local frame = script.Parent.Frame local folder = script.Parent.AchNotifications local c c = cookies.Changed:connect(function() if cookies.Value >= 1 then c:disconnect() frame.Ach1.Visible = true folder.Ach1:TweenPosition(UDim2.new(0.41,0,0.725,0),"Out","Quad",2,true,nil) wait(6) folder.Ach1:TweenPosition(UDim2.new(1.5,0,0.725,0),"Out","Quad",2,true,nil) wait(3) folder.Ach1:Destroy() end end)