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 6 years ago
1wait ()
2while true do
3    wait ()
4    script.Parent.InputBox.InputEnded:Connect(function ()
5        print (script.Parent.InputBox.Text)
6        script.Parent.InputBox.Text = ""
7    end)
8end

How whould you prevent this script from running 200 times?

3
Maybe don't wrap it in a while loop User#19524 175 — 6y
1
Good idea xD MageMasterHD 261 — 6y
0
^ User#21908 42 — 6y
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 — 6y

2 answers

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

Hello, MageMasterHD!

To make that, do the following:

1 - Remove the While Loop

2 - Add debounce

Edited Script

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

Good Luck with your games

0
you don't need a debounce at all User#22604 1 — 6y
1
@gewehrreben I Know, but its good for somone that added a event on a While Loop Leamir 3138 — 6y
Ad
Log in to vote
0
Answered by
iladoga 129
6 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.

1wait ()
2while true do
3    wait ()
4    clicked =  script.Parent.InputBox.InputEnded:Connect(function (
5        print (script.Parent.InputBox.Text)
6        script.Parent.InputBox.Text = ""
7    clicked:disconnect()
8    end)
9end

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 — 6y
0
^_^^^^^^^_^_^_^_^_^__^_^^_^_^__^_^^^^^____^_^_^_^__^_^_^__^_^_^__^_^ SummerEquinox 643 — 6y
0
Also why is clicked a global variable User#19524 175 — 6y
0
I said it might not work -_- now I got like no reputation. iladoga 129 — 6y

Answer this question