Repeat is not firing when variable meets proper requirements

I want to make a dialogue system where animated text will play (like a typewriter, the story game style where its like someone is talking to you) and the player can click to cancel the typewriting effect and it will show the original effect.

My issue is that whenever clickAmount == 3 the repeat variable is not firing.

I’ve tried changing the variable and I couldn’t find any problems like mine on the developer hub.

Here’s my script,

game.Workspace.mainApartment.mainDorm.phonehitbox:Destroy()
local label = script.Parent
local clickAmount = 0
impatientPlayer = false

script.Parent.MouseButton1Click:Connect(function()
	clickAmount = clickAmount + 1
	print(clickAmount)
	local Text = "Hi child! Click the text button to stop the text animation and launch the next one!!"
	textAmount = 0
	repeat
		if clickAmount >= 3 then return end
		textAmount = textAmount + 1
		if impatientPlayer then break end
		impatientPlayer = false
		label.Text = string.sub(Text, 1, textAmount)
		wait(0.01)

		if clickAmount == 2 then
			impatientPlayer = true
			label.Text = Text
		end
	until textAmount == #Text or impatientPlayer
	impatientPlayer = false
	textAmount = 0

	if clickAmount == 3 then
		print(clickAmount)
		Text = "Congratulations! You stopped the text animation, click again to stop this one!"
		repeat
			print(clickAmount)
			textAmount = textAmount + 1
			if impatientPlayer then break end
			impatientPlayer = false
			label.Text = string.sub(Text, 1, textAmount)
			wait(0.01)
			print(impatientPlayer)
			print(clickAmount)

			if clickAmount == 4 then
				impatientPlayer = true
			end
		until textAmount == #Text or impatientPlayer
	end
end)

I think your issue is stemming from the fact that you’re only continuing the code if it is equal to 3.
Instead of doing this:

if clickAmount == 3 then

I recommend doing this:

if clickAmount >= 3 then

If you have any questions or concerns feel free to ask!

I tried that, but it did not work. Thank you for your offer! :blush:

I found a fix. I switched out the position of the code so in the script the second dialogue sentence appears BEFORE the first. Apparently it’s working so I’m going to stick with that, here’s what I did incase my explanation was not clear.


game.Workspace.mainApartment.mainDorm.phonehitbox:Destroy()
local label = script.Parent
local clickAmount = 0
impatientPlayer = false

script.Parent.MouseButton1Click:Connect(function()
	clickAmount = clickAmount + 1
	print(clickAmount)

	if clickAmount == 3 then
		print(clickAmount)
		Text = "Congratulations! You stopped the text animation, click again to stop this one!"
		repeat
			print(clickAmount)
			textAmount = textAmount + 1
			if impatientPlayer then break end
			impatientPlayer = false
			label.Text = string.sub(Text, 1, textAmount)
			wait(0.01)
			print(impatientPlayer)
			print(clickAmount)

			if clickAmount == 4 then
				impatientPlayer = true
				label.Text = Text
			end
		until textAmount == #Text or impatientPlayer
	end
	
	
	local Text = "Hi child! Click the text button to stop the text animation and launch the next one!!"
	textAmount = 0
	repeat
		if clickAmount >= 3 then return end
		textAmount = textAmount + 1
		if impatientPlayer then break end
		impatientPlayer = false
		label.Text = string.sub(Text, 1, textAmount)
		wait(0.01)

		if clickAmount == 2 then
			impatientPlayer = true
			label.Text = Text
		end
	until textAmount == #Text or impatientPlayer
	impatientPlayer = false
	textAmount = 0

	
	
end)

You could’ve just wrapped the first message in a conditional statement too, such as in the following.

game.Workspace.mainApartment.mainDorm.phonehitbox:Destroy()
local label = script.Parent
local clickAmount = 0
impatientPlayer = false

script.Parent.MouseButton1Click:Connect(function()
	clickAmount = clickAmount + 1
	if clickAmount == 1 then
		print(clickAmount)
		local Text = "Hi child! Click the text button to stop the text animation and launch the next one!!"
		textAmount = 0
		repeat
			if clickAmount >= 3 then return end
			textAmount = textAmount + 1
			if impatientPlayer then break end
			impatientPlayer = false
			label.Text = string.sub(Text, 1, textAmount)
			wait(0.01)

			if clickAmount == 2 then
				impatientPlayer = true
				label.Text = Text
			end
		until textAmount == #Text or impatientPlayer
		impatientPlayer = false
		textAmount = 0
	end
	
	if clickAmount == 3 then
		print(clickAmount)
		Text = "Congratulations! You stopped the text animation, click again to stop this one!"
		repeat
			print(clickAmount)
			textAmount = textAmount + 1
			if impatientPlayer then break end
			impatientPlayer = false
			label.Text = string.sub(Text, 1, textAmount)
			wait(0.01)
			print(impatientPlayer)
			print(clickAmount)

			if clickAmount == 4 then
				impatientPlayer = true
			end
		until textAmount == #Text or impatientPlayer
	end
end)