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

Getting certain args in .Chatted + strings?

So I am detecting when a player chats with in a local script.

Player.Chatted:connect(function(Message)

end)

but how can I pick out certain things of that message and define them as a variable? I've tried things like:

Player.Chatted:connect(function(Message)
	if Message == "Hello " then
		--code
end)

but how do I get what's after "Hello ", so if the player says "Hello World" I can continue, but if they say "Hello foo" something else will happen, but mainly defining the 'extra' (world, bananas) as its own variable so like;

Args = string.sub() --?

Not that good with string. - any help would be appreciated.

1 answer

✓ Accepted answer

Match it
If the pattern fits.

If you want to start getting into more variadic stuff, you want to look at string pattern matching. In your case, you should be good to just collect anything that isn't whitespace into a table:

local t = {};
spoken:gsub('%S+', function(c) t[#t+1] = c end);

What this does is:

  • Creates a new table t
  • For everything which matches %S+ do:
    • Add the capture to table t

The pattern %S+ is formed of:

  • %S which matches anything which is not a whitespace (%s matches anything which is a whitespace)
  • + matches 1 or more occurrences of the preceding pattern.

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