What I want to do is to check to see what plot the player owns so I can load their plot save to their plot. When I tried my script it didn’t work but when I added my print() it has shown me that it sees the owner as nil but when I go into the plots the Owner equals the player’s name. Is there a way to make the if statement run until it gets a certain “answer” back that is not nil?
Here’s my script:
local function GetPlot(plr)
if workspace.Plots.StarterPlot1.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot1
elseif workspace.Plots.StarterPlot2.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot2
elseif workspace.Plots.StarterPlot3.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot3
elseif workspace.Plots.StarterPlot4.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot4
elseif workspace.Plots.StarterPlot5.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot5
elseif workspace.Plots.StarterPlot6.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot6
elseif workspace.Plots.StarterPlot7.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot7
elseif workspace.Plots.StarterPlot8.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot8
elseif workspace.Plots.StarterPlot9.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot9
elseif workspace.Plots.StarterPlot10.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot10
elseif workspace.Plots.StarterPlot11.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot11
elseif workspace.Plots.StarterPlot12.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot12
end
print(Plot)
end
local DS = game:GetService("DataStoreService")
local Plrs = game:GetService("Players")
local PlotSave = DS:GetDataStore("PlotSave")
local Plot
local function GetPlot(plr)
if workspace.Plots.StarterPlot1.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot1
elseif workspace.Plots.StarterPlot2.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot2
elseif workspace.Plots.StarterPlot3.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot3
elseif workspace.Plots.StarterPlot4.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot4
elseif workspace.Plots.StarterPlot5.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot5
elseif workspace.Plots.StarterPlot6.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot6
elseif workspace.Plots.StarterPlot7.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot7
elseif workspace.Plots.StarterPlot8.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot8
elseif workspace.Plots.StarterPlot9.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot9
elseif workspace.Plots.StarterPlot10.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot10
elseif workspace.Plots.StarterPlot11.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot11
elseif workspace.Plots.StarterPlot12.Owner.Value == plr.Name then
Plot = workspace.Plots.StarterPlot12
end
print(Plot)
end
local function Save(plr)
local Key = "plr-"..plr.UserId
if Plot then
local save = {}
for i, obj in pairs(Plot.PlacedObjects:GetChildren()) do
if obj then
print("Attempt table.insert")
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
else
GetPlot()
end
end
local function Load(plr)
if Plot then
print("Plot exists")
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
else
GetPlot()
end
end
game.ReplicatedStorage.BuildEvents.Save.OnServerEvent:Connect(Save)
Plrs.PlayerAdded:Connect(Load)
Plrs.PlayerAdded:Connect(function(plr)
GetPlot(plr)
end)
Plrs.PlayerRemoving:Connect(Save)
local Players = game:GetService("Players")
local Plots = script.Parent.Plots:GetChildren()
local function GivePlot(plr)
for _, Plot in pairs(Plots) do
if Plot then
if Plot.Owner.Value.len then return
elseif string.len(Plot.Owner.Value) ~= 1 or Plot.Owner.Value ~= nil then
Plot.Owner.Value = plr.Name
game.ReplicatedStorage.BuildEvents.GivenPlot:FireClient(plr, Plot)
break
end
end
end
end
game.Players.PlayerAdded:Connect(GivePlot)
Other script:
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 (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 = {}
for i, obj in pairs(Plot.PlacedObjects:GetChildren()) do
if obj then
print("Attempt table.insert")
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
else
GetPlot()
end
end
local function Load(plr)
if Plot then
print("Plot exists")
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
else
GetPlot()
end
end
game.ReplicatedStorage.BuildEvents.Save.OnServerEvent:Connect(Save)
Plrs.PlayerAdded:Connect(Load)
Plrs.PlayerAdded:Connect(GetPlot)
Plrs.PlayerRemoving:Connect(Save)
local players = game:GetService("Players")
local plots = script.Parent.Plots:GetChildren()
local plot
local function GivePlot(plr)
for i, plot in pairs(plots) do
if plot then
if plot.Owner.Value ~= "" then
elseif plot.Owner.Value == "" then
plot.Owner.Value = plr.Name
plot = plot
game.ReplicatedStorage.BuildEvents.GivenPlot:FireClient(plr, plot)
break
end
end
end
end
players.PlayerAdded:Connect(GivePlot)
Idk if this would work but what if this was set in the saving script to make it easier idk
I’ve updated my last reply, try using the update codes, and, sorry for not explaining even a single bit of it, I’am literally the worst on explaining things.