Data Store not saving (or loading) properly?

I made a datastore that saves a table through JSONEncode and JSONDecode. For some reason it is not saving (or loading) I dont quite know.

Heres my code

local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local DataStore = DataStoreService:GetDataStore("ItemStorage")
local onplrinv = {}
local oncharinv = {}

game.Players.PlayerAdded:Connect(function(plr)
	local plrequipmentdata = DataStore:GetAsync("plrequipment-"..plr.UserId) or {}
	local charequipmentdata = DataStore:GetAsync("charequipment-"..plr.UserId) or {}
	
	print(table.unpack(HttpService:JSONDecode(DataStore:GetAsync("plrequipment-"..plr.UserId))))
	print(table.unpack(HttpService:JSONDecode(DataStore:GetAsync("charequipment-"..plr.UserId))))
	
	if plrequipmentdata then
		local newData = HttpService:JSONDecode(plrequipmentdata)
		for i,v in pairs(newData) do
			if game.ReplicatedStorage.AllItems:FindFirstChild(v) then
				game.ReplicatedStorage.AllItems:FindFirstChild(v):Clone().Parent = plr.Backpack
			end
		end
	end
	
	if charequipmentdata then
		local othernewData = HttpService:JSONDecode(charequipmentdata)
		for i,v in pairs(othernewData) do
			if game.ReplicatedStorage.AllItems:FindFirstChild(v) then
				if game.ReplicatedStorage.AllItems:FindFirstChild(v):IsA("Shirt") then
					game.ReplicatedStorage.AllItems:FindFirstChild(v):Clone().Parent = plr.Character
					game.ReplicatedStorage.EquipmentFiles.Armor[v]:FindFirstChild(v.."Pants"):Clone().Parent = plr.Character
				else
					game.ReplicatedStorage.AllItems:FindFirstChild(v):Clone().Parent = plr.Character
				end
			end
		end
	end
	

	while wait() do
		if plr.Character then
			for i,v in pairs(oncharinv) do
				table.clear(oncharinv)
				table.remove(oncharinv,i)
			end
			for i, v in pairs(plr.Character:GetChildren()) do
				if v:IsA("Shirt") then
					table.insert(oncharinv,v.Name)
				else if v:IsA("Tool") then
						table.insert(oncharinv,v.Name)
					end
				end
			end
		end
	end
end)

game.Players.PlayerRemoving:connect(function(Player)
	for i, v in pairs(Player.Backpack:GetChildren()) do
		table.insert(onplrinv,v.Name)
	end
	
	print(table.unpack(oncharinv))
	print(table.unpack(onplrinv))
	
	pcall(function()
		DataStore:SetAsync("plrequipment-"..Player.UserId,HttpService:JSONEncode(onplrinv))
		DataStore:SetAsync("charequipment-"..Player.UserId,HttpService:JSONEncode(oncharinv))
	end)
end)

Its made to get the items on the player and when they rejoin it puts them back on, and to get the items in the inventory and when they rejoin put them back in. The script is not getting any errors but the table when I rejoin in empty no matter what I had in my inventory.

1 Like

Change to s,e=pcall(function()

After the pcall, add

if not s then
print(e)
end

test for errors.

1 Like

No errors, and nothing printed. Not quite sure if I did what you asked though.

	s,e=pcall(function()
		DataStore:SetAsync("plrequipment-"..Player.UserId,HttpService:JSONEncode(onplrinv))
		DataStore:SetAsync("charequipment-"..Player.UserId,HttpService:JSONEncode(oncharinv))
	end)
	
	if not s then
		print(e)
	end
1 Like

My mistake. I just realized you’re missing the bind to close function. That’s the problem.

How would I go about doing this? A little confused about the thread you sent.

1 Like

I explain it more here:

You must save the data in the bind to close function for remaining players, and GetAsync that data constantly until it is verified to be correct. It’s a required step in saving data stores.

1 Like

I’m probably doing something wrong here, but Its still not saving.
I’m confused on what pfr is in the newer thread you sent me.
heres my bindtoclose() function

local function BindToClose()
	for key, value in pairs(oncharinv) do
		local result
		local success
		repeat
			task.wait(1)
			success, result = pcall(DataStore.Store.GetAsync, DataStore.Store, key)
			print("Waiting for data to save...")
		until success and result and ((typeof(result)=="table" and rawget(result)==rawget(value)) or result == value)
		--The data should be saved now.
	end
	
	for key, value in pairs(onplrinv) do
		local result
		local success
		repeat
			task.wait(1)
			success, result = pcall(DataStore.Store.GetAsync, DataStore.Store, key)
			print("Waiting for data to save...")
		until success and result and ((typeof(result)=="table" and rawget(result)==rawget(value)) or result == value)
		--The data should be saved now.
	end
end

and here is my player leaving code

game.Players.PlayerRemoving:connect(function(Player)
	for i, v in pairs(Player.Backpack:GetChildren()) do
		table.insert(onplrinv,v.Name)
	end
	
	print(table.unpack(oncharinv))
	print(table.unpack(onplrinv))
	
	BindToClose()
	
	s,e=pcall(function()
		DataStore:SetAsync("plrequipment-"..Player.UserId,HttpService:JSONEncode(onplrinv))
		DataStore:SetAsync("charequipment-"..Player.UserId,HttpService:JSONEncode(oncharinv))
	end)
	
	if not s then
		print(e)
	end
end)

You should be doing game:BindToClose(BindToClose), it’s supposed to add on to what you already have.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.