Загрузка данных


local Players = game:GetService("Players")
local spawnFolder = workspace:WaitForChild("SpawnPoints")

local minDistance = 8 -- минимальная дистанция между игроками

-- получаем все точки спавна
local function getSpawns()
	local points = {}
	for _, obj in ipairs(spawnFolder:GetChildren()) do
		if obj:IsA("BasePart") then
			table.insert(points, obj)
		end
	end
	return points
end

-- проверка: можно ли сюда ставить игрока
local function isFarFromOthers(pos)
	for _, plr in ipairs(Players:GetPlayers()) do
		if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
			local otherPos = plr.Character.HumanoidRootPart.Position
			if (otherPos - pos).Magnitude < minDistance then
				return false
			end
		end
	end
	return true
end

local function spawnCharacter(character)
	local root = character:WaitForChild("HumanoidRootPart")
	local points = getSpawns()
	if #points == 0 then return end

	-- пробуем случайные точки, пока не найдем свободную
	for i = 1, #points do
		local point = points[math.random(1, #points)]
		local pos = point.Position + Vector3.new(0, 3, 0)
		if isFarFromOthers(pos) then
			root.CFrame = CFrame.new(pos)
			return
		end
	end

	-- если все заняты, ставим просто в случайную
	local fallback = points[math.random(1, #points)]
	root.CFrame = CFrame.new(fallback.Position + Vector3.new(0, 3, 0))
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		task.wait(0.1)
		spawnCharacter(character)
	end)
end)