New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

How do I read the last letter of a string?

Asked by 9 days 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.)

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

Log in to vote
Answered by 9 days ago
Edited 9 days ago

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


Log in to vote
Answered by
RAFA1608 195
9 days ago

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 ="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
    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?

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

Answer this question