Type-Writer Effect Module

Hello, I got bored today and decided to make a quick Type-Writer module. It’s very straightforward to use and has great features that make it good to use!

How to use:
local script:

local Module = require(module.Path)

local TextLabel = --diction to textLabel
local Text_String = "This is a text string, I am used for tests. :D"

local module_Propertys = { --all values that can be changed at the moment.
    _typeWriter_Wait = .05, --Average wait time for any other characters
	_commaWait = .15, --Wait time when there is a comma
	_puncuationWait = .75, --Wait time when there are punctuations
	_soundId = "rbxassetid://2541098554", --Add any custom sound for typewriting
	_volume = .1 --Volume for the sound

     }

Module.TypeWrite(Text_String, TextLabel, module_Propertys)

Module Script

local debris = game:GetService("Debris")

local module = {}

function module.TypeWrite(Text: string, TextLabel: TextLabel, propertys)
	setmetatable(propertys, {
		
		__index = { --adds default properties if table left empty
			
			_typeWriter_Wait = .05,
			_commaWait = .15,
			_puncuationWait = .75,
			_soundId = "rbxassetid://2541098554",
			_volume = .1
		}
	})
	local waitTime = propertys._typeWriter_Wait
	local sound = script:WaitForChild("cache"):WaitForChild("sound")
	sound.Volume = propertys._volume
	sound.SoundId = propertys._soundId
	
	for i = 1, #Text do
		print(string.sub(Text, i, i))
		local soundClone = sound:Clone()
		if string.sub(Text, i, i) == "." or string.sub(Text, i, i) == "?" or string.sub(Text, i, i) == "!" then
			waitTime = propertys._puncuationWait
			
		elseif string.sub(Text, i, i) == "," then
			waitTime = propertys._puncuationWait
		elseif string.sub(Text, i, i) ~= "," or string.sub(Text, i, i) ~= "!" or string.sub(Text, i, i) ~= "?" or string.sub(Text, i, i) ~= "." then
			waitTime = propertys._typeWriter_Wait
		end
		soundClone.Parent = TextLabel
		soundClone:Play()
		debris:AddItem(soundClone, .25)
		TextLabel.Text = string.sub(Text, 1, i)
		print(string.sub(Text, 1, i))
		task.wait(waitTime)
	end
end

return module

TypeWriter.rbxm (2.2 KB)

Of course, I’m too tired to explain everything in detail so for right now this will have to do. Please submit feedback so I can make this better and more efficient

6 Likes

Would it not be better to include a video?

When I have the time I’ll include a video