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

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

Log in to vote
0
Answered by 3 years ago
Edited 3 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
3 years 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 = 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.

Answer this question