Datastore duplicating values?

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)

:SetAsync() is set once. It’s handled in the modulescript, and I want it to run the module if it hasn’t been saved yet.

Sure, I’ll research on that, I’ll update you the results.

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?

UpdateAsync is a Get/Set combo, and a lot of times it’s used to compare data before saving it. Like versions or stuff like that.

UpdateAsync also doesn’t um… cache stuff.

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.

Sure, which code specifically? You’ve seen the module before too and I haven’t changed much, lmk what functions you need and I’ll send them.

… the :AddItem() one please.

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

Wouldn’t this duplicate stuff? And I’m pretty sure you’re using this function on loading.

You might wanna have another argument, which doesn’t allow that call to happen idk. Just remove it.

2 Likes

lol I forgot that was there. Idk why I even added it in the first place, I’m pretty sure this is the issue, I’ll remove it and edit this post

edit: im stupid

3 Likes