How to transform if statements to Dictionaries

Hey! I’m making an ‘advanced’ dialogue system. This means that if the text includes a . , ! or ? it will wait a custom amount of seconds before continuing the sentences. I want to switch the if statements to this dictionary:

local Dictionary = {
	["."] = 1;
	["!"] = 1;
	["?"] = 1;
	[","] = 0.5;
}

The current textloop looks like this: (working)

local Comma = ","
local Period = "."
local Exclamation = "!"
local QuestionMark = "?"

	for i = 1, #Text do
		Frame.Dialogue.Text = string.sub(Text, 1, i)
		local Time = 0.1
		if string.sub(Text, i, i) == Comma then
			Time = 0.5
		elseif string.sub(Text, i, i) == Period or 
			string.sub(Text, i, i) == Exclamation or 
			string.sub(Text, i, i) == QuestionMark
		then
			Time = 1
		end
		task.wait(Time)
	end

I’ve tried to make it a dictionary supported by doing thing such as:

for i = 1, #Text do
		local sub = string.sub(Text, 1, i)
		Frame.Dialogue.Text = sub
		local Time = 0.05
		for x, v in pairs(string.split(Text, "")) do
			if Dictionary[v] then
				Time = Dictionary[v]
				warn(Time)
			end
		end
		task.wait(Time)
	end

The issue with this code is that because I do string.split it splits the entire sentence and ofcourse it’ll register multiple periods, commas, etc. such as in the code below.
Type("You are going to die, muhahhaa! Are you ready to die, friend? I will kill you!", "The Jester", Color3.fromRGB(255, 0, 0))

I’ve had no success so far. Does anyone know a solution? Thank you!

local Dictionary = {
	["."] = 1;
	["!"] = 1;
	["?"] = 1;
	[","] = 0.5;
}

for i = 1, #Text do
		Frame.Dialogue.Text = string.sub(Text, 1, i)
		local s = string.match(string.sub(Text, i, i), "%p")
        local exist = Dictionary[s]
           
        Time = if s and exist then Dictionary[s] or .1
		task.wait(Time)
	end

String Patterns

Sadly, this doesn’t work. I did have to modify it a little bit. Do you think you could assist me further?
image

local function Type(Text, User, Color)
	if not Text or not User or not Color then return end
	Frame.User.Text = User
	Frame.User.TextColor3 = Color
	Frame.Dialogue.TextColor3 = Color
	for i = 1, #Text do
		Frame.Dialogue.Text = string.sub(Text, 1, i)
		local s = string.match(string.sub(Text, i, i), "%p")
		local exist = table.find(Dictionary, s)

		local Time
		
		if s and exist then Time = Dictionary[s] or .1 end
		task.wait(Time)
		warn(Time)
		print(s)
		print(exist)
	end
end

You made on mistake. How it should look:

Time = if s and exist then Dictionary[s] else .1

And how you did:

local Time
		
if s and exist then Time = Dictionary[s] or .1 end

Oops, it should be else instead of or.

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