My plot system glitch when two player claim the same plot

Can someone help me to fix the script

My plot system glitch when two player claim the same plot.
Video: 2021-10-12 17-02-34.mp4 - Google Drive

i looked up youtube and cant find result

Here is my script:

PlotSelects localscript in StarterGui:

local TweenService = game:GetService("TweenService")
local PlotService = require(game.ReplicatedStorage.PlotService)

local Player = game.Players.LocalPlayer
local Camera = game.Workspace.Camera

local PlotSelect = script.Parent.Parent:WaitForChild("PlotSelect")
local PlotAction = PlotSelect:WaitForChild("PlotAction")

local Frame = PlotSelect:WaitForChild("Frame")
local Left = Frame:WaitForChild("L")
local Right = Frame:WaitForChild("R")
local Select = Frame:WaitForChild("Select")

local SelectedPlot = Frame:WaitForChild("SelectedPlot")

local Plots = game.Workspace.Simworld.plot

local TI = TweenInfo.new(
	1,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local function camTween(plot)
	local cf = CFrame.new(plot.Position+Vector3.new(0,120,0),plot.Position)
	local tween = TweenService:Create(game.Workspace.Camera,TI,{CFrame = cf})
	tween:Play()
end

local connections = {}

local function startPlotSelect()
	Frame.Visible = true
	Frame.Select.Text = "Select"
	
	Camera.CameraType = Enum.CameraType.Scriptable
    local plotsTable = Plots:GetChildren()

    local index = 1

    SelectedPlot.Value = plotsTable[1]
    camTween(SelectedPlot.Value)

connections[#connections+1] = Right.MouseButton1Click:Connect(function()
	if Plots:FindFirstChild("Plot" .. index-1) then
		index -= 1
	else
		
		index = #Plots:GetChildren()
	end
	
	SelectedPlot.Value = plotsTable[index]
	camTween(plotsTable[index])
end)

connections[#connections+1] = Left.MouseButton1Click:Connect(function()
	if Plots:FindFirstChild("Plot" .. index+1) then
		index += 1
	else
		index = 1
	end
	
	SelectedPlot.Value = plotsTable[index]
	camTween(plotsTable[index])
end)

connections[#connections+1] = Select.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.RequestPlot:InvokeServer(plotsTable[index])
	
	if result then
		Select.Text = "Success"
	else
		Select.Text = "Error"
	end
	
	Camera.CameraType = Enum.CameraType.Custom
	PlotSelect.Enabled = false
	SelectedPlot.Value = nil
		
	for i,v in pairs(connections) do
			if v then
				v:Disconnect()
			end
		end
		
		PlotSelect.Enabled = true
		Frame.Visible = false
		PlotAction.Text = "Sell Plot"
		PlotAction.Visible = true
    end)
end

startPlotSelect()

PlotAction.MouseButton1Click:Connect(function()
	local plot = PlotService:GetPlot(Player)
	
	if plot then
		game.ReplicatedStorage.SellPlot:FireServer()
		PlotAction.Text = "Buy Plot"
		PlotAction.Visible = true
	else
		startPlotSelect()
		PlotAction.Visible = false
	end
end)

PlotServer serverScript in serverScriptService:

local PlotService = require(game.ReplicatedStorage.PlotService)

game.ReplicatedStorage.RequestPlot.OnServerInvoke = function(player,plot)
	
	--Check
	if not plot then return end
	if not plot:IsA("BasePart") then return end
	
	if plot.Occupant.Value ~= nil then end
	plot.Occupant.Value = player
	plot.SurfaceGui.PlayerName.Text = player.Name.."'s Plot"
	player.Character.HumanoidRootPart.CFrame = plot.CFrame + Vector3.new(0, 20 ,0)
	return true
end

game.Players.PlayerRemoving:Connect(function(player)
	PlotService:GiveUpPlot(player)
end)

game.ReplicatedStorage.SellPlot.OnServerEvent:Connect(function(player)
	PlotService:GiveUpPlot(player)
end)

PlotService ModuleScript in replicatedStroage:

local PlotService = {}

local Plots = game.Workspace.Simworld.plot

function PlotService:GetPlot(player)
	for i, plot in pairs(Plots:GetChildren()) do
		if plot.Occupant.Value == player then
			
			return plot
		end
	end
end

function PlotService:GiveUpPlot(player)
	local plot = PlotService:GetPlot(player)
	if plot then
		plot.Occupant.Value = nil
		plot.SurfaceGui.PlayerName.Text = ""
	end
end

return PlotService

ReplicatedStorage:
image

I believe you forgot a return statement in this line. You can just replace it with:

if plot.Occupant.Value ~= nil then return end
1 Like