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

Random map loading is not random?

Asked by
Daanni 10
9 years ago

Here I have some code for loading a random map. Every time I launch the server, it loads map #2 (there are 5).

function loadMap(x)
    local maps = Maps:GetChildren()
    for i = 1, #maps do
        if maps[i].Value == x then
            print(maps[i].MapName.Value)
            maps[i].Model.Parent = game.Workspace
        end
    end
end

function loadRandomMap()
    math.randomseed(tick()) 
    local mapNum = math.random(Options.Maps.Value)
    if mapNum == System.CurrentMap.Value then
        math.randomseed(tick()) 
        local mapNum = math.random(Options.Maps.Value)
    end 
    System.CurrentMap.Value = mapNum
    loadMap(mapNum)
end

2 answers

Log in to vote
3
Answered by 9 years ago

Don't call math.randomseed(tick()) in your function. Just call it at the top of your script or else it will choose the same number over and over.

math.randomseed(tick())  --moved it to the start of your script

function loadMap(x)
    local maps = Maps:GetChildren()
    for i = 1, #maps do
        if maps[i].Value == x then
            print(maps[i].MapName.Value)
            maps[i].Model.Parent = game.Workspace
        end
    end
end

function loadRandomMap()
    local mapNum = math.random(Options.Maps.Value)
    if mapNum == System.CurrentMap.Value then
        local mapNum = math.random(Options.Maps.Value)
    end 
    System.CurrentMap.Value = mapNum
    loadMap(mapNum)
end
0
I tried it, but now it just chooses either #4 or #2 Daanni 10 — 9y
Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago
local Updates = 10000
local seeder = coroutine.create( function() for int = 1, Updates do math.randomseed(tick()) coroutine.yield() end end)


coroutine.resume(seeder)--Use this line of code everytime you want to get a random number

Try this instead:

local Updates = 10000
local seeder = coroutine.create( function() for int = 1, Updates do math.randomseed(tick()) coroutine.yield() end end)

function loadMap(x)
    local maps = Maps:GetChildren()
    for i = 1, #maps do
        if maps[i].Value == x then
            print(maps[i].MapName.Value)
            maps[i].Model.Parent = game.Workspace
        end
    end
end

function loadRandomMap()
coroutine.resume(seeder)
    local mapNum = math.random(Options.Maps.Value)
    if mapNum == System.CurrentMap.Value then
coroutine.resume(seeder)
        local mapNum = math.random(Options.Maps.Value)
    end 
    System.CurrentMap.Value = mapNum
    loadMap(mapNum)
end

0
It only gets map #1 each time Daanni 10 — 9y

Answer this question