Hello, I had an old post that was with this exact datastore, but the problem in that post was resolved, that it wasn’t saving the values. Now it does in fact save, but it duplicates the value. Example: I have 2 dogs in my inventory. I leave and rejoin, and now all of a sudden I got 4. I don’t know what’s causing this. I read on some other topics that it’s caused by a BindToClose function, but I added a check to see if the player has already had their value saved, and it prints true, which is what it should print if the player has already saved their values. Thanks
Code:
Module script that saves the values:
function module:SaveItems(player)
if module.Saved[player.UserId] == false then
local inventory = game.ServerStorage.PlayerValues[player.UserId].Inventory:GetChildren()
local SaveDict = {
["Teddy"] = 0,
["Dragon"] = 0,
["Panda"] = 0,
["Bear"] = 0,
["Deer"] = 0,
["Pig"] = 0,
["Monkey"] = 0,
["Bunny"] = 0,
["Dog"] = 0,
["Cat"] = 0
}
for _, child in pairs(inventory) do
SaveDict[child.Name] = child.Value
end
local success, errormsg = pcall(function()
print(module.Saved[player.UserId])
if module.Saved[player.UserId] == false then
module.Saved[player.UserId] = true
invData:SetAsync(player.UserId,SaveDict)
end
end)
if not success then
warn(errormsg)
else
print("SAVED")
print(module.Saved[player.UserId])
for i,v in pairs(SaveDict) do
print(SaveDict[i])
end
end
end
end
My BindToClose:
local toClose = coroutine.wrap(function()
for _, player in ipairs(game.Players:GetPlayers()) do
if Inventory.Saved[player.UserId] == false then
print("peepeepoopoo")
Inventory:SaveItems(player)
end
end
end)
game:BindToClose(function()
wait()
toClose()
end)
Hey just got time to research on UpdateAsync, and I’m kind of confused on it. In the saving function, how would this apply to a table? Will I need to run the value changer function for every value then assign to the table that will be saved, or do I save the table normally then use UpdateAsync on the table?
Can you please just share the entire code instead? You have a lot of other functions, the problem could be at loading too. Just send the entire thing, as you might have some functions (like I saw before) which could cause some problems.
function module:AddItem(player, pet)
-- print(pet.Name)
for i,v in pairs(module.Pets) do
if pet == v then
local inventoryFrame = player.PlayerGui:WaitForChild("MainUI").PetsFrame.Inventory.Frame
local inventory = game.ServerStorage.PlayerValues:WaitForChild(player.UserId).Inventory:GetChildren()
local guiObject = script.ViewportFrame
local inventoryPets = script.Pets.Normal:GetChildren()
local petValue = nil
for _, object in ipairs(inventory) do
if object.Name == pet.Name then
object.Value += 1
end
end
local newFrame = guiObject:Clone()
local newPet = v:Clone()
newPet.Parent = newFrame
newFrame.Parent = inventoryFrame
end
end
end