How would I pull words out of this string?

string is as follows “samjay22 on Obby”

Is there a way to pull samjay22 and Obby out?

I have attempted to loop through the player list to check if that player exists, however the problem with that is that if the player is not in the game it errors.

Please note the method would need to work with other names for example:

timmy_123xx3 on Obby_3
in the listed above I would need to get timmy_123xx3 and Obby_3 out of the string.

I tried to look on string documentation however failed to find anything on this topic.

Any advice?

If all your strings follow the pattern “Player on Obby”, you can use string:split() to get what you need.

local str = "samjay22 on Obby"
local strTbl = str:split(" on ")

local player = strTbl[1] -- "samjay22"
local obby = strTbl[2]   -- "Obby"

Thank you! It seemed to work. :smiley:

There’s no reason to use string.split there. string.split is useful when there are multiple occurrences. You can use a simple string pattern here: local name, obby = string.match("samjay22 on Obby", "^(%S+) on (%S+)$"). This is more general/flexible, too; you could easily change it to not only accept “on” as the second word (by changing “on” to “%S+”).

5 Likes