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

Prevent a Connect function from running several times?

Asked by 5 years ago
wait ()
while true do
    wait ()
    script.Parent.InputBox.InputEnded:Connect(function ()
        print (script.Parent.InputBox.Text)
        script.Parent.InputBox.Text = ""
    end)
end

How whould you prevent this script from running 200 times?

3
Maybe don't wrap it in a while loop User#19524 175 — 5y
1
Good idea xD MageMasterHD 261 — 5y
0
^ User#21908 42 — 5y
0
Once you figure out the loop part of the issue, if you still don't want the function to be called as often look into debouncing. User#21908 42 — 5y

2 answers

Log in to vote
3
Answered by
Leamir 3138 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Hello, MageMasterHD!

To make that, do the following:

1 - Remove the While Loop

2 - Add debounce

Edited Script

wait ()
local deb = false --Debounce variable
script.Parent.InputBox.InputEnded:Connect(function () --This is a event, no loops needed =D
    if (not deb) then --Test for debounce(prevent running several time)
        deb = true --sets debounce to true
        print (script.Parent.InputBox.Text)
        script.Parent.InputBox.Text = ""
        wait(5) --Debounce Time
        deb = false --sets debounce to false
    end
end)

Good Luck with your games

0
you don't need a debounce at all User#22604 1 — 5y
1
@gewehrreben I Know, but its good for somone that added a event on a While Loop Leamir 3138 — 5y
Ad
Log in to vote
0
Answered by
iladoga 129
5 years ago

I do believe there is a way to disconnect functions. I do not know much about this but I do think it works. Correct me if I am wrong.

    wait ()
    while true do
        wait ()
        clicked =  script.Parent.InputBox.InputEnded:Connect(function (
            print (script.Parent.InputBox.Text)
            script.Parent.InputBox.Text = ""
        clicked:disconnect()
        end)
    end

Try it and it might work but I am not 0 sure I posted this answer so that if it did work then it would help you.

Hope it works!

0
Why not just remove the loop? Leamir 3138 — 5y
0
^_^^^^^^^_^_^_^_^_^__^_^^_^_^__^_^^^^^____^_^_^_^__^_^_^__^_^_^__^_^ SummerEquinox 643 — 5y
0
Also why is clicked a global variable User#19524 175 — 5y
0
I said it might not work -_- now I got like no reputation. iladoga 129 — 5y

Answer this question