I'm confused with accessing stuff in tables

So say there’s a table like:

Table = {}

and I added values to the table this way:

Table[value1] = value2

how do I access value2? like this?

if Table[value2] then
      -- do stuff
end

This tutorial should help you out.

Values in a table can be assigned in the format of Table[Key] = Value. To access the value, you must use the key.

In your example, to access value2, you would write Table[value1]. Intuitively, you wouldn’t write Table[value2] to access value2, as that would assume you already have access to value2.

2 Likes

For anyone who finds PiL hard to understand, or has formatting issues with it, the Roblox DevHub has an article as well.

3 Likes

If you already have value2 im not sure why you would want to index Table for value2?

@sjr04 The reason I want to remove it from the table too is for checks, to make sure the instance or value is no longer a member of the table. Also tables are useful for other things like teams.

Here is a couple of examples on what you can do

local team = {}

table.insert(team, player) -- add a player
local indexInArray = table.find(team, player) -- returns the index if it finds the value passed, otherwise it will return nil

if indexInArray then
    local player = team[indexInArray] -- you can get the player again like this
    table.remove(team, indexInArray) -- removes a player from the array/team
end

I prefer the example above, but you can also do it like this, and it’s a bit easier to understand

local team = {}

team[player] = true -- add player to team
team[player] = nil -- remove player from team
local isPlayerInTeam = team[player] -- check if a player is in the team
1 Like