I am trying to make a plot saving/loading system for my game, it is not close to being done yet, It’s suppose to work kind of like the one in lumber tycoon 2
I Get the error: ServerScriptService.SaveDataController:31: attempt to index nil with ‘GetChildren’
Problem:
for i, Object in pairs(PlayersPlot:GetChildren()) do --Error Here
local ObjectData = {}
if Object.Name ~= "Owner" then
ObjectData = {
["ID"] = Object.ID,
["CFrame"] = Object.CFrame,
["Color"] = Object.Color,
--["NDID"] = Object.NDID
}
table.insert(PlotData, ObjectData)
end
wait()
end
Annoying for loop:
for i, plot in pairs(workspace.Plots:GetChildren()) do
if plot.Owner == plr then
PlayersPlot = plot
end
wait()
end
I know why it gets nil but cant figure out why the for loop won’t redefine the “PlayersPlot” variable
This happens in both the “Save” and “Load” functions
Server script: (In ServerScriptService)
local DataStores = game:GetService("DataStoreService")
local RepStorage = game:WaitForChild("ReplicatedStorage")
local Load = RepStorage.LoadData
local Save = RepStorage.SaveData
local function AssetLookUp(ID) --Asset look up for loading
local Part = nil
if ID == 1 then
Part = RepStorage.Assets.Test
print(Part.Name .. " " .. ID .. " Part was added")
elseif ID == 2 then
Part = nil --More assets soon, just 1 test part for now
else
error("AssetLookUp Error: " .. ID .. " Is not a valid asset")
end
local ClonedPart = Part:Clone()
return ClonedPart
end
Save.OnServerEvent:Connect(function(plr, id)
if workspace.Plots then
local PlotSaves = DataStores:GetDataStore("PlotSaves")
local PlayersPlot = nil
local PlotData = {}
for i, plot in pairs(workspace.Plots:GetChildren()) do
if plot.Owner == plr then
PlayersPlot = plot
end
wait()
end
for i, Object in pairs(PlayersPlot:GetChildren()) do
local ObjectData = {}
if Object.Name ~= "Owner" then
ObjectData = {
["ID"] = Object.ID,
["CFrame"] = Object.CFrame,
["Color"] = Object.Color,
--["NDID"] = Object.NDID,
}
table.insert(PlotData, ObjectData)
end
wait()
end
local success, errorMessage = pcall(function()
PlotSaves:SetAsync(id, PlotData)
print("Saved")
end)
if not success then
print(errorMessage)
end
end
end)
Load.OnServerEvent:Connect(function(plr, id) -- id = 1 to 5 or Max plots
if workspace.Plots then
local PlotSaves = DataStores:GetDataStore("PlotSaves")
local PlayersPlot = nil
local PlotData = PlotSaves:GetAsync(id)
print(PlotData)
for i, plot in pairs(workspace.Plots:GetChildren()) do
if plot.Owner == nil then
plot.Owner = plr
PlayersPlot = plot
end
wait()
end
for i, ObjectData in pairs(PlotData:GetChildren()) do
local Part = AssetLookUp(ObjectData["ID"])
Part.Parent = PlayersPlot
Part.CFrame = ObjectData["CFrame"]
Part.Color = ObjectData["Color"]
wait(0.05)
end
end
end)
Local script: (In starterGui)
local RepStorage = game:WaitForChild("ReplicatedStorage")
local Player = game.Players.LocalPlayer
local Load = RepStorage.LoadData
local Save = RepStorage.SaveData
script.Parent.LoadButton.MouseButton1Click:Connect(function()
Load:FireServer(1) --1 is the id of the save
end)
script.Parent.SaveButton.MouseButton1Click:Connect(function()
Save:FireServer(1)
end)
Let me know if you see anything other wrong other than the main problem too, I have literally no clue how datastores work lol.