Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Making a script more efficient- Help?

Asked by 9 years ago

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.

2 answers

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)

Ad
Log in to vote
0
Answered by 9 years ago

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)
0
The initial state of the debounce is completely irrelevent, actually. adark 5487 — 9y
1
I know, but both he and you used it wrong. You set it to true, and if debounce was false, then it would run the code. The code would never run. aquathorn321 858 — 9y

Answer this question