Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't my table of names work in for my admin script and how do I fix it?

Asked by 5 years ago
Edited 5 years ago

I have a script here and it doesn't work for some reason. It is suppose to confirm if your an admin or not. I wonder what is wrong with my script

 local UserName = {"InsertNameHere","voidofdeathfire"}



game.Players.PlayerAdded:Connect(function(player)

print(player.Name)

if player.Name == UserName then

while true do

wait(1)

print("ADMIN")



end

end

end)
0
By the way thats legit the end of the code because I haven't written the rest voidofdeathfire 148 — 5y
0
Since you are using a table, maybe ipairs can help you. https://developer.roblox.com/articles/For-Loops#standard-library-iterators CSploit 0 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Here's the easier way:

It isn't where you have to type the exact player name with the correct capitalization. It allows you to just type it like, for example, Johny. You can type it like JoHnY and it still counts as a player name.

But I still recommend using user Ids in case of a user changing their username.

local administratorList = {}

local function checkAdministratorStatus(player)
    local loweredPlayerName = string.lower(player.Name)
    for _,Administrator in next,administratorList do
        local loweredAdministratorName = string.lower(player.Name)
        if loweredPlayerName == loweredAdministratorName then
            return true
        end
    end 
end

User Id Version:

local administratorList = {}

local function checkAdministratorStatus(player)
    for _,Administrator in next,administratorList do
        if player.UserId == Administrator then
            return true
        end
    end 
end 
0
If you want to know how to use the functions, you can do if checkAdministratorStatus(playerInstance) == true then statements. User#17915 0 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited by User#24403 5 years ago

You are comparing a single string to a table. What you need to do is compare the contents inside the table (strings) with the username string. There are two ways to do this:

Firstly is a "for loop" with a regular table:

local Administrators = {
    "Player1",
    "Player2"
}

game.Players.PlayerAdded:Connect(function(player)
    for _, adminName in pairs(Administrators) do
        if player.Name == adminName then
            print("Administrator")
        end
    end
end)

Another alternative is to use a dictionary:

local Administrators = {
    ["Player1"] = true,
    ["Player2"] = true
}

game.Players.PlayerAdded:Connect(function(player)
    if Administrators[player.Name] then
        print("Admin")
    end
end)

This works because if the key (player name) exists in the dictionary of Administrator names, it will evaluate to true. If not, it will evaluate to nil

0
There's an easier way to do this. User#17915 0 — 5y

Answer this question