Optimization For Script

Hi! I made a text generator thingy and there’s this one part that I think can look way better.
Here’s that one part:

	for i = 1, #text, 1 do
		if name == "MAT" then
			matsfx:Play()
		elseif name == "Draggo - Co-Owner" then
			draggosfx:Play()
		elseif name == "Angel" then
			angelsfx:Play()
		elseif name == "BLENDER" then
			blendersfx:Play()
		end
		textlabel.Text = string.sub(text,1,i)
		task.wait(waittime)
	end

How would I optimize this better?
Idk if this topic goes into this category but uhh ye

Honestly that’s fine. For a cleaner and more scalable version you can look at using dictionaries to hold the thing you are calling :Play() on under the key of the name you are comparing it to. But this won’t speed up your code, just make it easier to add more things. (With enough things this would be faster)

If you want this to look a bit better, I would recommend putting the sounds in a dictionary instead of an if chain. I would also recommend changing the MaxVisibleGraphemes rather than doing string.sub

e.g.

local Audio = {
	["MAT"] = matsfx,
	["Draggo - Co-Owner"] = draggosfx,
	["Angel"] = angelsfx,
	["BLENDER"] = blendersfx
}
textLabel.MaxVisibleGraphemes = 0
textLabel.Text = text

for i = 1, #text do
	textLabel.MaxVisibleGraphemes = i
	Audio[name]:Play()
	task.wait(waittime)
end
1 Like