Round Robin Spawn Areas

--[[
    -- user: togagames for CoreGames.com and LuaSnippets.com
    
* Make multiple "maps" (platforms) that round robin (spawns) for each new round

* Make "lobby" map that all players start from and go back to at the end of each round


* You will set your spawn objects inside of a group and move that group, 
storing each X (for each new map area). I have them in an X position in the _map array

--]]

local _spawnsId = "2659E0C4D18A947A:Spawns"
local _spawnsFolder = World.FindObjectById(_spawnsId)
local _spawnsFolderPos = _spawnsFolder:GetWorldPosition()
--print(_spawnsFolderPos)
local _map = {} -- name, spawns in folder (x position), 
_map[0] = {"Lobby","0"} -- lobby at pos 0 (ignored in round robin)
_map[1] = {"Map 1","2600"}
_map[2] = {"Map 2","5200"}
_map[3] = {"Map 3","7800"}
_map[4] = {"Map 4","10400"}
_map[5] = {"Map 5","12900"}
local _maps = 5
local _currentMap = _maps

function OnRoundStart()
    _setNextMap()
    print("Map: "..tostring(_map[_currentMap][1]))
end

function OnRoundEnd()
    _setMapToLobby()
end


Game.playerJoinedEvent:Connect(function(player) -- get our player object upon join 
    print(player.team)
end)

Game.roundStartEvent:Connect(OnRoundStart)

Game.roundEndEvent:Connect(OnRoundEnd)

function _setNextMap()
    _currentMap = _currentMap + 1
    if(_currentMap > _maps)then
        _currentMap = 1
    end
    _spawnsFolder:SetWorldPosition(Vector3.New(_map[_currentMap][2],0,0))
    Task.Wait(.4)
    _respawnAll()
end

function _setMapToLobby()
    _spawnsFolder:SetWorldPosition(Vector3.New(_map[0][2],0,0))
    Task.Wait(.4)
    _respawnAll()
end

function _respawnAll() -- Respawn all the players
    local allPlayers = Game.GetPlayers()
    for _, player in ipairs(allPlayers) do
        player:Respawn()
    end
end


Latest Posts