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)
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")
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)