How to check if player is in player list?

So I have this script where if your name is on the list, when you type /checkin, it will work. But it doesn’t work, I tried module’s but had 0 clue how to do it, so im trying this now, any help?

local NYPD = {"Pooglies","tjooking23","xT_iny"}

game.Players.PlayerAdded:Connect(function(Player)
		if Player.name == NYPD then
			Player.Chatted:Connect(function(msg)
				if string.lower(msg) == "/checkin" then
					Player.Team = game.Teams.NYPD
					wait(0.1)
                  Player:LoadCharacter()
				end
			end)
		end
	end)
1 Like

Your if statement’s condition is checking if the player’s name (string) is equal to the variable NYPD (table) which is always going to return false since you’re never converting the table to a string and I highly doubt someone would have a username that’d be equal to the table’s address.

What you should be doing is trying to index the player name in the table instead of comparing the table to the name. To do that, set the table indices to the player usernames and their values to true like so:

{["Example Username"] = true}

and then try to index the username inside of the table in your if statement’s condition:

if NYPD[Player.Name] then ... end
1 Like

Try using table.find to see if the Player’s Name exists inside the table.

local NYPD = {"Pooglies","tjooking23","xT_iny"}

game.Players.PlayerAdded:Connect(function(Player)
   -- Check if the Player's Name is in the NYPD table
   if table.find(NYPD, Player.Name) then 
      Player.Chatted:Connect(function(msg)
         if string.lower(msg) == "/checkin" then
            Player.Team = game.Teams.NYPD 
            game:GetService("RunService").Stepped:Wait()
            Player:LoadCharacter()
         end
      end)
   end
end)

I’d recommend using UserId instead of the name as people can change their names.

I don’t usually dabble with player id’s, how would I go about using them in this situation.

Just get the UserIds of the people you want whitelisted by searching them up and copying them on the search bar.

Then you would need to edit your code accordingly.

local NYPD = {1, 261, 1337} -- all userIds

game.Players.PlayerAdded:Connect(function(Player)
   -- Check if the Player's UserId is in the NYPD table
   if table.find(NYPD, Player.UserId) then 
      Player.Chatted:Connect(function(msg)
         if string.lower(msg) == "/checkin" then
            Player.Team = game.Teams.NYPD 
            game:GetService("RunService").Stepped:Wait()
            Player:LoadCharacter()
         end
      end)
   end
end)
4 Likes

Hellloooooo so, Just saying anyway, sometimes :LoadCharacter() Remove’s some gui’s and sometimes glitchs ( for me ), I would use table.find!!

local NYPD = {“Pooglies”,“tjooking23”,“xT_iny,”, “123123123”}

game.Players.PlayerAdded:Connect(function(Player)
if table.find(NYPD, Player.Name) then
Player.Team = game.Teams.NYPD
Player()
end
end)

2 Likes

You should always work in UserIds when handling player benefits and whatnot, rather than working with usernames. On that note though (cc @AbiZinho), it’s possible to keep using usernames but work with UserIds. Try not to use it where not necessary though and just stick to inputting UserIds.

Enter: GetUserIdFromNameAsync. Usernames are permanently associated with an id, so this function takes a username and finds the id it’s associated with, then returns that out. If you don’t want to go UserId hunting, feed this either into your code or the command bar for the player’s UserId.

4 Likes