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

Any effective way to randomize maps?

Asked by 5 years ago

I'm currently looking to implement an more effective randomize script for choosing maps. Currently my method will sometimes choose same map over and over again. I'm looking for a method to prevent that from happening. My current script:

local MapsToChoose = {}
for i,v in pairs(game.ServerStorage.Maps:GetChildren()) do
table.insert(MapsToChoose, v.Name)
end

local currentMap = MapsToChoose[math.random(#MapsToChoose)]
local workspace_map = game.ServerStorage.Maps[currentMap]:Clone()
workspace_map.Parent = game.Workspace.CurrentMap
0
if you don't want to run the chance of being selected over and over again, try removing the map once it's chosen from the table, so out of a pool of 5 possible maps and one gets chosen, it will become a pool of 4 possible maps with the chosen map excluded from the table. SupremeBloxx 0 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

General Practice

Use :GetService() to retrieve the ServerStorage

Use :WaitForChild() to make sure something exists before using it

Use workspace rather than game.Workspace

Option 1: Removal from the Table

You can remove map choices from the table temporarily to prevent the same map from being chosen twice. To do this, you'd need an additional empty table which you can place used map names into.

local MapsToChoose = {}
local prevent = {}

local maps = game:GetService("ServerStorage"):WaitForChild("Maps")
for i, v in pairs(maps:GetChildren()) do
    table.insert(MapsToChoose, v.Name)
end

local function MapChoice()
    local random = math.random(1, #MapsToChoose)
    local currentMap = MapsToChoose[random]
    local workspace_map = maps[currentMap]:Clone()
    workspace_map.Parent = workspace:WaitForChild("CurrentMap")

    local count = #prevent + 1
    if count == 1 then
        table.insert(prevent, count, currentMap)
        table.remove(MapsToChoose, random)
    elseif count == 2 then
        table.insert(prevent, count, currentMap)
        table.remove(MapsToChoose, random)
        table.insert(MapsToChoose, (#MapsToChoose + 1), prevent[1])
        table.remove(prevent, 1)
    end
end

MapChoice()

Here, the count == 1 refers to when the table has never had a value in it, so you only need to insert into the prevent table and subsequently remove it from the MapsToChoose table.

After this, the count is always expected to be 2, so we need to do the same thing as above, except also do the reverse, where we reinsert the old chosen map back into the MapsToChoose table.

Option 2: Shuffling the Table

You can also shuffle the table every time the maps are chosen, although this will not preclude the same map from being chosen.

local MapsToChoose = {}
local shuffle = {}

local maps = game:GetService("ServerStorage"):WaitForChild("Maps")
for i, v in pairs(maps:GetChildren()) do
    table.insert(MapsToChoose, v.Name)
end

local function Shuffle()
    local random = math.random(1, #MapsToChoose)
    local currentMap = MapsToChoose[random]
    local workspace_map = maps[currentMap]:Clone()
    workspace_map.Parent = workspace:WaitForChild("CurrentMap")

    for a = 1, #MapsToChoose do
        local random = math.random(1, #MapsToChoose)
        local count = #shuffle + 1
        table.insert(shuffle, count, MapsToChoose[random])
        table.remove(MapsToChoose, random)
    end

    MapsToChoose = shuffle
    shuffle = {}
end

Shuffle()

Above, we simply add all the map names into the shuffle table randomly and then make the MapsToChoose table equal shuffle and reset shuffle to empty.

Ad

Answer this question