This is my first script written for Creator Hub, I created a script with which administrators can register a Kick command and kick players out of the game, can you give any tips on the script?(for example, shorten the code)
local Admins={
"stepan563265",
"Roblox",
"Player1"
}
game.Players.PlayerAdded:Connect(function(plr)
if table.find(Admins, plr.Name) then
print("The logged in player is an administrator")
if not game.TextChatService:WaitForChild("TextChatCommands"):FindFirstChild("KickCommand") then
local Kick=Instance.new("TextChatCommand",game.TextChatService.TextChatCommands)
Kick.Name="KickCommand"
Kick.PrimaryAlias="/Kick"
Kick.SecondaryAlias="/K"
Kick.Triggered:Connect(function(orig,text)
print(orig)
if table.find(Admins,orig.Name) then
print("The player who specified /kick is the administrator")
local TableSpaces={}
for i=1, #text do
if text:sub(i,i)==" " then
local Space=i
table.insert(TableSpaces, Space)
end
end
if #TableSpaces==1 then
local Name=text:sub(TableSpaces[1]+1, TableSpaces[2])
local TargetPlayer=game.Players:FindFirstChild(Name)
if TargetPlayer then
TargetPlayer:Kick("the reason is not specified!")
else
print("The player was not found!")
end
else
local Name=text:sub(TableSpaces[1]+1, TableSpaces[2]-1)
local Message=text:sub(TableSpaces[2]+1, #text)
local TargetPlayer=game.Players:FindFirstChild(Name)
if TargetPlayer then
TargetPlayer:Kick(Message)
else
print("The player was not found!")
end
end
else
print("The player who specified /kick is not an administrator")
end
end)
end
else
print("The logged-in player is not an administrator")
end
end)
local Players = game:GetService('Players')
local Prefix = '/'
local whisper = '/e '
local admins = {
266723646
}
local function isWhisper(str : string) : string
if string.find(str, whisper, 0) ~= nil then
return str:sub(whisper:len() + 1)
end
return str
end
local function hasPrefix(str : string) : string | boolean
if string.find(str, Prefix, 0) ~= nil then
return str:sub(Prefix:len() + 1)
end
return false
end
local function isAdmin(player : Player)
local userId = player.UserId
return table.find(admins, userId) ~= nil
end
local function onChat(player : Player, message, ...)
message = isWhisper(message)
message = hasPrefix(message)
if message == false then return end
if isAdmin(player) == false then return end
local split = string.split(message, ' ')
local command = split[1]
table.remove(split, 1)
if command:lower() == 'kick' then
for _, arg : string in pairs(split) do
for _, v in pairs(Players:GetPlayers()) do
local name = v.Name:lower()
if string.find(name, arg:lower(), 0) ~= nil then
v:Kick('You have been kicked')
end
end
end
end
end
local function playerAdded(player : Player)
player.Chatted:Connect(function(...)
onChat(player, ...)
end)
end
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
end
Players.PlayerAdded:Connect(playerAdded)
Bit late, but hereβs some suggestions if you want to use the TextChatService commands still.
local Admins={
"stepan563265",
"Roblox",
"Player1",
}
-- define services (instead of game.Players / game.TextChatService)
local Players = game:GetService('Players')
local TextChatService = game:GetService('TextChatService')
Players.PlayerAdded:Connect(function(plr)
-- use guard clauses
if not table.find(Admins, plr.Name) then
print("The logged-in player is not an administrator")
return
end
print("The logged in player is an administrator")
-- command already exists
if TextChatService:WaitForChild("TextChatCommands"):FindFirstChild("KickCommand") then
return
end
local Kick = Instance.new("TextChatCommand", TextChatService.TextChatCommands)
Kick.Name = "KickCommand"
Kick.PrimaryAlias = "/Kick"
Kick.SecondaryAlias = "/K"
Kick.Triggered:Connect(function(orig,text)
print(orig)
-- use guard clauses again
if not table.find(Admins, orig.Name) then
print("The player who specified /kick is not an administrator")
return
end
-- use string matching (https://create.roblox.com/docs/luau/strings#character-classes)
-- /%a+ is the command after the slash
-- (%w+) is alphanumeric capture (and to return it as arg 1)
-- (.*) is anything capture if it exists (and to return it as arg 2)
local target, reason = text:match("/%a+ (%w+)%s?(.*)")
local targetPlayer = Players:FindFirstChild(target)
-- reason if it doesn't exist
if not reason or reason == "" then
reason = "the reason is not specified!"
end
if targetPlayer then
targetPlayer:Kick(reason)
else
print("The player was not found!")
end
end)
end)