Checking the Length of a String

I’m trying to make a script that changes the text in a TextLabel, then waits for half of the text’s length. I keep getting an error message that says “attempt to perform arithmetic (div) on nil and number.” I’ve tried to convert the .len into a number but for some reason it still says this. The script is a normal script in a TextLabel.

text = script.Parent
lines = {"text", "more text", "hi"}

function escape()
	for i = 1, #lines do
		text.Text = lines[i]
		
		local length = tonumber(tostring(text.Text).len)
		wait(length / 2) --the problem is on this line
	end
end

escape()

If anyone could help me figure out why it doesn’t treat the length as a number that would be great!

3 Likes

The length operator (#) can also be used on strings! There’s no ‘len’ property on strings - you were possibly thinking of the len method:

text.Text:len();
--// Or
string.len(text.Text);
15 Likes

Thanks! I just realized I’ve been using .len incorrectly so I’ll use that instead.

2 Likes