Hello, I am currently having issues with a Datastore. I have a datastore that saves some values to be loaded in that will insert items into a GUI, but it’s bugged. When I first encountered this issue, I thought it was that the values were not saving. That is sometimes the case, but printing usually prints the right loaded value. After spamming print() for some time, I believe I narrowed the issue down to this one function
function module:LoadItems(player)
local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
for i,v in ipairs(inventory) do
local data = nil
local success, errormsg = pcall(function()
print(v.Value, v.Name)
data = invData:GetAsync(player.UserId.. v.Name) or 0
end)
if success then
print(data)
v.Value = data
else
warn(errormsg)
end
end
for i, v in ipairs(inventory) do
print(v.Value)
if v.Value > 0 then
for x = 1, v.Value, 1 do
print(v.Name)
local pet = module.Pets[v.Name]
print(pet.Name)
module:AddItem(player, pet)
end
end
end
end
I am only asking on the DevForum since this code looks fine to me, so I am unaware of the issue. No errors in the output, and all prints print (I think). If you need more code I’ll happily provide it, but all the other code seems to be working. Also, the module is only called from the server, so the script can see ServerStorage. Thanks for reading!
function module:SaveItems(player)
local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
for i,v in ipairs(inventory) do
print(v.Value, invData:GetAsync(player.UserId.. v.Name))
if v.Value ~= 0 and v.Value ~= invData:GetAsync(player.UserId.. v.Name) then
local success, errormsg = pcall(function()
print(v.Value.. " SAVEITEMS")
invData:SetAsync(player.UserId.. v.Name, v.Value)
print("Saved")
end)
if not success then
warn(errormsg)
end
end
end
end
Yes they are being changed, via the module:AddPet() function (ill send if needed), and it is being saved when the player leaves or BindToClose(), and being loaded when they join.
Sure, here’s a video that basically shows what’s going on and everything that is being printed… (btw, the teddy that’s in there is the only one that saves and loads,)
Checking the keys, the only saved key was the Teddy. Changing the save data for the Cat creates 10 cats in the gui (and I did set it to 10), so it is a saving problem.
EDIT: But I don’t know what’s wrong with the saving since I’ve made multiple datastores in the past with this same saving method and they worked, and the pcall() doesn’t return any errors.
No I do not believe so. The only reason why that is there because I only want to save the value if they already hatched the pet before, and if the value changed (thats why the :GetAsync() is there), so I don’t hit the datastore limit if the values are already saved. I’ll remove it and see what happens, and I’ll edit this post.
EDIT: Removing the if statement doesn’t change anything
I think you might need to consider saving in tables instead of having an individual key for each item, I believe your requests are being cached which causes them not to be saved. You should try saving in tables.