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

How to check if a player in-game is in an table/array?

Asked by 5 years ago

How would I have multiple usernames in a table, then check if a player with said username joins the game?

I made this, but it's not quite what I'm trying to make.

game.Players.PlayerAdded:connect(function(player)
    if player.Name == "centuriontb" then
        print(player.Name.." joined the game")
    end
end)

I have no idea how I would make it work, I've checked the wiki but im still confused.

I tried doing this, but it doesn't work. anyway to fix this and make it work how I want?

local oof = {
    "Player1",
    "Player2"   
}
game.Players.PlayerAdded:connect(function(player)
    if player.Name == oof then
        print(player.Name.." joined the game")
    end
end)

To make any player in the table "oof" print in the console when they join the game. Please and thanks :D

2 answers

Log in to vote
0
Answered by 5 years ago

You got close, but everytime we need to "loop" the table to see if there names match. In your script:

local oof = {
    "Player1",
    "Player2"   
}
game.Players.PlayerAdded:connect(function(player)
    if player.Name == oof then
        print(player.Name.." joined the game")
    end
end)

The if statement is checking for a variable. It would obviously not do anything since we need to check the whole table. The event would not print or do anything either as nothing is after the if statement. Instead you should be comparing it to a string,

Alright so you recognized your problem, now we get to solve it! In programming we can "loop" through a table. Looping through a table is like getting school children's attendance checked. If Student1 and Student2 are in a table, the loop would go through Student1 and Student2 it will always go in sequence according how you put the table together.

To call that term we call it iteration.

You might be confused right now: Here is a better explanation on tables right on the wiki! It also has other useful table stuff too (Scroll down to iteration) https://developer.roblox.com/en-us/articles/Table

To fix your script we iterate through the table as such:

local oof = {
    "Player1",
    "Player2"   
}
game.Players.PlayerAdded:connect(function(player)
    for i,v in pairs(oof) do
     if player.Name == v then
             print(player.Name.." joined the game")
        end
end)

You can ask questions anytime.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Instead of this:

local oof = {
    "Player1",
    "Player2"   
}
game.Players.PlayerAdded:connect(function(player)
    if player.Name == oof then
        print(player.Name.." joined the game")
    end
end)

You can do this:

local oof = {
    Player1 = true,
    Player2 = true,
}
game.Players.PlayerAdded:Connect(function(player)
    if oof[player.Name] then
        print(player.Name.." joined the game")
    end
end)

Still O(1), so it will scale well to a very large player list (because it does not have to loop over an array on each player join, which has O(n) runtime). Just be aware that you can't build the table like this for all valid player names, because some old Roblox names start with numbers. But this is easily overcome using this syntax:

local oof = {}
oof["Player1"] = true
oof["Player2"] = true
oof["1337Player"] = true

The important distinction from your original form is just that this is a dictionary where the player names are the keys, not an array as in your original code. Here the values of true are just because a dictionary will only store keys with non-nil values, the actual value is arbitrary.

Answer this question