This is why I don’t really recommend learning from big admin scripts, as they can often be confusing, very broad, and some outdated.
In order to make an admin script, you need to create a server script, place it preferably in ServerScriptService
, and then make it listen for chat messages. If it finds a message that starts with your desired prefix (such as ":"
), check if it’s a valid command, if the player is an admin, etc.
What you should not do is using if statements along with string.sub
to check for commands and arguments. Instead, store all commands as functions in a table, and upon finding a chatted command split the message by spaces, match the first split against the list of commands, and call the command function with the rest of the arguments.
Example code:
local admims = {ScytheSlayin = true}
local plrs = game:GetService"Players"
local cmds = {} --make commands an empty table, it's gonna be a dictionary table
local prefix = ";"
local function AddCommand(name, func) --addcommand function, for adding commands
cmds[name:lower()] = func --insert the command into the table, the name of it will be the key
end
local function Split(str)
local words = {}
for str in string.gmatch(str, "([^%s]+)") do --split by space
words[#words + 1] = str --insert all splitted words into the table
end
return words
end
local function GetPlayers(text, plr)
local t = {}
if text = "all" then --if text is "all", return all players
return plrs:GetPlayers()
elseif text == "me" then --if it's "me", return a table with only the plr
return {plr}
elseif text == "others" then --if it's "others", return everyone except the plr
for i,v in pairs(plrs:GetPlayers()) do
if v ~= plr then
table.insert(t, v)
end
end
return t
end
--if none from above, loop through all players
for i,v in pairs(plrs:GetPlayers()) do
if v.Name:lower():sub(1,#text) == text:lower() then --if first x characters of their nickname match the text
table.insert(t, v) --add them to the table
end
end
return t
end
AddCommand("kill", function(plr, args)
if not args[1] then return end --exit if no args were supplied
for i,v in pairs(GetPlayers(args[1], plr)) do
if v.Character then
v.Character:BreakJoints()
end
end
end)
plrs.PlayerAdded:Connect(function(plr)
if admins[plr.Name] then
plr.Chatted:Connect(function(msg)
if msg:sub(1, #prefix) == prefix then --if starts with the prefix
msg = msg:sub(#prefix + 1) --remove the prefix
local args = Split(msg)
local cmd = cmds[table.remove(args,1):lower()]
if not cmd then return end --command not found
pcall(cmd, plr, args) --run the command
end
end)
end
end)
(forgive any mistakes, it’ 1:35 am and I’m on mobile)