Why isn't my for loop running to check the children of PlacedObjects?

I’m making a plot save script and I’ve had to go through a lot of topics to figure out how to fix this script and now my script has no errors but the for loop doesn’t seem to work.

I don’t have an explanation for this problem so I’m looking for someone to guide me to a solution and learn from this mistake or error.

I’ve tried using a print(“Attempt Save”) in the function(Save) and it does not print… Can someone review my script to help fix this?

local DS = game:GetService("DataStoreService")
local Plrs = game:GetService("Players")
local PlotSave = DS:GetDataStore("PlotSave")
local Plot

local function GetPlot(plr)
	for _, StarterPlot in pairs(workspace.Plots:GetChildren()) do
		if plr and (StarterPlot.Owner.Value == plr or plr.Name) then -- If this line doesn't work, then try using just plr.Name
			Plot = StarterPlot
		end
	end
end

local function Save(plr)
	local Key = "plr-"..plr.UserId
	if Plot then
		local save = {}
		print("SAVE = {}")
		for i, Obj in pairs(Plot.PlacedObjects:GetChildren()) do
			print("Attempt Save")
			if Obj then
				print("Attempt table.insert")
				table.insert(save, {
					ObjectName = Obj.Name,
					
					CFrames = {
						X = Obj.PrimaryPart.CFrame.X,
						Y = Obj.PrimaryPart.CFrame.Y,
						Z = Obj.PrimaryPart.CFrame.Z,
						Orientation = Obj.PrimaryPart.Orientation.Y
					}
				})
			end
		end
		
		for _, Saves in pairs(save) do
			for _, a in pairs(Saves) do
				print(a)
			end
		end
		local success, err = pcall(function()
			PlotSave:SetAsync(Key, save)
		end)
		if not success then
			warn("Plot Data failed to save: "..err)
			return
		end
	else
		GetPlot(plr)
	end
end

local function Load(plr)
	if Plot then
		print("Plot Load Exists")
		print("Plot exists")
		local Key = "plr-"..plr.UserId
		local savedData
		local success, err = pcall(function()
			savedData = PlotSave:GetAsync(Key, savedData) -- This one line might be a issue though, I did not change it since we're dealing with DataStoreService.
		end)
		
		if not success then
			warn("Failed to load data: "..tostring(err))
		end
		
		if savedData then
			for _, data in pairs(savedData) do
				if data then
					local serializedModel = game.ReplicatedStorage.BuildObjects:FindFirstChild(data.Name):Clone()
					
					if serializedModel then
						serializedModel.PrimaryPart.Transparency = 1
						
						serializedModel:SetPrimaryPartCFrame(CFrame.new(data.CFS.X, data.CFS.Y, data.CFS.Z) * CFrame.Angles(0, math.rad(data.CFS.R, 0)))
						serializedModel.Parent = Plot.PlacedObjects
					end
				end
			end
		else
			Save(plr)
		end
	else
		GetPlot(plr)
	end
end

game.ReplicatedStorage.BuildEvents.Save.OnServerEvent:Connect(Save)

game.ReplicatedStorage.PlotSave.Event:Connect(GetPlot)
Plrs.PlayerAdded:Connect(Load)
Plrs.PlayerRemoving:Connect(Save)

Are you sure that “Plot.PlacedObjects” has any children to begin with? Try adding a print right after the for loop finishes to see if it ran.

There is a print in there… There are children of the plot when I added them but ti stills doesn’t print

I meant add a print before and after the loop.

Like so:

