A Problem with my Save/Load script

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.

Whats plot.Owner looks like some sort of ObjectValue, but you are comparing the Instance and the player object and not the Value of it which cause the playersPlot to be nil.

Oh I did forget to add the .Value to it, but that did not fix the issue.

Well that Indicates that Player is not the owner of any plot,is plot.Owner an ObjectValue or a StringValue? Try debugging by adding some print statements so you could find where you wnet wrong

It’s an ObjectValue, and I tried debugging it already with no luck

for i, plot in ipairs(workspace.Plots:GetChildren()) do
                        print(plot.Owner.Value,plr)
			if plot.Owner.Value == plr then
				PlayersPlot = plot
			end
			wait()
		end

Send me the output logs for this print statement

It Returns exactly what the script needs, Owner nuexr

Its supposed to print the Owner name and the player name and not “Owner”, can you check the script again do it.

They are both different

If I click Load: Nil, nuexr
and if I click save after I click load it Returns: nuexr neuxr

I think this happens since the owner is set after the plot loads
Not sure if Owner.Value being Nil is making it not work

Owner.Value has to be the player in order to work , being it nil the if statement won’t run.

Why does it error still? (It still returns the same error as before)

Cause PlayersPlot becomes nil, I can’t help further cause I have no Idea how you are handling your plot ownership.

Can you show us the full script for this? If there’s a chance that PlayersPlot might be nil, why not add an if statement? What exactly is “PlayersPlot” and what information or objects does it contain?

Also sidenote, I’d advise not to rely on the client to update and save the players data. I recommend keeping loading and saving within a serverscript using the PlayerAdded and PlayerRemoving connections. If you want to keep your current method, I would also advise you add a debounce as any exploiter can just spam fire the remote and cause your datastore to hit rate limits.