Weird error with plot claiming system

I am working on a tycoon game and currently creating a plot-claiming system for it. The script below works, but I keep getting a weird error. I tried to ignore it, but I have a feeling it will come back to haunt me later.

The Script

local plotFolder = game.Workspace:FindFirstChild("Plots")
local plotList = plotFolder:GetDescendants()

for _, plot in plotList do
	if plot:IsA("Model") then
		local plotdoor = plot:FindFirstChild("PlotDoor")
		if plotdoor and plotdoor:IsA("BasePart") then
			plotdoor.Touched:Connect(function(hit)
				local character = hit.Parent
				local player = game.Players:GetPlayerFromCharacter(character)
				local ownertag = player:GetAttribute("OwnerTag")
				
				if player and not ownertag then
					if plot:GetAttribute("Taken") ~= nil then
						print(player.Name .. " This Plot Is Not Avaliable")
						return
					else
						plot:SetAttribute("Taken", true)
						plot:SetAttribute("Owner", player.UserId)
						player:SetAttribute("OwnerTag", true)
						print(player.Name .. " has claimed the plot")
					end
				end
			end)
		end
	end
end

game.Players.PlayerRemoving:Connect(function(player)
	for _, plot in plotList do
		if plot:GetAttribute("Owner") == player.UserId then
			plot:SetAttribute("Taken", nil)
			plot:SetAttribute("Owner", nil)
			print(player.Name .. " Has Left The Plot")
		elseif plot:GetAttribute("Owner") ~= player.UserId then
			return
		end
	end
end)

The Error

ServerScriptService.AnythingScriptFolder.PlotSystem:11: attempt to index nil with 'GetAttribute' - Server - PlotSystem:11

Try running the code yourself and see for yourself. I have a feeling that the error is coming from the player’s accessory, but I might be wrong. Even if I’m right, I still don’t know how to fix it.

Players:GetPlayerFromCharacter is a function that can return nil. You are assuming player exists here, since there aren’t any checks checking if it is nil. Therefore, whenever any character touches the plot who’s not a player that error will appear, since player is nil.

Add a guard statement to exit out of the function (via a return) if the player doesn’t exist.

local player = game.Players:GetPlayerFromCharacter(character)

if not player then
	return
end

Noticed this piece of code:

This is supposed to make the plots the players owns to become unowned, correct? That second check with the inequality ~= comparison has a return statement, which means the first time it encounters a plot which isn’t this player’s, the entire function stops. This will result in certain plots not being properly unclaimed once the player leaves. Just remove that second check and it should be fine.

1 Like

So is this ok?

 local character = hit.Parent
				local player = game.Players:GetPlayerFromCharacter(character)
				if not player then return end -- the gaurd statement
				
				local ownertag = player:GetAttribute("OwnerTag")

Alright I will, thank you

1 Like

Yes. One-liner guard statements are also okay if they’re your preference.

1 Like

Ok, thank you very much.
I appreciate your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.