How do i make a script that allows only one player in a certain zone

So I want to make a script where if there’s a player in a zone, another player can’t get into the same zone. I’m pretty new to scripting, so helping me would be much appreciated.

So you need a script in the zone part since I’m assuming you’re using a part.

local zone = script.Parent
local maxDistance = 15 ----you can change this
local currentPlayer = nil
    
local function findPlayer()
   for _,player in pairs(game.Players:GetPlayers()) do
      if player ~= nil then
          local character = player.Character or player.CharacterAdded:Wait()
          local hrp = character:WaitForChild("HumanoidRootPart")
          if (hrp.Position - zone.Position).Magnitude <= 15 then
              if currentPlayer ~= nil then
                  currentPlayer = player
              end
          else
              if currentPlayer == player then
                  currentPlayer = nil
              end
          end
        end
    end
end

while wait() do
    local player = findPlayer()
    if player ~= nil and player == currentPlayer then
        ---- what you want to do with the player ------
    elseif player ~= currentPlayer then
        ---- what you want to do with the player who tresspassed the zone that's already been taken -----
    end
end

1 Like

Thanks for your efforts but the script doesn’t seem to work i’ve tried looking in the output but there’s no errors