Why is my script printing Plot.Owner = nil when the Owner is susposed to equal the player's name?

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

Was “Plot” (The variable) created before that function?

All you need is a variable like this one:

local Plot = – Code goes here

Yes it was created before as

Local Plot

Like plot = function getPlot(plr)
(so on)

What was given to place the “plr” argument, of the function GetPlots()?

It was game.Player.PlayerAdded:Connect(GetPlots)

game.Players.PlayerAdded:Connect(plr)
    GetPlots(plr)
end

Though, there more likely will have more ways to do this…

Now I’m getting attempt to index nil with the plot.woner = plr.Name

Can you please show the full code?

Edit: Currently opening studio for test purposes.

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)

I hope you can help and thanks so far!

Is Plot.Owner being set to anything?

Yes it equals the players name… like for example Plot.Owner.Value = TyPlays_YT
This is set in a different server script…

It is being set in other script, I suppose?

Edit: Try this out:

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)

Mhm here’s the script:

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.

Alright so now the “Plot” in the rest of the script is undefined how can i fix that

What do you mean with “undefined”? You used print() and it gave nil or something? And, did the code have any errors? (The whole code.)

No like the word “Plot” in the script has a blue line under it because Plot is only in the function

I don’t see a blue underline anywhere:

1 Like