Insert table with a name

#1

What I want

['New House'] = {
    House = {},
    Purchases = {}
}

What I am currently doing

local PlotName = i -- i = 'New House'
PlotName = {
	House = {},
	Purchases = {}
}
table.insert(User.Houses, PlotName) --User.Houses is a table

So PlotName should == ‘New House’, so

PlotName = {
		House = {},
		Purchases = {}
	}

Should be

['New House'] = {
    House = {},
    Purchases = {}
}

However the table is just being stored as 1

#2

Can’t you just do User.Houses["New House"] = PlotName? I’m confused on why you would need to use table.insert, am I misunderstanding something?

1 Like
#3

Cause there is no New House in the User.Houses. What I want is to only add in the new house when a player has unlocked it, so it has to get added into the table when they unlock the house

#4

So the final table structure you want would be something like this?

User.Houses = {
     [i] = { -- whatever the variable `i` is
          House = {},
          Purchases = {}
     }
}

If so, you should just be able to do User.Houses[i] = { House = {}, Purchases = {} }

#5

I get this error when it tries to save now tho
[Cannot convert mixed or non-array tables: keys must be strings]
Is there anyway to debug this? I’ve got a DataStore viewer, and it looks the same as the default table


Both those tables contains House and Purchases. The Default.House table has items in it, while the Nicer House has 2 empty tables. It should still save tho?

#6

You can’t save mixed tables in DataStores (along with any other JSON related services).

If you don’t know, this is a mixed table

local t = {
    [1] = "";
    Two = "";
};
1 Like
#7

But it should look like this

User.Houses = {
    ['Default'] = {
        House = {},
        Purchases = {}
    },
    ['Nicer House'] = {
        House = {},
        Purchases = {}
    },
}

So what part of it is mixed?? Cause if I just leave with only the Default table it works fine, it’s only when the Nicer House table gets added in (which Im doing like this)

User.Houses[i] = { House = {}, Purchases = {} }
#8

From your previous posts, you stated you were trying to use table.insert() to add the new data. This only works in arrays, not dictionaries.

What is your “i” variable?

#9

i is ‘Nicer House’

I’ve since changed the table.insert to this:

User.Houses[i] = { House = {}, Purchases = {} }

As stated in @posatta post

#10

If possible, could you please provide a bit more code? There seems to be a picture that’s not being seen right now.

#11

So there’s the players data (below is their default data when they first join)

PlayerData = {
    Houses = {
        ['Default'] = {
            House = {},
            Purchases = {}
        },
    }
}

Now when they unlock the next house, I do this:

User.Houses[i] = { House = {}, Purchases = {} }

to add the new house (‘Nicer House’) to this list, so it should look like this

User.Houses = {
    ['Default'] = {
        House = {},
        Purchases = {}
    },
    ['Nicer House'] = {
        House = {},
        Purchases = {}
    },
}

Please note ‘User’ is the PlayerData table.

So before they unlock the new house, when I leave rejoin, etc. It works fine, no errors, it’s only when I add in the house that I get the error of
[Cannot convert mixed or non-array tables: keys must be strings]
when I leave the game.

Now, according the DataStore viewer plugin, it looks like this when the new house is added


So idk why I’m getting the error.

#12

Your i variable is most likely an object. Add a print(i, typeof(i)) before adding it into the table (or show the code where you define i, like people already asked you to do).
Perhaps you accidently store the instance in it, while you meant to store its name?

Also, as someone already said, you can do

User.Houses["New House"] = { House = {}, Purchases = {} }

You can use a variable instead of a literal "New House", but make sure the variable is a string and not an instance.

1 Like