Help me understand Tables better?

I’m not sure I understand Tables (even after reading the Tables documentation).

I have an array where if players are put there, they are safe and can continue to the next area.

for i, v in pairs(playerArray) do -- playerArray is the array in which contains the current alive players
    if game.Players:FindFirstChild(v) then -- checks to see if those players are still in the game
        if not table.find(safePlayersArray, v.Name) then
            print("Someone died!");
            game.Players:FindFirstChild(v).Character:BreakJoints();
        end
    end
end

For some reason, the players are dying regardless of being in that safePlayersArray or not. :confused:

1 Like

I assume Name is not valid as you are finding the player with just v. Instead of using

if not table.find(safePlayersArray, v.Name) then

Use this:

if not table.find(safePlayersArray, v) then
1 Like

Unfortunately, I’ve tried both just v and v.Name. The characters still die.

This is the only spot in the code where I have the .Character:BreakJoints(); execution so I know it’s happening here somewhere.

Does it ever add anyone to the safePlayersArray?

1 Like

I do understand tables may be confusing at some points this is explanation of what tables are.

Links: Tables | Documentation - Roblox Creator Hub.

1 Like

FindFirstChild() uses a string, not an object, v in this case is an object, so you should use FindFirstChild(v.Name)
Actually scratch that, you can replace the whole thing with v

1 Like

I’d recommend you read this first in order to understand what array’s are, I’ll be here to help just let me know if that link helped.

1 Like

As she said, it kills the player meaning she has v as a string.

1 Like

I thought she was talking about the table.find function

1 Like

No, she is talking about an Array.

2 Likes

The logic in my code does make sense, right?

I’m assuming you are using players:Get players in order to get all the players. The issue is that you will get player objects and not the name of the players.

Table.find will not work here as it only searches for what is within the index and not what is within that object properties like a name, a for loop will be required instead.

1 Like

Ah. Maybe that is why! Let me try that out really quick and I’ll be right back.

Thank you so much! That did the trick.

Thanks everyone for your input! I really appreciate it! :slight_smile: