admins = {"HydroDivinity"} game.Players.PlayerAdded:connect(function(pl) for i,v in pairs(admins) do if v == pl.Name then pl.Chatted:connect(function(mes) if mes:sub(1,4) == "add " then print(pl.Name.." is running the "..mes:sub(1,3).." command") if mes:sub(5,7) == "c " then print(pl.Name.." is changing somebody's coin-value") end end end) end end end)
Alright, so basically, I have a gui in-game for currency. I want admins to be able to change people's currency depending on what they write on the cmd.
For example;
add c HydroDivinity 6
Not sure how to make "HydroDivinity" come before the 6 value
There are a few ways to do this, but I think the best way would be to use string patterns.
I will use the pattern (%w+) (%d+)%s*$
. This translates to:\
look for the word add and a space
look for multiple alphanumeric characters (A-z, 0-9) at least one and save it (parenthesis make it a capture group)
look for multiple digits at least one and save it
look for any whitespace, okay if I don't find any
end of string $
Let's try this out (you can do so online with the lua demo site here) with some code.
str = "add kools 10" name, amount = str:match("(%w+) (%d+)$") print(name) print(amount)...
kools
10
Cool, let's apply this to your code.
pl.Chatted:connect(function(mes) if mes:sub(1,4) == "add" then name, value = mes:match("(%w+) (%d+)$") -- If there is no match, this will be nil. if name and value then print(pl.Name.." is changing "..name.."'s coin value by ".. value) end end end)
You can of course adapt these patterns to work better for yourself, and use them when determining change as well because it has the same form.