1
2
3
4
5
6
7
local split = {}
local i = 0
for w in txt:gmatch("!?%w+") do
if (i > 2) then break end
split[i] = w
i = i + 1
end
This splits every word the message the player writes has.
txt:gmatch("!?%w+")
returns an iterator which contains each word the original string
txt
has. The weird string
"!?%w+"
is a regex. It means that it matches anything that starts with one or zero "!" (
"!?"
) followed with at least (
"+"
) one or more word characters (
"%w"
). It's tricky but you don't need to memorize it. So every word that matches the regex is returned in the iterator I mentioned before.
Now the for loop goes through every word and stores each word in the array
split
(which is actually a table). So then you can access each word with
split[0]
,
split[1]
, etc (in order).
1
2
3
4
5
6
7
8
9
10
elseif (split[0] == "!givePoints") then
local id = tonumber(split[1])
local amount = tonumber(split[2])
if (id ~= nil and amount ~= nil) then
ms.money[id] = ms.money[id] + amount
ms.moneyshow(id)
--else
-- showError("Wrong arguments (id or amount are not numbers)")
end
end
This checks if the first word (
split[0]
) is equal to "!givePoints" (what you asked for). If so, then it verifies that
id
and
amount
are numbers with
if (id ~= nil and amount ~= nil) then
. If both are numbers then it adds the amount to the player's money. If at least one is not a number then it does nothing, or you can alternatively show an error message as the comments show. Note that it won't work if you just uncomment the comments because the function showError() may not exist in your script. It's just a placeholder.