One player cell system

I’m trying to make a one player cell system that will split players between a certain amount of cells. And if it can’t find any available cells it will send them to a different spot (e.g the middle of the block in my case.)

There’s so far been nothing truly that helpful to assist me in this that I’ve found yet. If there’s something that could it would be greatly appreciated if I could have that recourse.

1 Like

You can store cells in a table and track which ones are occupied. When a player joins, loop through the table to find an open cell then assign them to it. Here’s an example:

local Cells = {Cell1, Cell2, Cell3}
local AssignedCells = {}

local function AssignPlayerToCell(player)
    for _, cell in ipairs(Cells) do
        if not AssignedCells[cell] then
            AssignedCells[cell] = player
            player.Character:SetPrimaryPartCFrame(cell.CFrame)
            return
        end
    end
    local fallback = game.Workspace.FallbackSpawn
    player.Character:SetPrimaryPartCFrame(fallback.CFrame)
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        task.wait(1) -- (optional, only use task.wait if it's glitching)
        AssignPlayerToCell(player)
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    for cell, assignedPlayer in pairs(AssignedCells) do
        if assignedPlayer == player then
            AssignedCells[cell] = nil
            break
        end
    end
end)

2 Likes

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