Datastore not loading values.`

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!

3 Likes

So is everything printing as it should be?

1 Like

Most of the time yes, but sometimes when the value is changed to, for example 1, when I leave and rejoin the datastore returns 0.

1 Like

From what you have provided, I can not see any method of saving in this code.

1 Like
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

Here’s the code for saving/

1 Like

Can you try testing this again and screenshot all the prints in your console.

2 Likes

Sure,

Testing ingame btw because studio wont load for some odd reason

1 Like

Are the values actually set to a number apart from 0? Cause from what I can see your saving the value which is 0? Are the numbers actually updating?

Edit: How are you triggering the module to save the data?

1 Like

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.

1 Like

I suspect your saving isn’t working correctly. Could you try testing in studio since it is the only way you can see prints for saving?

Edit: or you can get a friend that has tools to save to join then leave.

1 Like

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,)

https://streamable.com/hb1z52

(Uploaded to streamable since roblox wouldn’t let me upload it here.)

1 Like

Have you tried modifying the Tool counts on the server in studio then leave?

1 Like

Just tried this right now and the values are still 0 :frowning:

1 Like

Do you have the Datastore editor plugin?

1 Like

Yup. Checked it earlier on I forgot what, but the value was 0. I’ll double check all keys right now.

1 Like

Try modifying the keys to a number higher then 0. This will help us narrow down to if it is a saving or loading issue.

2 Likes

Checking the keys, the only saved key was the Teddy. Changing the save data for the Cat creates 10 cats in the gui :smiley: (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.

1 Like

Maybe try removing the if’s that check if the value ~= 0, will this break your script if it has a value of 0?

1 Like

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 :frowning:

1 Like

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.

1 Like