I don’t know much about string patterns and I’m having trouble understanding the roblox API.
Could someone explain to me how I could extract a string that could change from a sentence?
For example: A script prints a player’s name and current position. I want to extract a player’s name and print it regardless of whether or not the name changes
I don’t know what you mean by “from a sentence”, but couldn’t you just get the players name each time you want to print it? local name = Player.Name print(name)
It shouldn’t matter if the name changes
Just used sentence because I don’t know what to call the print’s results. I could get the player’s name using other methods but the method I want to use is using string patterns so that I can understand how they work
local sentence = "hello its me star!"
local wordToFind = "star"
if string.find(sentence,wordToFind) then
print("found word")
else
print("word not found")
end
use function : string.sub()
with first parameter as where string you want to extract starts and 2nd parameter where it ends so : string.sub(1, 3) : (“HelloWorld”) => (“Hel”)
This is useful but in my example if I wanted to extract a player’s name and they change their name, I would then also have to change the targeted word to find, is there another way for me to extract the name regardless of whether or not they change it? Or better yet, is there a way for me to extract a word based on it’s position in a sentence?
local str = "hello i am Darobbap"
local newStr = string.gsub(str, "hello i am ", "") --The 3rd argument is the string you want to replace the 2nd arg with btw
print(newStr) --It should print "Darobbap"