Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Printing values within dictionaries?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.