Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Official battle royale project how to add the Randomized Map Script?

Asked by 2 years ago

I am roblox studio newbie, I found the Official battle royale project .

https://education.roblox.com/en-us/resources/battle-royale/project-setup

in the Get the Game Ready page found Randomized Map Script , but I don't know how to add the picknewmap () and GetSpawnLocations() to playermanager module. can anyone help ? thanks.

https://education.roblox.com/en-us/resources/battle-royale/get-the-game-ready

1 answer

Log in to vote
0
Answered by 2 years ago

Below is official random script and player manager script, can help to add random map ?

Randomized Map Script >>
--[[
Setup Notes:
1. In Workspace, create a folder named Maps. Store all parts of a map in individual folders.
2. For each individual map, include a folder named SpawnLocations
3. When starting a match, use pickNewMap() to get a random map. When assigning player spawn points, use
    GetSpawnLocations() to get a table with all locations.
]]

local MapManager = {}

local mapsFolder = workspace.Maps
-- Stores all maps that can be rotated between
local availableMaps = mapsFolder:GetChildren()

-- Stores the current map in play
local activeMap

-- Used to get random maps.
local randomGenerator = Random.new()

-- Gets a random map from the available maps table
function MapManager.pickNewMap()
    local whichMapKey = randomGenerator:NextInteger(1,#availableMaps)
    activeMap = availableMaps[whichMapKey]
    print("New map: " .. activeMap.Name)
end

-- returns a table with the maps current spawn points
function MapManager.getSpawnLocations()
    local spawnPoints = activeMap:FindFirstChild("SpawnLocations")
    local availableSpawnPoints = spawnPoints:GetChildren()
    return availableSpawnPoints
end

return MapManager

Below is official player manager script

local PlayerManager = {}

-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Modules
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))

-- Events
local events = ServerStorage:WaitForChild("Events")
local matchEnd = events:WaitForChild("MatchEnd")

-- Map Variables
local lobbySpawn = workspace.Lobby.StartSpawn
local arenaMap = workspace.Arena
local spawnLocations = arenaMap.SpawnLocations

-- Values
local displayValues = ReplicatedStorage:WaitForChild("DisplayValues")
local playersLeft = displayValues:WaitForChild("PlayersLeft")

-- Player Variables
local activePlayers = {}
local playerWeapon = ServerStorage.Weapon

local function checkPlayerCount()
    if #activePlayers == 1 then
        matchEnd:Fire(gameSettings.endStates.FoundWinner)
        print("Found winner")
    end
end

local function removeActivePlayer(player)
    print("removing player")
    for playerKey, whichPlayer in pairs(activePlayers) do
        if whichPlayer == player then
            table.remove(activePlayers, playerKey)
            playersLeft.Value = #activePlayers
            checkPlayerCount()
        end
    end
end

local function respawnPlayerInLobby(player)
    player.RespawnLocation = lobbySpawn
    player:LoadCharacter()
end

local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()

    local character = player.Character or player.CharacterAdded:Wait()
    -- Give the player a tool
    local sword = playerWeapon:Clone()
    sword.Parent = character

    local humanoid = character:WaitForChild("Humanoid")

    humanoid.Died:Connect(function()
        respawnPlayerInLobby(player)
        removeActivePlayer(player)
    end)
end

local function onPlayerJoin(player)
    player.RespawnLocation = lobbySpawn
end

local function removePlayerWeapon(whichPlayer)
    -- Check to see if a player exist in case they disconnected or left. 
    if whichPlayer then
        local character = whichPlayer.Character

        -- If the player has it currently on their character
        if character:FindFirstChild("Weapon") then
            character.Weapon:Destroy()
        end
        -- If the player has the weapon in their backpack
        if whichPlayer.Backpack:FindFirstChild("Weapon") then
            whichPlayer.Backpack.Weapon:Destroy()
        end
    else
        print("No player to remove weapon")
    end
end


function PlayerManager.sendPlayersToMatch()
    local availableSpawnPoints = spawnLocations:GetChildren()

    for playerKey, whichPlayer in pairs(Players:GetPlayers()) do
        table.insert(activePlayers,whichPlayer)

        -- Gets a spawn location and then removes it from the table so the next player gets the next spawn
        local spawnLocation = availableSpawnPoints[1]
        table.remove(availableSpawnPoints, 1)
        preparePlayer(whichPlayer, spawnLocation)
    end

    playersLeft.Value = #activePlayers
end

function PlayerManager.getWinnerName()
    if activePlayers[1] then
        local winningPlayer = activePlayers[1]
        return winningPlayer.Name
    else
        return "Error: No player found"
    end
end

function PlayerManager.removeAllWeapons()
    for playerKey, whichPlayer in pairs(activePlayers) do
        removePlayerWeapon(whichPlayer)
    end
end

function PlayerManager.resetPlayers()
    for playerKey, whichPlayer in pairs(activePlayers) do
        respawnPlayerInLobby(whichPlayer)
    end

    activePlayers = {}
end

-- Events
Players.PlayerAdded:Connect(onPlayerJoin)

return PlayerManager
Ad

Answer this question