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

How do I avoid a recursion?

I want players to only be able to type numbers into a textbox. The function runs when Textbox.Changed is triggered, and instantly deletes any letters it detects. The problem is that deleting any letter input re-triggers the Textbox.Changed property(Line 4), which then leads to a recursion. Is there any alternatives to this code?

Textbox.Changed:Connect(function(input)
	if input == "Text" then
		if not tonumber(Textbox.Text) then
			Textbox.Text = Textbox.Text:sub(1,#Textbox.Text-1)
		end
	end
end)

1 answer

✓ Accepted answer

This should do the trick:

local canRun = true
Textbox.Changed:Connect(function(input)
	if input == "Text" then
		if not tonumber(Textbox.Text) and canRun then
			Textbox.Text = Textbox.Text:sub(1,#Textbox.Text-1)
			canRun=false
		else
			canRun=true
		end
	end
end)

Basically, if the input is equal to "Text" and the canRun variable is equal to true, it'll set canRun to false and delete the letter in the textbox. After running again, the script will "see" that canRun is equal to false, setting it to true and not deleting the letters in the textbox.

2 comments

0

thank u kind sir, have a great day

0

No problem.

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