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

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

I put a photo in my post above

Maybe you’re doing it wrong.

Alright I’ll try that in a second

Alright it says the plot exists :smiley:! But now I’m going to see if it saves…

Edit:

It didn’t save… can you maybe help me figure that out too… if you can that would mean the world to me!!

2 Likes

Is there a DataStore Issue?
sorry I only skimmed the convo and didn’t really read it

I think it said a DataStore timeout or its like overfilled

1 Like

how much requests are you giving?

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
			local Plot = StarterPlot
		end
	end
end

local function Save(plr)
	local Key = "plr-"..plr.UserId
	if Plot then
		local save = {}
		
		for _, PlacedObject in pairs(Plot.PlacedObjects:GetChildren()) do
			if PlacedObject then
				print("Attempt table.insert")
				table.insert(save, {
					ObjectName = PlacedObject.Name,
					
					CFrames = {
						X = PlacedObject.PrimaryPart.CFrame.X,
						Y = PlacedObject.PrimaryPart.CFrame.Y,
						Z = PlacedObject.PrimaryPart.CFrame.Z,
						Orientation = PlacedObject.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 exists")
		local Key = "plr-"..plr.UserId
		local savedData
		local success, err = pcall(function()
			savedData = PlotSave:GetAsync(Key) -- 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)

Plrs.PlayerAdded:Connect(Load)
Plrs.PlayerAdded:Connect(GetPlot)
Plrs.PlayerRemoving:Connect(Save)

It’s morning already for me, and I have to go, I will only be able to help you tomorrow… And, I don’t mess with DataStoreService that much, so I know almost nothing of it…

Now its still saying on and off Plot.Owner = nil

Sorry, that wasn’t the issue. Its still the main issue

Oh wait! Tjis is in my game’s output: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = plr-108067676

Sorry I know it’s the morning for you but if you are able to help later on the problem is during the Save function when it’s for_, PlacedObject in pairs(Plot.PlacedObjects:getChildren()) do, it doesn’t seem to save but there is no error so that good but the children of the folder don’t save when I rejoin

You’ve got a race condition where two scripts are both doing actions upon a player joining. You have no idea which order these will occur in.

Either perform a loop until plot is not nil, or restructure your code to avoid the race condition.

To make your code easier to read, consider using a for loop instead of all the conditional statements:

local plot
for _, p in ipairs( game.Workspace.Plots:GetChildren() ) do
    if p.Owner.Value == plr.Name then
        plot = p
        break
    end
end

But the main issue is likely the race condition.

Another option to avoid the race condition would be to use BindableEvents. Having a BindableEvent Fire once the player has been assigned a plot will allow you to hook into the event in another script and know for certain that the plot has been allocated without any arbitrary waits or loops. This is probably preferred if you are unable to restructure code into a single server script.

I fixed that problem but now the load function doesn’t load…