Hello this is my first roblox community tutorial!
This tutorial will tech how to make a ;kick command which will kick a player with a message
So first insert a new script into ServerScriptService call it AdminCommands
Then make a module script called Settings and make it the child of AdminCommands
Now open up AdminCommands. First we need to get the settings module(We will edit this later)
So in the script put:
local settings = require(script.Settings)
Next we need to define a table which will store our commands
so we type
local commands = {}
Ok so now open up the Settings script and type in this:
local module = {}
module.admins = {
"YourUsernameHere";
--Admins Names Here
}
--what the command prefix is
module.prefix = ";"
return module
The admins variable creates a table of admins in the game. The prefix variable defines what you put before your commands(ex: / in Minecraft)
Next we need a function which will run every time a player speaks, check if they used the prefix, find the command and run that commands function with arguments
game.Players.PlayerAdded:Connect(function(player) --Runs when a player joins
player.Chatted:Connect(function(message, recipient) -- Runs when a player sends a message
if isAdmin(player) then -- Checks if they are an admin
message = string.lower(message) --Converts the message to all lowercase
local splitString = message:split(" ") --Splits the message based on spaces
local Command = splitString[1] --Gets the command from the first word of the message
local cmd = Command:split(settings.prefix) --Splits the first part of the msg based on the prefix
local cmdName = cmd[2] --Gets the requested command
if commands[cmdName] then --Checks if the command exists
local args = {} --Defines a table for arguments
for i = 2, #splitString, 1 do --Creates a table of all the arguments of the message
table.insert(args,splitString[i])
end
commands[cmdName](player,args) --Runs the command
end
end
end)
end)
Ok so now we actually need to define a command so under the commands variable at the top type:
commands.kick = function(sender, arguments) --Adds a function to the commands table
local target = arguments[1] --Gets the target player
local targetPlayer = FindPlayer(target) --More on this later
local messageTable = {} -- Creates a table for the kick message
local kickMessage = "" --Creates a string for the kick message
for i, v in pairs(arguments) do --Runs a for loop for all the arguments
if i == 1 then
print("Skipping First One") --Skips the first argument because thats the player
elseif i >= 2 then
kickMessage = kickMessage.." "..v --Adds on the part to the kick message
end
end
targetPlayer:Kick(kickMessage) --Kicks the player
end
Now don’t run it yet. You may have noticed this FindPlayer function doesn’t come up as a function and thats becuase we haven’t made it yet. So to make this function above your commands.kick function type:
local function FindPlayer(name) --Defines a function that takes the name argument
for i, player in pairs(game.Players:GetPlayers()) do --Runs a loop through all the players in game
if string.lower(player.Name) == name then --Checks if the argument is actually a player
return player --Returns the player
end
end
return nil
end
So Now your script should look like this:
local settings = require(script.Settings)
local commands = {}
local function FindPlayer(name)
for i, player in pairs(game.Players:GetPlayers()) do
if string.lower(player.Name) == name then
return player
end
end
return nil
end
commands.kick = function(sender, arguments)
local target = arguments[1]
local targetPlayer = FindPlayer(target)
local messageTable = {}
local kickMessage = ""
for i, v in pairs(arguments) do
if i == 1 then
print("Skipping First One")
elseif i >= 2 then
kickMessage = kickMessage.." "..v
end
end
targetPlayer:Kick(kickMessage)
end
local function isAdmin(player)
for _, v in pairs(settings.admins) do
if v == player.Name then
return true
end
end
return false
end
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message, recipient)
if isAdmin(player) then
message = string.lower(message)
local splitString = message:split(" ")
local slashCommand = splitString[1]
local cmd = slashCommand:split(settings.prefix)
local cmdName = cmd[2]
if commands[cmdName] then
local args = {}
for i = 2, #splitString, 1 do
table.insert(args,splitString[i])
end
commands[cmdName](player,args)
end
end
end)
end)
And module script look like:
local module = {}
module.admins = {
"YourNameHere";
--Admins Names Here
}
--what the command prefix is
module.prefix = ";"
return module
So now you should have something like this:
FAQ
Q: Help! The command doesnt run!
A: Did you replace the “YourNameHere” with your username?