Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I read the last letter of a string?

I want to make a script that plays an audio based on the last letter of a chat message. The point is to make a custom chat box that reads out every individual letter (Like Animal Crossing).

Currently, the chat box will take one letter from what you said in chat at a time like this: (Each line is what the variable is.)

H
He
Hel
Hell
Hello
Hello,
Hello, 
Hello, W
Hello, Wo
Hello, Wor
Hello, Worl
Hello, World
Hello, World!

I want it to find only the last letter, so i can play the audio for that letter. How do I do this?

2 answers

✓ Accepted answer

You can use string.sub(myString, -1) to get the last character from a string.

Example

I suppouse you aren't using a for loop, and writing all these letters out. So i'll teach you for loops.

local string = "Hello, World!" --the string to do the shenanigans
local lastletter --the last letter that will be set by the for loop
local textlabel = Instance.new("TextLabel") -- a placeholder textlabel
for i = 1,#string do
	if i == #string then
		lastletter = string:sub(i,i) --if variable i (times went thru the code) is how many characters there are in the string, set last letter
	end
	textlabel.Text = string:sub(1,i) --sets text to the placeholder textlabel
	wait(0.075) --waits so the script doesnt have a game script timeout and uh, works?
end
print(lastletter)

So thats basically how you use it for strings. I hope it's easy to understand.

This discussion is preserved as it was. New answers, comments, and votes are closed.