Hello!
I’ve been testing with my game, I’m trying to save warehouse data for my game, I’ve been testing this with my friends and my success right now is not the best. Here’s my code, my error this is my error:
(If my screenshot isn’t loading, my error is: attempt to index nil with 'Crates'
on line 103)
Line 103 looks like
if SAVE_Warehouse then
for _, stat in pairs(playerData.Warehouses:GetChildren()) do
saveData.Warehouses[stat.Name] = stat.Value
end
end
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- constants
local PLAYER_DATA = ReplicatedStorage.PlayerData
local PREFIX = "beta2_"
local DATASTORE = DataStoreService:GetDataStore("Data")
local SAVE_Stats = true
local SAVE_Warehouse = true
-- functions
local function LoadData(player)
print("Loading " .. player.Name .."'s data...")
local playerData = script.PlayerData:Clone()
playerData.Name = player.Name
local saveData
local success, err = pcall(function()
saveData = DATASTORE:GetAsync(PREFIX .. tostring(player.UserId))
end)
if success then
print("\tSuccess!")
playerData.Loaded.Value = true
if saveData then
if saveData.Stats then
for stat, value in pairs(saveData.Stats) do
if playerData.Stats:FindFirstChild(stat) then
playerData.Stats[stat].Value = value
end
end
if saveData.Warehouses then
for stat, value in pairs(saveData.Warehouses) do
if playerData.Warehouses:FindFirstChild(stat) then
playerData.Warehouses[stat].Value = value
end
end
end
end
else
print("\tNo save data")
end
else
print("\tUnsafe load")
local errorValue = Instance.new("StringValue")
errorValue.Name = "ERROR_ON_LOAD"
errorValue.Value = err
errorValue.Parent = playerData
end
playerData.Parent = PLAYER_DATA
return playerData
end
local function SaveData(player)
print("Saving " .. player.Name .. "'s data...")
local playerData = PLAYER_DATA:FindFirstChild(player.Name)
if playerData then
if playerData:FindFirstChild("ERROR_ON_LOAD") then
print("\tData unsafe to save, aborting")
return false
end
local tries = 0
local success = false
repeat
tries = tries + 1
print("\tTry #" .. tostring(tries))
success = pcall(function()
DATASTORE:UpdateAsync(PREFIX .. tostring(player.UserId), function(saveData)
if saveData then
if not saveData.Stats then
saveData.Stats = {}
end
if not saveData.Warehouses then
saveData.Warehouses = {}
end
else
saveData = {
Stats = {},
Warehouse = {}
}
end
if SAVE_Stats then
for _, stat in pairs(playerData.Stats:GetChildren()) do
saveData.Stats[stat.Name] = stat.Value
end
end
if SAVE_Warehouse then
for _, stat in pairs(playerData.Warehouses:GetChildren()) do
saveData.Warehouses[stat.Name] = stat.Value
end
end
return saveData
end)
end)
until success or tries == 3
print("successfully saved data")
return success
else
return false
end
end
-- events
script.SaveData.OnInvoke = SaveData
Players.PlayerAdded:connect(function(player)
LoadData(player)
end)
Players.PlayerRemoving:connect(function(player)
SaveData(player)
local playerData = PLAYER_DATA:FindFirstChild(player.Name)
if playerData then
playerData:Destroy()
end
end)
If I can provide extra info about what is all in my “Warehouses” folder, comment, and I can do so! Thank you for helping me if you can.