Datastore loading code does not work

The data store script that I made can save data, but not load it, I think something is wrong with this section of the code, but can’t figure out what it is.

game.Players.PlayerAdded:Connect(function(player) 
	local PlayerUserId = "Player_"..player.UserId 
	print(player)

	local Data

	local success, errormessage = pcall(function()
		Data = MyDataStore:GetAsync(PlayerUserId)
	print(Data)
	end)

	if success then
		player:WaitForChild("Inventory"):WaitForChild("Wheat").Value = Data[1][1]   player:WaitForChild("Inventory"):WaitForChild("Wheat_Seeds").Value = Data[1][2]
		player:WaitForChild("Inventory"):WaitForChild("Carrots").Value = Data[2][1] player:WaitForChild("Inventory"):WaitForChild("Carrot_Seeds").Value = Data[2][2]
		player:WaitForChild("Inventory"):WaitForChild("Berries").Value = Data[3][1] player:WaitForChild("Inventory"):WaitForChild("Berry_Seeds").Value = Data[3][2]
		print("Item Data Loaded")
         end
end)
1 Like

Try and see if this works for you: (This will tell you whats wrong in output most likely)


game.Players.PlayerAdded:Connect(function(player) 
	local PlayerUserId = "Player_"..player.UserId 

	local Data

	local success, errormessage = pcall(function()
		Data = MyDataStore:GetAsync(PlayerUserId)
	end)

	if Data then
		player:WaitForChild("Inventory"):WaitForChild("Wheat").Value = Data[1][1]; player:WaitForChild("Inventory"):WaitForChild("Wheat_Seeds").Value = Data[1][2]
		player:WaitForChild("Inventory"):WaitForChild("Carrots").Value = Data[2][1]; player:WaitForChild("Inventory"):WaitForChild("Carrot_Seeds").Value = Data[2][2]
		player:WaitForChild("Inventory"):WaitForChild("Berries").Value = Data[3][1]; player:WaitForChild("Inventory"):WaitForChild("Berry_Seeds").Value = Data[3][2]
		print("Item Data Loaded")
	else
		warn(errormessage)
	end
end)

I put in that code, but nothing is in the output when I enter the game.

Can I see the script that saves the data?

(This saves more than I showed loading in the last script, because I cut off the end part of the loading script to post it)
‘’'lua
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserId = “Player_”…player.UserId
local Inv = player:WaitForChild(“Inventory”)

--Crop data 1
local Farm = game.Workspace.Farm
local FarmTable1 = {}

	print("Don't save crop data")
 --Main code (Crop data 1)
	for i, v in pairs(Farm.Farm_Field_1:GetChildren()) do
		--print(tostring(i)..","..tostring(v))
		if v.Name == "Farm_Tile" then
			if v:GetChildren()[1] then
				FarmTable1[v:GetAttribute("id")] = v:GetChildren()[1].Name
			else
				FarmTable1[v:GetAttribute("id")] = nil
			end
	end
end


--Full table
local Data = 
	{{Inv.Wheat.Value, Inv.Wheat_Seeds.Value}, --Wheat, 1
	 {Inv.Carrots.Value, Inv.Carrot_Seeds.Value}, --Carrots, 2 
     {Inv.Berries.Value, Inv.Berry_Seeds.Value}, --Berries, 3
     FarmTable1,	 --4
     {"placeholder"}, --5
     {Inv.Money.Value,Inv.Level.Value}, --Money, and levels, 6
	{Inv.Music.Value}, --Sound Settings, 7
	{Inv.Exp.Value,Inv.RequiedExp.Value}} --exp (data for levels), 8

print(Data)

local success, errormessage = pcall(function()
	MyDataStore:SetAsync(PlayerUserId, Data)
end)

if success then
	print("Data Saved")
else
	print("Data Saving Error")
	warn(errormessage)
end

end)
‘’’

Some of the code is outside the box, I don’t know why though

The script looks fine only problem is it kinda looks like a mess and sometimes tables won’t save in the same order you make them so I suggest adding key names to them, for example:


local Data = {
		["Wheat"] = {[1] = Inv.Wheat.Value, [2] = Inv.Wheat_Seeds.Value}, --Wheat, 1
		["Carrot"] = {[1] = Inv.Carrots.Value, [2] = Inv.Carrot_Seeds.Value}, --Carrots, 2 
		["Berry"] = {[1] = Inv.Berries.Value, [2] = Inv.Berry_Seeds.Value}, --Berries, 3
		["FarmTable1"] = FarmTable1,	 --4
		["placeholder"] = {"placeholder"}, --5
		["Stats"] = {[1] = Inv.Money.Value, [2] = Inv.Level.Value}, --Money, and levels, 6
		["Sound"] = {[1] = Inv.Music.Value}, --Sound Settings, 7
		["Exp"] = {[1] = Inv.Exp.Value, [2] = Inv.RequiedExp.Value}
} --exp (data for levels), 

Then get them like:


Wheat = Data["Wheat"][1] -- Wheat
["Wheat Seeds"] = Data["Wheat"][2] -- Wheat seeds

All I’m saying is I can’t figure out the problem because the script doesn’t tell me what is where and what does what. Try and organize it a bit. I have to go sleep but trust me this will help a lot to find the bug faster.

1 Like

Thanks for the help, I managed to find the issue, and have fixed the code.

1 Like

Sorry that I couldn’t reply with anything else since it was late at night. At least you found the bug and a fix for it.