Trying to access a list in a list but returns nil

local PlayerInventory = Inventory:GetAsync(player.UserId) or {Items = {Trails = {"DefultTrail"}, Outfits = {"DefultOutfit"}}, Bubbles = 1000}
	for _,P in pairs(Inventory:GetAsync(player.UserId)) do
		print(tostring(P))
	end

Output

DefultOutfit

DefultTrail

How do i access Trails and Outfits separate
if i say PlayerInventory[“Items”] it returns nil :confused:

what you are doing is you are accessing the tables keys inside and not the table inside so what you could do add a second pairs loop that loops through pairs and that would give you the value for each table. here is an example

local PlayerInventory = Inventory:GetAsync(player.UserId) or {Items = {Trails = {"DefultTrail"}, Outfits = {"DefultOutfit"}}, Bubbles = 1000}
	for _,P in pairs(Inventory:GetAsync(player.UserId)) do
		for _,v in pairs(P) do
            print(tostring(v))
        end
	end

Dont forget to mark this as a solution if it helps :grin:

19:54:05.945 - ServerScriptService.Inventory:46: invalid argument #1 to ‘pairs’ (table expected, got string)

when i run that code it outputs this error message
this error message is for the print function i think. and also i created a new data store to see if i had just accidentally deleted “Items” or “Trails” but it still comes back with an error

2 Likes

i just realized you have many different tables with some with 1 index such as bubbles and then some with 2 tables deep. You need to understabd how pairs loops work before I can help you. Basically, a pairs loop only goes trhough ONE table at a time so if you call it is is going through items and sees the value is a table which has no value. you would have to go trhough a pairs loop each time there is a table inside a table. but since you have tables with multiple tables inside and some with only a single value it is going to throw errors sinc it doesnt know which tables are 2 deep or are a single value, I recommend storing all the values in a single table and JSONEncoding it to reduce table complexity.

1 Like

it was working before for some reason it just stopped. but i never changed any part of that code so its strange

also
What is JSONEncoding?

1 Like

I think you’re wanting to do this:

local PlayerInventory = Inventory:GetAsync(player.UserId) or {Items = {Trails = {"DefultTrail"}, Outfits = {"DefultOutfit"}}, Bubbles = 1000}

for i,v in pairs(PlayerInventory) do
	print(tostring(i), tostring(v))
end
1 Like

You should look at these articles, but basically JSONEncoding just turns any table or value you put inside into a readable string and json decoding decodes it back into a table. It is useful for storing a lot of values inside a table into a single one so that datastore doesnt get overloaded. Articles

https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONDecode

https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONEncode

Thanks, so now i can access trails and outfits but how do i access bubbles?

sorry for my lack of knowledge i don’t normally use dictionarys and when using for loops i normally use
something like

For i = 1,#List do
end

its just using dictionarys for data storage really cleans up the code

You would just index it normally e.g. PlayerInventory.Bubbles or PlayerInventory["Bubbles"].

Thanks this should help the situation

local PlayerInventory = Inventory:GetAsync(player.UserId) or {Items = {Trails = {"DefultTrail"}, Outfits = {"DefultOutfit"}}, Bubbles = 1000}
print(PlayerInventory["Bubbles"])
local PlayerInventory = Inventory:GetAsync(player.UserId) or {Items = {Trails = {"DefultTrail"}, Outfits = {"DefultOutfit"}}, Bubbles = 1000}
print(PlayerInventory.Bubbles)

Both methods just return nil

They wouldn’t make any difference from one another. They are just two different ways to index something in an array. However, what I said only applies if the GetAsync fails and returns a different array than the one you put in the or statement.

It should definitely work if you just do…

local PlayerInventory = {Items = {Trails = {"DefultTrail"}, Outfits = {"DefultOutfit"}}, Bubbles = 1000}

but this would remove the whole datastore functionality, though. Unless you reset the datastore and then use your original code with my tweaks and use the array which you put in the or statement as the default array if the player doesn’t have any previously saved data.