Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Prevent a Connect function from running several times?

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?

5 comments

3

Maybe don't wrap it in a while loop

1

Good idea xD

0

^

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.

2 answers

✓ Accepted answer

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

2 comments

0

you don't need a debounce at all

1

@gewehrreben I Know, but its good for somone that added a event on a While Loop

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!

4 comments

0

Why not just remove the loop?

0

^^^^^^^^^^^^^^^^^_^^^^^^^__^^^^^^^^^^__^_^

0

Also why is clicked a global variable

0

I said it might not work -_- now I got like no reputation.

This discussion is preserved as it was. New answers, comments, and votes are closed.