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)
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)
(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:
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.