Plot Claiming Issue

I’m currently having an issue with my plot-claiming system.

The script below is supposed to ensure that each player can claim only one plot. However, when a player claims a plot, they are still able to claim another one, which is not what I intended.

I’d appreciate it if you could help me figure out what to add or remove to fix this issue.

local Workspace = game:GetService("Workspace")
local PlotFolder = Workspace.Plots
local PlotList = PlotFolder:GetChildren()

for _, plot in PlotList do
	plot.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			if plot:GetAttribute("Taken") ~= nil then
				return
			else
				plot:SetAttribute("Taken", true)
				plot:SetAttribute("Owner", player.Name)

				print(player.Name .. "has claimed this plot")
			end
			print(player.Name .. " has touched the plot")
		end
	end)
end

game.Players.PlayerRemoving:Connect(function(player)
	for _, plot in PlotList do
		if plot:GetAttribute("Owner") == player.Name then
			plot:SetAttribute("Taken", nil)
			plot:SetAttribute("Owner", nil)
			
			print(player.Name .. "has unclaimed this plot")
			
		else if plot:GetAttribute("Owner") ~= player.Name then
				continue
			end
		end
		break
		
	end
end)

Oh, and by the way, I’m using a Part as a placeholder for the plot, regardless of its size, rather than a Model.

i dont see the part where you tried to make it so that players cant claim multiple plots… but a solution i have in mind is simply to also make an attribute or boolvalue in the player when he claims a plot, and check that whenever he touches a new plot

1 Like

Oh, okay! I’ll try it, but I’m new to Roblox scripting.

So if you could show me how to implement it or explain how to do it, I would be very grateful.

same thing you do to check if a plot is owned, except the attribute is in a player and it checks if they already have a plot

for _, plot in PlotList do
	plot.Touched:Connect(function(hit)
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                local hasPlot = player:GetAttribute("OwnsPlot")
		if player and not hasPlot then -- the player is said to not have a plot if ownsPlot is either false or nil
			if plot:GetAttribute("Taken") ~= nil then
				return
			else
				plot:SetAttribute("Taken", true)
				plot:SetAttribute("Owner", player.Name)
                                player:SetAttribute("OwnsPlot", true)
				print(player.Name .. "has claimed this plot")
			end
			print(player.Name .. " has touched the plot")
		end
	end)
end
2 Likes

It worked, thank you very much I appreciate it

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