Data store script?

Well that’s a load of errors:

ah, edited it the fourth time.
copy paste again.


Seems to be a problem with getting the items in the inventory.

I think I might know a fix to this issue, I’ll let anyone know if it works or not.

make it load the data after it loads the values inside the inventory.
that should fix it.

i edited the script for the fifth time, test it out.

What the hell is going on

image

fix ds.GetAsync to ds:GetAsync

thats not the issue,
if you test it out in your own game using .GetAsync method, it should still work.

1 Like

uh, edited it the sixth time i think? (i lost track)
copy paste again, im really sure this time it will work.

hold on, it finally printed, btw whats the delay all about?

wdym delay?
anyways did it work?

It worked at first with like a 5 minute delay till printing. Then it began erroring again, haven’t tested it today but this is surely a weird situation.

image
I’ve tried to save this with the module code that creates the string value, it printed but didn’t work.

local ds = game:GetService("DataStoreService"):GetDataStore("Inventory")
local buyitem = {}
	function buyitem.buy(item, itemcost, plr)
		local cash = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
		if cash.Value >= itemcost then
			cash.Value -= itemcost
			local obj = Instance.new("StringValue")
			obj.Name = item.Name
			obj.Value = item.Name
		obj.Parent = plr.Inventory
			print(obj.Name)
		game.Players.PlayerAdded:Connect(function()
			local data
			local success, result = pcall(function()
				ds:GetAsync(plr.UserId)
				print("Data has been loaded/saved!" .. obj.Value)
			end)
			if data ~= nil then
				obj.Value = data
				print("Data has been successfully loaded" .. obj.Value)
			end
		end)
		game.Players.PlayerRemoving:Connect(function()
			local success, result = pcall(function()
				ds:SetAsync(plr.UserId, obj.Value)
			end)
			if success then
				print("Lovely success")
			elseif result then
				warn(result)
				warn("Data failed to save! Please try again later.")
			end
		end)
		end
		end 
	

return buyitem

(I’ve also tested this in-game)

the issue is your trying to save data multiple times from different scripts.
a simple fix is using a single script to store all your data, using tables.

once you wrap up the datasaving scripts into a single script, with only one single :SetAsync or .UpdateAsync function, datasaving should work perfectly.

(in simple words, using :SetAsync() multiple times in different scripts when the player is leaving is causing this error)