Calling part results in error

Hi!
I have this really weird error here.
For some reason, I’m trying to access a part from a NEW script, and it errors.


When I access it from an OLD script, it errors but still works.
It exists in the Workspace clearly because it half works with the Selection script, so what’s wrong?

Script
Build Mode Open

Player:GetAttributeChangedSignal("BuildMode"):Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	local e = game:GetService("TweenService"):Create(Camera, TweenInfo.new(0.75,Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0), {
			CFrame = plot.CamPart.CFrame
	}):Play()
	local succ, err = pcall(function()
		e:Play()
	end)
	if succ then require(game.ReplicatedStorage.Building).Init(player) else warn("Build mode could not start. Error is below.") warn(err) end
end)


local function TweenToPart()
	game.Players.LocalPlayer:SetAttribute("PlayCutscene", false)
	game.Workspace.Camera.CameraType = "Scriptable"
	game:GetService("TweenService"):Create(Camera, TweenInfo.new(0.75, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0), {
		CFrame = PlotsToTweenTo[PlotAt]:WaitForChild("CamPart",1).CFrame
	}):Play()
end

player:WaitForChild("PlayerGui"):WaitForChild("Plot"):WaitForChild("SelectPlot"):WaitForChild("Left").MouseButton1Click:Connect(function()
	if PlotAt == 1 then PlotAt = #PlotsToTweenTo else PlotAt -= 1 end
	TweenToPart()
	player:WaitForChild("PlayerGui"):WaitForChild("Plot"):WaitForChild("SelectPlot"):WaitForChild("chooseplot").Text = "Spawning "..script.Parent.Parent.Name.." (Plot #"..PlotAt..")"
end)

player:WaitForChild("PlayerGui"):WaitForChild("Plot"):WaitForChild("SelectPlot"):WaitForChild("Right").MouseButton1Click:Connect(function()
	if PlotAt >= #PlotsToTweenTo then PlotAt = 1 else PlotAt += 1 end
	TweenToPart()
	player:WaitForChild("PlayerGui"):WaitForChild("Plot"):WaitForChild("SelectPlot"):WaitForChild("chooseplot").Text = "Spawning "..script.Parent.Parent.Name.." (Plot #"..PlotAt..")"
end)

Thanks!
The WaitForChild in the first code block won’t change anything.

I might be wrong, but as far as I understand, “plot” isn’t set to anything.

Is that line 80?

Yes.

Your error means “plot” is nil or in other words is never declared/given an initial value. You need to check the entire script and make sure “plot” is being assigned some value somewhere before this line of code executes otherwise this error will occur.

It is.

local plot = game.Workspace:WaitForChild("Plots"):FindFirstChild(game.Players.LocalPlayer.Name.."'s Plot")

It isn’t finding CamPart.

“Attempt to index nil with CamPart” means “plot” is nil. Does workspace have an instance named “Plots” inside of it?

local plot = game.Workspace:WaitForChild("Plots"):WaitForChild(game.Players.LocalPlayer.Name.."'s Plot")

If you get an infinite yield warning from this then the player’s plot doesn’t exist (or at least it’s incorrectly referenced and has a different name).

Yes, plots are renamed once claimed.
image

Check workspace when testing in studio to make sure they’re being renamed to the expected name of “Player1’s Plot”.

After claiming it it did change.
image

local plot = game.Workspace:WaitForChild("Plots"):WaitForChild(game.Players.LocalPlayer.Name.."'s Plot")

Perhaps this line of code is running before the plot is claimed.

it’s waiting for the LocalPlayer’s plot, should I do something like

repeat task.wait() until game.Workspace:WaitForChild("Plots"):FindFirstChild(game.Players.LocalPlayer.Name.."'s Plot")

?

local plot
for _, aPlot in pairs(workspace:WaitForChild("Plots"):GetChildren()) do
	if aPlot:IsA("Folder") then
		aPlot:GetPropertyChangedSignal("Name"):Connect(function()
			plot = aPlot
		end)
	end
end
1 Like

What script would that go to? As I said, my plot selector works, but not the open script.

local plot = game.Workspace:WaitForChild("Plots"):WaitForChild(game.Players.LocalPlayer.Name.."'s Plot")

Replace this line of code with the code I provided.

image
I

repeat task.wait() until game.Workspace:WaitForChild("Plots"):FindFirstChild(game.Players.LocalPlayer.Name.."'s Plot")

Did you try this repeat until block of code?

Wait your error references line 80 again, which it shouldn’t do (because the code you replaced had fewer lines than the code it was replaced with) is there perhaps other scripts involved?

Yes

I have no idea where I pasted it now, lemme paste it.


The camera tweens, but this happens at the pcall.

Ok, it works!