Retrieving table data from a table/array?

I found something that looked similar to C++ structs that could work in Roblox lua. I was just wondering how someone could be able to retrieve the data with its corresponding data (if that makes any sense)?

For example:

    local playersStatsArray = {};
    local playersStats;

    -- Make player stats and add them into playerStatsArray
    for i, v in pairs(players) do
        playerStats = {
            ["name"] = game.Players:FindFirstChild(v), ["money"] = 100;
        }
    end

Say we have a player named “Hello123” and another player named “Bye123”, how could we retrieve the ‘money’ stats from both of these players?

1 Like

In Lua, the best way to do this would be a dictionary.

You set the data for each player in playerStatsArray like so:

playerStatsArray[playerName] = {} -- Create the player dictionary
playerStatsArray[playerName]["name"] = playerName -- Set the name
playerStatsArray[playerName]["money"] = 50 -- Set the money

You can then access it like so:

local money = playerStatsArray[playerName]["money"]
1 Like

Thank you so much! I appreciate it!

1 Like