Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

0

By the way thats legit the end of the code because I haven't written the rest

0

Since you are using a table, maybe ipairs can help you. https://developer.roblox.com/articles/For-Loops#standard-library-iterators

2 answers

✓ Accepted answer

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	

1 comment

0

If you want to know how to use the functions, you can do if checkAdministratorStatus(playerInstance) == true then statements.

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

1 comment

0

There's an easier way to do this.

This discussion is preserved as it was. New answers, comments, and votes are closed.