Why do you need to split the message? The point is to combine the information into a message, if I read your original post correctly. Oh, I see. You’re trying to grab the reason. Take a look at this code:
local function tokenizeString(str)
local tokens = string.split(str, " ")
local prefix = string.sub(tokens[1], 1, 1)
tokens[1] = string.lower(string.sub(tokens[1], 2))
if prefix == config.chatService.prefix then
return tokens
end
return nil
end
This code takes the input string and splits it into a table using the space character. For me, config.chatService.prefix = "/"
. Then it removes the first character from the string at index 1. If it’s a command, then this will be the prefix. If the prefix is the command prefix, then it returns the tokenized string as a table (with the prefix removed from the command) or nil if it’s not.
After that, the following indices apply:
- The command to process. “ban” in your case.
- The name or user id of the player to ban.
- The first word of the reason.
To combine the reason, you use table.concat starting at index 3 to the end of the table. Something like reason = table.concat(tokens, " ", 3)
will reconstruct the reason string. Afterwards, if the player tries to log in, you ban them. The name of the moderator is captured in the process and is displayed according to string.format. If you’re familiar with C at all, C has a function called printf
which uses replaceable parameters %d for decimal number, %s for string, %f for floating point number, %x for hex number, etc… which string.format also uses. Below is an excerpt from my sessionData table and my kickBannedPlayer function again for reference:
sessionData = {
Banned = false;
BanReason = "";
BanModUserId = -1;
BanModUserName = "";
}
local function kickBannedPlayer(player, data)
message = string.format("You have been banned from this game by %s for %s.",
data.BanModUserName, data.BanReason)
player:Kick(message)
end
Looking at your code, instead of checking for both “:Ban” and “:ban”, just check for “ban” if you use my code. Doing it my way, you can set a variable to be the prefix and then just check for the command as tokens[1] will always be lower case and the prefix will be removed.
When building my admin commands system, I used this post as a guideline. My code is way more complicated than what is shown there, partly because I have implemented a method of banning offline players too.
Hope this helps.