print(#Plot.PlacedObjects:GetChildren())
for i, Obj in pairs(Plot.PlacedObjects:GetChildren()) do
	print("Attempt Save")
	if Obj then
		print("Attempt table.insert")
		table.insert(save, {
			ObjectName = Obj.Name,
			
			CFrames = {
				X = Obj.PrimaryPart.CFrame.X,
				Y = Obj.PrimaryPart.CFrame.Y,
				Z = Obj.PrimaryPart.CFrame.Z,
				Orientation = Obj.PrimaryPart.Orientation.Y
			}
		})
	end
end
print("The loop has ran")

I just tried that and it prints before and after so Its something inside the loop but I’m not sure what it is

What did this print?

print(#Plot.PlacedObjects:GetChildren())

That printed " table: 0xc1b1f4a7118f4eff

That might be your issue since printing:

print(#Plot.PlacedObjects:GetChildren())

Is giving you a table and not a number, which is weird.

Any ideas to fix that? Please…

When you’re doing this:

Plot.PlacedObjects:GetChildren()

Are you trying to access the variable called local Plot or are you trying to access workspace.Plots like you did in the GetPlot function?

If the first 1, then what exactly is .PlacedObjects?

So I did a function to GetPlot here:

local DS = game:GetService("DataStoreService")
local Plrs = game:GetService("Players")
local PlotSave = DS:GetDataStore("PlotSave")
local Plot

local function GetPlot(plr)
	for _, StarterPlot in pairs(workspace.Plots:GetChildren()) do
		if plr and (StarterPlot.Owner.Value == plr or plr.Name) then -- If this line doesn't work, then try using just plr.Name
			Plot = StarterPlot
		end
	end
end

local function Save(plr)
	local Key = "plr-"..plr.UserId
	if Plot then
		local save = {}
		print("SAVE = {}")
		for i, Obj in pairs(Plot.PlacedObjects:GetChildren()) do
			print("Attempt Save")
			if Obj then
				print("Attempt table.insert")
				table.insert(save, {
					ObjectName = Obj.Name,
					
					CFrames = {
						X = Obj.PrimaryPart.CFrame.X,
						Y = Obj.PrimaryPart.CFrame.Y,
						Z = Obj.PrimaryPart.CFrame.Z,
						Orientation = Obj.PrimaryPart.Orientation.Y
					}
				})
			end
		end
		print("Loop Has RAN!!!")
		print(Plot.PlacedObjects:GetChildren())
		
		for _, Saves in pairs(save) do
			for _, a in pairs(Saves) do
				print(a)
			end
		end
		local success, err = pcall(function()
			PlotSave:SetAsync(Key, save)
		end)
		if not success then
			warn("Plot Data failed to save: "..err)
			return
		end
	else
		GetPlot(plr)
	end
end

local function Load(plr)
	if Plot then
		print("Plot Load Exists")
		print("Plot exists")
		local Key = "plr-"..plr.UserId
		local savedData
		local success, err = pcall(function()
			savedData = PlotSave:GetAsync(Key, savedData) -- This one line might be a issue though, I did not change it since we're dealing with DataStoreService.
		end)
		
		if not success then
			warn("Failed to load data: "..tostring(err))
		end
		
		if savedData then
			for _, data in pairs(savedData) do
				if data then
					local serializedModel = game.ReplicatedStorage.BuildObjects:FindFirstChild(data.Name):Clone()
					
					if serializedModel then
						serializedModel.PrimaryPart.Transparency = 1
						
						serializedModel:SetPrimaryPartCFrame(CFrame.new(data.CFS.X, data.CFS.Y, data.CFS.Z) * CFrame.Angles(0, math.rad(data.CFS.R, 0)))
						serializedModel.Parent = Plot.PlacedObjects
					end
				end
			end
		else
			Save(plr)
		end
	else
		GetPlot(plr)
	end
end

game.ReplicatedStorage.BuildEvents.Save.OnServerEvent:Connect(Save)

game.ReplicatedStorage.PlotSave.Event:Connect(GetPlot)
Plrs.PlayerAdded:Connect(Load)
Plrs.PlayerRemoving:Connect(Save)

PlotObjects is a folder in each plot

Where is that folder created or located? I don’t see you creating any folder? So is it physically in workspace or being stored somewhere else?

Its physically in the workspace in the Plot so you can define it as Plot.Folder but I named it

So try accessing it directly from workspace?

Is this what I whould put because can a script handle this complicated line:

workspace.Plots:FindFirstChild(Plot).PlacedObjects:GetChildren()

Would the variable plot be the name of the players Plot or land?

Also yes it can.

Name of the Plot (Which is a part)

So just do Part.Name or whatever the Players plot is called. Make sure its a string.

The

workspace.Plots:FindFirstChild(Plot).PlacedObjects:GetChildren()

in the output said the PlacedObjects was nil…

Can you test in studio and screenshot the plot folder in workspace and its children. I want to see how you’re naming each plot for each player.