I am trying to datastore a table of owned pets for players, but every time a code is redeemed, it does not seem to detect that the entry is in the table, even though it is CLEARLY there as I have printed all the items that are in the table and it is in there about five times now.
local function CheckCode(Player, Code)
if(Codes[Code])then
local Inventory = InventoryStore:GetAsync(Player.UserId.."_Inventory")
if Inventory[Codes[Code][2]] then
return "Already Used"
else
table.insert(Inventory, tostring(Codes[Code][2]))
InventoryStore:SetAsync(Player.UserId.."_Inventory", Inventory)
return "Redeemed"
end
else
return "Invalid Code"
end
end
I do not understand what I am doing wrong, and this is pushing me to the edge of sanity, all help is appreciated.
Can we see your Inventory variable’s array structure? I am perplexed by what you are doing with it, and I may be able to understand more if I can see what the Inventory table represents in its entirety.
Just as @chasedig1 said we need to understand your variables array structure. I can tell you something though, never use SetAsync when updating a players inventory (Stop using SetAsync() to save player data)
local HTTP = game:GetService("HttpService")
-- Censored for reasons
local Codes = HTTP:JSONDecode(CodeStore)
local DSS = game:GetService("DataStoreService")
local InventoryStore = DSS:GetDataStore("Inventory")
local CodeEvent = game.ReplicatedStorage.CodeEvent
local CodeFunction = game.ReplicatedStorage.CodeFunction
local StarterInventory = {"Starter1","Starter2"}
game.Players.PlayerAdded:Connect(function(Player)
local UserData
pcall(function()
UserData = InventoryStore:GetAsync(Player.UserId.."_Inventory")
end)
if UserData == nil then
InventoryStore:SetAsync(Player.UserId.."_Inventory",StarterInventory)
end
end)
You are only inserting the value of Codes[Code][2] into inventory. You are then trying to receive that value, which is much lower down the array tree, which IS nil, because Inventory has been set to, not Inventory[Codes[Code][2]], which you are checking for.
Edit: To be more specific, you are inserting the value of a variable you JSONDecoded earlier, into the bottom level of the array. You are trying to access a value that is a few layers down with your if statement:
I don’t work with tables much (correct me if im wrong) but I believe it’s because you’re doing Codes[Code] and Code is the actual code not a number like 1 (which will find the first entry in the table). Try table.find()
1. local t = {"a", "b", "c", "d", "e"}
2. print(table.find(t, "d")) --> 4
3. print(table.find(t, "z")) --> nil, because z is not in the table
4. print(table.find(t, "b", 3)) --> nil, because b appears before index 3
When I insert this value it shows up in the table as one of the entries after the async is set, which is why I am confused unto why the second time it is called, it is still nil. I am sorry if you explained this already, my brain is quite dead right now as it is three in the morning.
Also, Codes[Code] does work, what it is doing is looking for an entry that is equal to the value provided.
Codes[Code] doesn’t work. It is looking for an entry INSIDE the table at and index that doesn’t exist, and checking that the nonexistent index exists, which it doesn’t, as it is nil.
You inserted a string converted value into the main layer of the table, which has no depth or object properties. Try replacing your table.insert statement with:
Usually, when dealing with deep, confusing arrays, I just tend to just use normal syntax with the brackets. That wouldn’t work because Codes, is just a link to his webserver, containing every code in the game. We’re checking if he already used the code, not if it exists!
Inventory[“Dog”] won’t return a true statement because that is the value, not the index, silly!
The indexes are 1, 2, 3, and so on. Actually, now knowing what your goal is, @MineDevs was right to use table.find()! To find an index with a certain value in it within a table, use this:
table.find(Inventory, “Dog”) to find what index dog is.