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

Text Debounce doesn't seem to work?

I was making a little thing that talks once clicked, however the debounce to prevent the player from spam click and messing up the text isn't working? It works of course without any errors

StringMessages = {'Hello My Name Is Bob','I am a lovely NPC','What about you?'}
Messages = 0
Sound = script.Parent.Sound

typing = false

function NextMessage()
	Messages = Messages + 1
	if Messages > #StringMessages then
		Messages = 1
		end
	Length = string.len(StringMessages[Messages])
end

script.Parent.ClickDetector.MouseClick:connect(function()
	if not typing then
		typing = true
			NextMessage()
	for i=1,Length do
	script.Parent.NameGUI.Tex.Text = (string.sub(StringMessages[Messages],1,i))
		Sound:Play()
	wait(.25)
		end
	end
		typing = false
end)

1 answer

✓ Accepted answer

You got this structure:

function MouseClick()
	if not typing then
		-- do stuff
		typing = false
	end
	typing = false
end

-- First click: typing is set to false and the for-loop runs
-- Second click: if-statement is skipped, it does the "typing = false"
-- Third click: typing is false, so if-statement works again

Solution: Putting the "typing = false" inside the if-statement:

script.Parent.ClickDetector.MouseClick:connect(function()
	if not typing then
		typing = true
		NextMessage()
		for i=1,Length do
			script.Parent.NameGUI.Tex.Text = (string.sub(StringMessages[Messages],1,i))
			Sound:Play()
			wait(.25)
		end
		typing = false
	end
end)

1 comment

0

Thanks, it was this simple mistake I didn't catch ;)

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