I’m trying to make a ban command using message:sub(1, ?), but how would I know what the second parameter is, because the players name could be any length, thanks.
local text_name = "length"
local length = #text_name
print("The length is:",length, ". The half is:", String.sub(text_name, 1, length / 2))
--output
The length is: 6. The half is: len
Thank you but I’m trying to find the second parameter, because you don’t know what the length of the players name is.
you should use string.split(string, " ")
that will separate the string between every space which will make parsing much easier
here’s a quick example
local args = string.split(",ban everyone", " ")
args[1] – “,ban”
args[2] – “everyone”
hopefully that should make it easier
Unfortunately, split
is not the way to go for chat commands, as this method isn’t very flexible and has a lot of downsides.
Few of them are mainly:
It can’t handle text:
string.split("message Hello World", " "); -- since this command has a space in where the message should be, it will split the message into 2 values instead of one
It can’t handle multiple spaces:
local a = string.split("kick all", " "); -- notice the double space
a[1]; -- "kick"
a[2]; -- ""
a[3]; -- "all"
-- everything breaks
The best and most efficient solution is to use string patterns, or to write your own system to handle all that.
If you wish to use string patterns, then for each command take an “arg” parameter which is the text after the initial command name (“kick all” → “kick”: command name, “all”: arg), and then use string patterns to retrieve certain values.
Use match
and parentheses (in string pattern, lets you save a match into a variable) in order to do that.
Example:
-- usage (chat): `:teleport player1 player2`
local function teleport(arg)
local player1, player2 = arg:match("^(%w+)%s-(%w+)$"); -- since we have 2 sets of parentheses in our pattern, we expect to have 2 values returned
if player1 and player2 then
-- you now have player1's and player2's name
-- ...
else
error("Invalid command arguments.");
end
end
In this example, no matter how many whitespaces there are in the teleport command the command still executes successfully (if all the other values are correct of course), and it’s quite flexible as well if you know the patterns.
Thanks for the respons, would you mind explaining why you used such random characters?
Hello.
Those “random characters” are string patterns. As explained above, ()
(parentheses) indicate that we want to save whatever is in them into a variable.
Some of the patterns that I used in my previous post:
-
^
matches everything at the beginning of string,$
matches everything at the end of string, and^...$
matches the whole string. -
%w
matches all alphanumeric characters (letters and numbers). I know that usernames can also have underscores, but you can add them yourself with sets[]
:[%w_]+
. This will match both alphanumeric characters and underscores. -
%s
matches all whitespaces. -
+
matches 1 or more of the specified character class (in our case%w
). -
-
matches as few of the specified character classes as possible (in our case%s
, we don’t need to match any whitespaces between the arguments).
I recommend reading this article to get to know string patterns better, as they are very interesting and useful. You can also find a list of all magic characters and patterns there.