Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Random map loading is not random?

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

✓ Accepted answer

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

1 comment

0

I tried it, but now it just chooses either #4 or #2

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

1 comment

0

It only gets map #1 each time

This discussion is preserved as it was. New answers, comments, and votes are closed.