How to format multiple UserIDs?

I’m trying to make a simple script that prints “Player is in the server” when a player with a corresponding UserID from a variable joins the game. Here’s the script:

local ListedUserId = {'123456789', '987654321'} -- Just throwing random IDs out there
if Player.UserId == ListedUserId then
   print("Player with the matching userID joined the game!")
end

(Yes, there’s already something for “Player,” that’s not the problem.)

My main question is, how should I be formatting multiple user IDs in the local variable to work with the rest of the script? Currently, it does not work.

Thanks in advance for your response!

1 Like

I didn’t understand completely but I think this is what you mean-
You have a table with some userIDs.
And if a player joined with any of those userIDs, you want to print out (“Player with the matching userID joined the game”)

You could do it like this -

local ListedUserIds = {"123456789" , "987654321"}
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
    for i, userID in pairs(ListedUserIds) do
        local userIDInteger = tonumber(userID)

        if userIDInteger == player.UserID then
            print("Player with the matching userID joined the game")
        end
    end
end)

I will try to explain it.
So first of all, the table has strings, so you will need to convert them to integers while checking.
So what the script does is just that whenever a player joins the game, it loops through all of the userIds in the table and if any of them are same, then it prints it.

2 Likes

That is because you are comparing an integer (Player.UserId) with a string (UserId wrapped in quotes in the table).

Simplest fix?
Remove the quotes.
(Also for that to even work it needs to be in a PlayerAdded event, I’d recommend you follow what @GoodBoy_786 sent as he provides an example.)

1 Like

To add onto what @GoodBoy_786 Provided, I believe the code could be a lot shorter by using table.find

local ListedUserIds = {"123456789" , "987654321"}
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
    if table.find(ListedUserIds, tostring(player.UserId)) then
        print("Player with the matching userID joined the game")
    end
end)

This should work because if it finds that the player has the same userid as something in the table, then it’ll return a non-nil value, but if it doesn’t find the same userid in the table, then it’ll return nil, which would run the code in the if statement.

Also as @Tritative mentioned, it’s better to remove the quotes of the IDs in ListedUserIds since tables in Roblox can also store numbers, which also removes the need to convert the userid into a string

3 Likes

Thanks, guys! I would mark a solution. However, all of you are right.

I already did have an event to define what “Player” meant. I probably should’ve included that in the script (my bad).

To specify more what I was trying to say…I was trying to find out how I should reformat the variable to work with the script I had. (So, Tritative would have the answer that I was looking for.)

However, I’m interested in the ways that the other two of you used to accomplish this feature, so thanks so much for showing me!

This is simplest way.

local ListedUserId = {123456789, 987654321} -- have them as numbers
if table.find(ListedUserId, Player.UserId) then -- check if value is in table
   print("Player with the matching userID joined the game!")
end
2 Likes

There are 2 ways you could achieve this. If you want to just make it happen once when the server starts, you could do @WhitexHat’s method, if you want to make it check everytime a player joins, you could use a changed version of how I did it

local ListedUserIds = {123456789, 987654321} --Numbers in this case
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
    if table.find(ListedUserIds, player.UserId) then --Returns nil if the id is not in the table
        print("Player with the matching userID joined the game")
    end
end)
local list = {123123123, 456789789}

local updatedfasterandbetterlist = {}

for k, v in next, list do
 updatedfasterandbetterlist[v] = true
 list[k] = nil
end
list = nil

game.Players.PlayerAdded:Connect(function(Player)
  if updatedfasterandbetterlist[Player.UserId] then
    print'yes in the list'
  end
end)

Keys are faster than searching say, a list containing 300 ids
btw player ids are integers, not string.

The Player’s UserId isn’t a table, it’s a number.

if ListedUserId[Player.UserId] then

would be the right solution here.
Also, as I already said before, the Player’s UserId is a number, you’re using a string in your table. Therefore remove the speech marks.

try:

if table.find(ListedUserId, Player.UserId) then
-- Insert code here
end

You can store all user ids in a regular array, when checking if the player’s userid is one if them I suggest iterating though the array and check if the values is equal to the user id. You can iterate over the array user a (for i,v in pairs(array) do) loop

It is slow if you iterate the entire array. Which is why using the UserIds as keys will be a faster and more efficient solution.

Yeah actually that makes more sence. So the key is the userid what should the value be?

Yes
the table should look like this

{
  [123123123123] = true -- any value that isn't nil or false
}
local ListedUserId = {'123456789', '987654321'} -- Just throwing random IDs out there
if table.find(ListedUserId, Player.UserId)  then
   print("Player with the matching userID joined the game!")
end