Finding a character in a string via a number?

So I’ve been working on a dialog system in which i’m trying to identify the newest character to be added via a typewriter effect. And I’ve been wondering how exactly can I find a character inside a string via a number? I’ve been googling a lot about this and I have tried to look for any other string functions that are what i’m looking for. So is this possible and if so how?

You can use string.sub(str, n, m) in a for loop to generate the typewriter effect. Only m is needed to be incremental while n is a constant of 1. Information about string.sub() is found here as well.

https://www.lua.org/manual/5.1/manual.html

1 Like

As said @Operatik,
You can use

if string.sub(msg,0,6) == "!test" then
-- Do something
end

I just did if 5 characters are equals to !test

1 Like

Typewriter.rbxl (22.4 KB)

Script
local function Typewrite(msg, obj, duration, func)
	if msg and obj and duration then
		if string.len(msg) <= 1 then
			error("|| (Arg1) -> Msg too short. ||")
			return
		end
		local x, y = pcall(function()
			local z = obj.Text
		end)
		if not x then error("|| (Arg2) -> Invalid Object. ||") return end
		if duration <= 0 then
			error("|| (Arg3) -> Duration too short. ||")
			return
		end
	else
		if not msg then error("|| Missing Argument #1 -> 'msg' ||") end
		if not obj then error("|| Missing Argument #2 -> 'obj' ||") end
		if not duration then error("|| Missing Argument #3 -> 'duration' ||") end
		return
	end
	obj.Text = ""
	for i = 1,string.len(msg) do
		wait(duration/string.len(msg))
		obj.Text = msg:sub(1,i)
		if func then spawn(function() func() end) end
	end
	return true
end


local function Click()
	local X = Instance.new("Sound", script.Parent)
	X.SoundId = "rbxassetid://421058925"
	X.Volume = 1
	X:Play()
	game:GetService("Debris"):AddItem(X, X.TimeLength + .125)
end


local dbg = true
script.Parent.MouseButton1Click:Connect(function()
	if dbg then
		dbg = false
		Typewrite("This is a test message.", script.Parent, 3, Click)
		dbg = true
	end
end)
1 Like