What do you want to achieve? I simply require feedback from more experienced individuals in coding.
What is the issue? There’s no imminent issue, as my code works finely, however, I can possibly recognize that my code might need optimized, and I might need advice in changing certain parts of it.
What solutions have you tried so far? Regarding on seeing how I could try debugging it, I’ve mainly tested with miscellaneous ways, though I fear I might’ve missed some certain bugs.
So, for more context, I consider myself an intermediate-level developer. I’m now just learning to use tables, optimized methods, et cetera. However, in order to truly see if I’m heading towards the right path, I’ve decided to stack it into a script.
The script, essentially, is a basic admin command one. Containing only three commands:
:SetServerLimit
A simple command that sets a limit of players to the server. It should supposedly kick any joining player when the limit has been reached.
:CheckPlayers
A command that lists out all of the players’ names within the console, and how many there are total.
:ToggleServerLock
A toggle command that could lock the server to prevent any players from joining.
PlayersTable = {}
PlayersService = game:GetService("Players")
SSLCommandList = {":SSL", "/e :SSL", ":SetServerLimit", "/e :SetServerLimit"}
ServerLimit = 5 --Default
AdminTable = {18807645}
Allow = false
ServerLock = false
PlayersService.PlayerAdded:Connect(function(Player)
if #PlayersTable < ServerLimit and not ServerLock then --if the server limit is not exceeded by adding this player, and the server isn't locked
table.insert(PlayersTable, Player.UserId)
elseif #PlayersTable >= ServerLimit or ServerLock then --if the server limit is exceeded by adding this player, or the server is locked
print("The server is full, and " .. Player.Name .. " was kicked.")
if ServerLock then
Player:Kick("The server was locked.")
elseif not ServerLock then
Player:Kick("The server was full.")
end
end
Player.Chatted:Connect(function(msg)
if table.find(AdminTable, Player.UserId) then
Allow = true
end
if Allow then --If player is an administrator
for i,v in ipairs(SSLCommandList) do
if not (msg == ":CheckPlayers") and string.find(msg, v) then --If not command being set to an integer value and msg is SSL
local Number = string.sub(msg, -1)
local Number2 = -3
if tonumber(string.sub(msg, -2)) > 9 then
Number = string.sub(msg, -2)
Number2 = -4
end
if msg == string.sub(msg, 0, Number2) .. " " .. Number then
ServerLimit = Number
print("The server limit has been changed by Administator " .. Player.Name .. " to " .. Number .. ".")
return
end
end
end
if msg == ":CheckPlayers" then --If player activates the checkplayer command
for i,v in ipairs(PlayersTable) do
local StringValue = "players"
if #PlayersTable == 1 then
StringValue = "player"
elseif #PlayersTable > 1 then
StringValue = "players"
end
print("These are all of the players: " .. PlayersService:GetPlayerByUserId(v).Name .. "." .. " Amongst all of them, they combined to be " .. #PlayersTable .. " " .. StringValue .. ".")
return
end
elseif msg == ":ToggleServerLock" then --Setting serverlock, self-explanatory
if not ServerLock then
print("The server has been locked by Administrator " .. Player.Name .. ".")
ServerLock = true
elseif ServerLock then
print("The server has been unlocked by Administrator " .. Player.Name .. ".")
ServerLock = false
end
end
end
end)
end)
PlayersService.PlayerRemoving:Connect(function(Player)
for i,v in ipairs(PlayersTable) do
if v == Player.UserId then
table.remove(PlayersTable, i)
return
end
end
end)
If you could, provide me some feedback regarding more efficient or optimized methods. (Also, if this type of feedback is not permitted within this category, then my condolences.)
Just to make sure, would the optimized script look something like this then?
PlayerService = game:GetService("Players")
ServerLimit = 5
PlayerService.PlayerAdded:Connect(function(Player)
local PlayersTable = PlayerService:GetPlayers()
if #PlayersTable >= ServerLimit then
--(And so on)
end)
You can try to shorten your code more in some cases, one example I saw a lot was the unnecessary usage of elseif when the code could* work perfectly fine by using just an else, example:
Here is a more optimized and efficient version of the script, according to your feedback. Any issues/suggestions @Gooncreeper and @wiva15?
PlayersService = game:GetService("Players")
SSLCommandList = {":SSL", "/e :SSL", ":SetServerLimit", "/e :SetServerLimit"}
ServerLimit = 5 --Default
AdminTable = {18807645}
Allow = false
ServerLock = false
PlayersService.PlayerAdded:Connect(function(Player)
local PlayersTable = PlayersService:GetPlayers()
if #PlayersTable >= ServerLimit or ServerLock then --if the server limit is exceeded by adding this player, or the server is locked
print("The server is full, and " .. Player.Name .. " was kicked.")
if ServerLock then
Player:Kick("The server was locked.")
else
Player:Kick("The server was full.")
end
end
if table.find(AdminTable, Player.UserId) then
Allow = true
end
Player.Chatted:Connect(function(msg)
if Allow then --If player is an administrator
for i,v in ipairs(SSLCommandList) do
if not (msg == ":CheckPlayers") and string.find(msg, v) then --If the command is being set to an integer value (i.e, :SSL 5) and msg is SSL
local Number = string.sub(msg, -1)
local Number2 = -3
if tonumber(string.sub(msg, -2)) > 9 then
Number = string.sub(msg, -2)
Number2 = -4
end
if msg == string.sub(msg, 0, Number2) .. " " .. Number then
ServerLimit = Number
print("The server limit has been changed by Administator " .. Player.Name .. " to " .. Number .. ".")
return
end
end
end
if msg == ":CheckPlayers" then --If player activates the checkplayer command
for i,v in ipairs(PlayersTable) do
local StringValue = "players"
if #PlayersTable == 1 then
StringValue = "player"
else
StringValue = "players"
end
print("These are all of the players: " .. v.Name .. "." .. " Amongst all of them, they combined to be " .. #PlayersTable .. " " .. StringValue .. ".")
return
end
elseif msg == ":ToggleServerLock" then --Setting serverlock, self-explanatory
if not ServerLock then
print("The server has been locked by Administrator " .. Player.Name .. ".")
ServerLock = true
else
print("The server has been unlocked by Administrator " .. Player.Name .. ".")
ServerLock = false
end
end
end
end)
end)
First is that, for your :CheckPlayers command, the player table may be inaccurate because you got the table at start of the script, I recommend you to update it when the command is called.
Second is, I’m not sure why you are looping trough the SSLCommandList, I believe you can still get it to work without the loop.
Do you plan on having the admintable changing in real time? I might of been confused, if so then the allow thing wont work, otherwise why dont you just do it like this,
if Allow then --If player is an administrator
Player.Chatted:Connect(function(msg)
end
end
When putting the list in the string.find, I was using a loop, otherwise, I’d just get this (unless if I’m just being a buffoon, and I’m not putting it in correctly).
yeah but i dont do that. i put all commands in one module, reusable functions in a function module, and even admins in a module. yeah i like modulescripts.