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

How do you make it so this double table doesn't bring an error?

I don't know why the double table hasn't been working, but it was working previously with just a single table like this:

local stats = {}
if stats[0] then
    -- Do stuff with stats
else
    stats[0] = true
end

Here is what I am trying to change it into:

local pn = "playerName"
local stats{[pn]={}}
if stats[pn][0] then
    -- Do stuff with stats
else
    stats[pn][0] = true
end

The problem with the new piece of code is that the if stats["PlayerName"][0] then brings an error instead of saying it's true or false. Also, while I was tryig to fix this, the script is acting like if the "PlayerName" table doesn't exist.. Any help is appreciated, thanks!

2 comments

0

Do stats[pn][1]

0

It still brings an error with your idea, the number value doesn't make the error change.

1 answer

✓ Accepted answer

You didn't put an equal on stats and you can't set a dictionary from inside a table like that. Learn metatables in the future, so you can just create and return nil indexes automatically.

local pn = "playerName"
local stats = {}
stats[pn] = {}

if stats[pn] then 
	if stats[pn][1] then
	    -- Do stuff with stats
	end
else
	stats[pn] = {}
	stats[pn][1] = true
end

print(stats[pn], stats[pn][1])

I recommend this though.

local stats = {}
local function make_new(player_name)
	local this = {}
	this.kills = 10;
	this.deaths = 0;
	stats[player_name] = this
	return this
end

local index = stats[pn]  or make_new('playername')
print(index.kills)

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