So, I’m basically attempting to make a saving system, to save models players have placed on their plot.
However, I keep getting the error message:
attempt to index nil with ‘PlacedObjects’
Here is my script:
players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataBaseV1")
local plot
local function GetPlot(plr)
for i, plt in pairs(workspace.Plots:GetChildren()) do
if plt then
if plt.Plot.Owner.Value == plr then
plot = workspace.Plots:FindFirstChild(plt.Name)
break
end
end
end
end
local function Save(plr)
local key = "plr-"..plr.UserId
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()
DataStore:SetAsync(key, save)
end)
if not success then
warn("Failed to over-write data( "..tostring(err))
return
end
end
local function Load(plr)
wait(4)
GetPlot(plr)
local key = "plr-"..plr.UserId
local savedData
local success, err = pcall(function()
savedData = DataStore:GetAsync(key)
end)
if not success then
warn("Failed to read data( "..tostring(err))
return
end
if savedData then
for i, data in pairs(savedData) do
if data then
local SaveModel = game.ReplicatedStorage.Models:FindFirstChild(data.Name):Clone()
if SaveModel then
SaveModel.PrimaryPart.Transparency = 1
SaveModel:SetPrimaryPartCFrame(CFrame.new(data.CFS.X,data.CFS.Y,data.CFS.Z) * CFrame.Angles(0, math.rad(data.CFS.R), 0))
end
SaveModel.Parent = plot.PlacedObjects
end
end
else
Save(plr)
end
end
game.ReplicatedStorage.Events.Serialize.OnServerEvent:Connect(Save)
players.PlayerAdded:Connect(Load)
players.PlayerRemoving:Connect(Save)
local function GetPlot(plr)
for i, plt in pairs(workspace.Plots:GetChildren()) do
if plt then
if plt.Plot.Owner.Value == plr then
plot = workspace.Plots:FindFirstChild(plt.Name)
break
end
end
end
to
local function GetPlot(plr)
plot = workspace.Plots:FindFirstChild(plr.Name);--does same thing
end
Tell me if that works, I’m thinking it’s because plot is null.
Why wouldn’t it be, do you create a plot of the player name, and that plot owner is not the actual player? Little weird, ngl. But I’m 100% thinking that plot is null.
Why not just name the model after the players UserId, or Username? It’s easier for you scripting wise. Also, make sure that plot is not null! I would also consider making this object-oriented to save you time in the future.
I also recommend changing your code to something like this so plots are defined in function instead of a global variable.
local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataBaseV1")
local function GetPlot(plr)
local plot = nil;
for i, plt in pairs(workspace.Plots:GetChildren()) do
if plt then
if plt.Plot.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()
DataStore:SetAsync(key, save)
end)
if not success then
warn("Failed to over-write data( "..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 = DataStore:GetAsync(key)
end)
if not success then
warn("Failed to read data( "..tostring(err))
return
end
if savedData then
for i, data in pairs(savedData) do
if data then
local SaveModel = game.ReplicatedStorage.Models:FindFirstChild(data.Name):Clone()
if SaveModel then
SaveModel.PrimaryPart.Transparency = 1
SaveModel:SetPrimaryPartCFrame(CFrame.new(data.CFS.X,data.CFS.Y,data.CFS.Z) * CFrame.Angles(0, math.rad(data.CFS.R), 0))
end
SaveModel.Parent = plot.PlacedObjects
end
end
else
Save(plr)
end
end
end
game.ReplicatedStorage.Events.Serialize.OnServerEvent:Connect(Save)
players.PlayerAdded:Connect(Load)
players.PlayerRemoving:Connect(Save)