Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I read the last letter of a string?

Asked by 4 years ago

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.)

01H
02He
03Hel
04Hell
05Hello
06Hello,
07Hello,
08Hello, W
09Hello, Wo
10Hello, Wor
11Hello, Worl
12Hello, World
13Hello, 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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Example

Ad
Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
4 years ago

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

01local string = "Hello, World!" --the string to do the shenanigans
02local lastletter --the last letter that will be set by the for loop
03local textlabel = Instance.new("TextLabel") -- a placeholder textlabel
04for i = 1,#string do
05    if i == #string then
06        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
07    end
08    textlabel.Text = string:sub(1,i) --sets text to the placeholder textlabel
09    wait(0.075) --waits so the script doesnt have a game script timeout and uh, works?
10end
11print(lastletter)

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

Answer this question