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.)
01 | H |
02 | He |
03 | Hel |
04 | Hell |
05 | Hello |
06 | Hello, |
07 | Hello, |
08 | Hello, W |
09 | Hello, Wo |
10 | Hello, Wor |
11 | Hello, Worl |
12 | Hello, World |
13 | 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?
You can use string.sub(myString, -1)
to get the last character from a string.
I suppouse you aren't using a for loop, and writing all these letters out. So i'll teach you for loops.
01 | local string = "Hello, World!" --the string to do the shenanigans |
02 | local lastletter --the last letter that will be set by the for loop |
03 | local textlabel = Instance.new( "TextLabel" ) -- a placeholder textlabel |
04 | for 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? |
10 | end |
11 | print (lastletter) |
So thats basically how you use it for strings. I hope it's easy to understand.