Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

separating words in strings?

I was wondering how I could separate words that are chatted, so that if I chat "give me all the money" and have it print results I would get -give -me -all -the -money

[EDIT] I figured ^^^ that out. but now I need to know how to access specific words.

game.Players.PlayerAdded:connect(function(Player)
Player.Chatted:connect(function(Message)
	for i in string.gmatch(Message, "%S+") do
    print(i)
end
end)
end)

2 comments

0

Elaborate 'access specific words'.

0

Lets say I said some thing with 5 words, and I wanted it to print the 2nd word I said...

1 answer

✓ Accepted answer

I'm answering according to this statement: "Lets say I said some thing with 5 words, and I wanted it to print the 2nd word I said..."

You CAN use some fancy string pattern for this, or if you're lazy like me then just predefine a number variable, then increment the variable for each string match with your generic for loop. Check if the number is the specified number.. then get it!(:

local wordWanted = 2 --The second word
local num = 0
local str = 'Hello, I really like to eat pie'
local word --Make an empty variable to set the word to when you find it

--%w+ is the pattern for any alphanumeric(letter or number)
for i in str:gmatch('%w+') do
	num = num + 1
	if num == wordWanted then
		word = i
	end
end

print(word or 'Did not find the #'..wordWanted..' word') --just for funzies(:

--Would print 'I'

This discussion is preserved as it was. New answers, comments, and votes are closed.