Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Getting certain args in .Chatted + strings?

Asked by
iFlusters 355 Moderation Voter
8 years ago

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

Log in to vote
2
Answered by 8 years ago

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.
Ad

Answer this question