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
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
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
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.
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
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
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.
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.