How would I go about detecting a punctuation in a typewriter dialogue script?

Just wondering how I would detect a punctuation, so I can increase the delay. Things like ? ! , . etc.

local Time = 0.05

	script.Parent.MouseClick:Connect(function()
		if not OnCooldown then
			OnCooldown = true

			Talking = true
			GUI.Enabled = true
			Name.Text = script.Parent.Parent.Name
			Dialogue.Text = "Test, Testing. Test"

			while Talking do
				Dialogue.MaxVisibleGraphemes += 1

				if Dialogue.MaxVisibleGraphemes == #Dialogue.Text then
					Talking = false

					task.wait(1)

					OnCooldown = false
					GUI.Enabled = false
					Dialogue.MaxVisibleGraphemes = 0
					
				else
					task.wait(Time)

				end
			end
		end
	end)

You can use string.sub to extract the current letter and check a dictionary to get the wait time

local CharacterDelay = {Default = 0.05, ["?"] = 1, [","] = 1, ["."] = 1, ["!"] = 1}

script.Parent.MouseClick:Connect(function()
	if not OnCooldown then
		OnCooldown = true

		Talking = true
		GUI.Enabled = true
		Name.Text = script.Parent.Parent.Name
		Dialogue.Text = "Test, Testing. Test"

		while Talking do
			Dialogue.MaxVisibleGraphemes += 1

			if Dialogue.MaxVisibleGraphemes == #Dialogue.Text then
				Talking = false

				task.wait(1)

				OnCooldown = false
				GUI.Enabled = false
				Dialogue.MaxVisibleGraphemes = 0

			else
				task.wait(CharacterDelay[string.sub(Dialogue.Text, Dialogue.MaxVisibleGraphemes, Dialogue.MaxVisibleGraphemes)] or CharacterDelay.Default)
			end
		end
	end
end)

Default will be used when there are no matches in the dictionary

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.