Having Issues with a plot claiming system

I am making a claiming system for a tycoon game, but I am having issues with it.

Below is the script and its error.

Please keep in mind that I am new to Roblox Lua scripting.

The Script

local Plots = game.Workspace.Plots

local function AddedPlayer(player, otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	
	if humanoid then
		for _, plot in pairs(Plots:GetChildren()) do
			--If the plot is taken
			if plot:GetAttribute("Taken") then
				continue
			end
			--If the plot Is not taken
			plot:SetAttribute("Taken", true)
			plot:SetAttribute("Owner" ..player.UserId)
		end
		
		print("This Plot is Occupied by" ..player.Name)
	end
end

local function RemovingPlayer(player)
	for _, plot in pairs(Plots:GetChildren()) do
		if not plot:GetAttribute("Owner") then
			continue
		end
		
		if player ~= plot:GetAttribute("Owner" ..player.UserId) then
			continue
		end 
		
		plot:SetAttribute("Taken", nil)
		plot:SetAttribute("Owner", nil)
	end
end

for i, player in Players:GetChildren() do
	AddedPlayer(player)
end

game.Players.PlayerAdded:Connect(function(player)
	AddedPlayer(player)
end)

game.Players.PlayerAdded:Connect(function(player)
	RemovingPlayer(player)
end)

The Error
ServerScriptService.AnythingScriptFolder.PlotScript:37: attempt to index nil with 'GetChildren' - Server - PlotScript:37

does replacing Players:GetChildren to Players:GetPlayers() solve the issue?

Unfortunately it does not
It throws out the same error but with GetPlayers()

did you define the Players variable?

local Players = game:GetService("Players")

No I did not. Should I have done that ?

yes you have to either define it

local Players = game:GetService("Players")

Players:GetPlayers()

or use it that way which is used less

game.Players:GetPlayers()

also the script editor should make a red warning when using an undefined variable
{FE84763C-5763-4275-932E-26E9D446CC48}

I did it but
but it’s giving me another error
ServerScriptService.AnythingScriptFolder.PlotScript:5: attempt to index nil with 'Parent' - Server - PlotScript:5

that is because you added 2 parameters to the AddedPlayer function

but you didn’t pass the 2nd argument when calling the function which means that its nil

Oh alright. So where should I add it?
In the PlayerAdded function or The for loop?

the thing is that you don’t need the player character nor the humanoid for that system
does changing the function to this work?

local function AddedPlayer(player)
	
	for _, plot in Plots:GetChildren() do
		--If the plot is taken
		if plot:GetAttribute("Taken") then
			continue
		end
		--If the plot Is not taken
		plot:SetAttribute("Taken", true)
		plot:SetAttribute("Owner" ..player.UserId)
	end
end

This script should fix the issues you’re experiencing:

-- Tested using a server script in ServerScriptService
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local plots = Workspace:WaitForChild("Plots")


local function onPlayerAdded(player: Player)
	for _, plot in plots:GetChildren() do
		if plot:GetAttribute("Taken") then continue end

		plot:SetAttribute("Taken", true)
		plot:SetAttribute("Owner", player.UserId)

		print(`The owner of plot {plot} is now {player.UserId}`)
		break -- Remember to break the loop once a plot is found!
	end
end

local function onPlayerRemoving(player: Player)
	for _, plot in plots:GetChildren() do
		if plot:GetAttribute("Owner") == player.UserId then
			plot:SetAttribute("Taken", nil)
			plot:SetAttribute("Owner", nil)
			break
		end
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

for _, player in Players:GetPlayers() do onPlayerAdded(player) end

Oh
Although I was trying to make the Player owner of the plot only when they touched it
but I will try this

Thanks, but I was trying to make the player owner of the plot only when they touch the plot

I’ll write a new version that does that :slight_smile::+1:


@Legendarydistroyer9

Here’s the new script:

-- Tested using a server script in ServerScriptService
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

local plots = Workspace:WaitForChild("Plots")


local function onPlayerRemoving(player: Player)
	for _, plot in plots:GetChildren() do
		if plot:GetAttribute("Owner") == player.UserId then
			plot:SetAttribute("Owner", nil)
			break
		end
	end
end

Players.PlayerRemoving:Connect(onPlayerRemoving)

for _, plot in plots:GetChildren() do
	if plot:IsA("BasePart") then
		plot.Touched:Connect(function(otherPart: BasePart)
			if plot:GetAttribute("Owner") ~= nil then return end -- Plot is already owned by someone

			local model = otherPart:FindFirstAncestorOfClass("Model")
			if model == nil then return end

			local player = Players:GetPlayerFromCharacter(model)

			if player then
				plot:SetAttribute("Owner", player.UserId)
				print(`The owner of plot {plot} is now {player.UserId}`)
			end
		end)
	end
end

you have to use the Touched event and connect it to the function when its touched
this is an example script (there are comments)

local Players = game:GetService("Players")

local Plots = game.Workspace.Plots

local function AddedPlayer(other :BasePart, plot :BasePart)

	local character :Model = other.Parent

	if not character:FindFirstChild("Humanoid") then
		return -- Exit the function if the touched part isnot a player
	end

	local player :Player = Players:GetPlayerFromCharacter(character) -- this function returns a player from the character

	--same as old code 
	if not plot:GetAttribute("Owner") then
		return 
	end

	if player ~= plot:GetAttribute("Owner" ..player.UserId) then
		return
	end 

	plot:SetAttribute("Taken", nil)
	plot:SetAttribute("Owner", nil)
end

local function RemovingPlayer(player)
	for _, plot in Plots:GetChildren() do
		if not plot:GetAttribute("Owner") then
			continue
		end

		if player ~= plot:GetAttribute("Owner" ..player.UserId) then
			continue
		end 

		plot:SetAttribute("Taken", nil)
		plot:SetAttribute("Owner", nil)
	end
end

--Loops through all plots and connects them to the function
for _,plot :Part in Plots do
	plot.Touched:Connect(function(hit)
		AddedPlayer(hit, plot)
	end)
end

Players.PlayerRemoving:Connect(RemovingPlayer)

I was able to fix it. I thank you both for your help
much appreciation