While making an admin script for a passion project between myself and some friends, I ran across an issue where the script would only pick up the first UserId within the table - and wouldn’t recieve the following for any other ‘admins’.
After using a ranking system, changing the syntax a multitude of times and trifling with whatever I could - I’ve finally hit the bump in the road.
Of course ‘Player1’ and ‘Player2’ are used to avoid using my friend’s UserId’s.
local Admins = {--Player1;
--Player2;
}
local function Administrator(player)
for _, v in pairs(Admins) do
if v == player.UserId then
return true
end
return false
end
end
The code above illustrates my bewildered state - supposedly it should pick up both names within the table, however, only picks Player1, and no others.
Later on within the script, the code is called as below:
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if Administrator(player) then
message = string.lower(message)
local Split = message:split(" ")
local SemiColon = Split[1]
local cmd = SemiColon:split(prefix)
local CommandName = cmd[2]
if commands[CommandName] then
local arguments = {}
for i = 2, #Split, 1 do
table.insert(arguments,Split[i])
end
commands[CommandName](player, arguments)
end
end
end)
end)
If anyone can provide advice or deliberation on a horrible issue I’ve accidentally made - I would be more than overjoyed!
For now I’ll return to troubleshooting this issue in whatever way I can muster.
I never thought of that before! I had the hunch return was ending the script too early, but I never knew it was the true issue.
Thanks for your quick reply, this has made my day - or maybe my year!
Thankyou for the help!
This was part of the issue due to me having put return too early, and it stopped the script before it could grab the admins from the table - thanks for the help!
It really is impressive how I never thought of that for any case at all for tables. Even though this isn’t my topic or problem, I am going to use this. Looks way more efficient than the for loop.
Actually table.find has the same runtime complexity as a loop because internally it uses the same linear search method. Neither is significantly faster than the other but in terms of cleaner code it is usually nicer to use the function that is already provided by the built-in table library.