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

Printing values within dictionaries?

Asked by 7 years ago

I'm attempting to create a dictionary of player statistics, such as kills, deaths and money, but when I attempt to print said values, it returns nil. I've attempted "pcall" as well as the documentation on the ROBLOX wiki, but this was unable to help.

local myTable = {
    TheHospitalDev = {
        ["kills"] = 10,
        ["deaths"] = 1,
        ["money"] = 10000},
    OtherPlayer = {
        ["kills"] = 1,
        ["deaths"] = 10,
        ["money"] = 1000},
}

local function displayInfo()
    for index,value in pairs(myTable) do
        print(index,"has:")
        print("...Kills:",myTable[value[1]])
        print("...Deaths:",myTable[value[2]])
        print("...Money:",myTable[value[3]])
        print("Total:",myTable[value])
    end 
end

local success,message = pcall(displayInfo)
if success then
    print("Success")
else
    print("Error:",message)
end

Thank you in advance

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Your problem is how you are going about indexing the dictionary. Dictionaries have strings as indexes, not integers. This is why they are called dictionaries. You have to index it by the name of the actual index.

local myTable = {
    TheHospitalDev = {
        ["kills"] = 10,
        ["deaths"] = 1,
        ["money"] = 10000},
    OtherPlayer = {
        ["kills"] = 1,
        ["deaths"] = 10,
        ["money"] = 1000},
}

local function displayInfo()
    for index,value in pairs(myTable) do
        print(index,"has:")
        print("...Kills:",value.kills) --Use actual index names
        print("...Deaths:",value.deaths)
        print("...Money:",value.money)
        print("Total:",myTable[value])
    end 
end

local success,message = pcall(displayInfo)
if success then
    print("Success")
else
    print("Error:",message)
end
Ad

Answer this question