Script doesn't seem to save or load Datastore

  1. My goal right now is to make my build system save when players place down their objects.The only problem is that my script doesn’t work and I get no errors in the output. I’ve tried to change a few things before but my script doesn’t work. I don’t have a great idea of why it wouldn’t work and I really just want to finish this script. I have had a problem that the script saw something as “nil” but with a few changes I think I fixed it but it still doesn’t work and I don’t get any clues from the output. If anyone has an idea to help fix my script that would be awesome. I would really like to learn from my mistakes if you find any and eventually finish my whole build system.

Here’s my script if anyone can review it or even better set me in the right direction to eventually find a solution:

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

local function GetPlot(plr)
	local plot = nil;
	for i, plt in pairs(workspace.Plots:GetChildren()) do
		if plt then
			if plot.plt.Owner.Value == plr then
				plot = workspace.Plots:FindFirstChild(plt.Name)
				break
			end
		end
	end
	return plot;
end

local function Save(plr)
	local Key = "plr-"..plr.UserId
	local plot = GetPlot(plr)
	
	if plot then
		local save = {}
		
		for i, obj in pairs(plot.PlacedObjects:GetChildren()) do
			if obj then
				table.insert(save, {
					["Name"] = obj.Name,
					
					["CFS"] = {
						["X"] = obj.PrimaryPart.CFrame.X,
						["Y"] = obj.PrimaryPart.CFrame.Y,
						["Z"] = obj.PrimaryPart.CFrame.Z,
						["R"] = obj.PrimaryPart.Orientation.Y
					}
				})
			end
		end
		local success, err = pcall(function()
			PlotSave:SetAsync(Key, save)
		end)
		if not success then
			warn("Plot Data failed to save: "..tostring(err))
			return
		end
	end
end

local function Load(plr)
	local plot = GetPlot(plr)
	
	if plot then
		local Key = "plr-"..plr.UserId
		local savedData
		local success, err = pcall(function()
			savedData = PlotSave:GetAsync(Key)
		end)
		
		if not success then
			warn("Failed to load data: "..tostring(err))
		end
		
		if savedData then
			for i, 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
	end
end

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

Plrs.PlayerAdded:Connect(Load)
Plrs.PlayerRemoving:Connect(Save)
1 Like

ok. what do you get back for the err from the pcall to PlotSave?

throw a few prints here and there, check where the script stops working

The problem is I think the for I, obj in pairs(plot.PlacedObjects:GetChildren()) do…

The script says PlacedObjects is a nil value but it is just a folder in the plot and I don’t know how to fix that…

Ok, so what does plot contain?
and if nil what does Plots contain.
What I am suggesting is go back through the logic and check each value to confirm they are being set as you expect and if not work on solving that and try the whole thing again.

I fixed the nil plot issue… but the script doesn’t seem to save/load the data in the for I, obj loop I believe

Did you confirm you have something in save table before you attempt the PlotSave:SetAsync?