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

Why is my ModuleScript returning the same value?

Well I kind of know now but... There is a large module script that controls everything that happens in a round of my game. When choosing a map it always chooses #1 or #2 and never changes. How can I fix this?

GameController.Intermission = function(roundEnd)
	-- unnecessary code removed
	-- Choose map
	local map = GameController.ChooseMap()
	GameController.BeginGame()
end

GameController.ChooseMap = function()
	math.randomseed(tick())
	local number = math.random(3)
	return number
end

-- more functions later

Thanks!

1 answer

✓ Accepted answer

You don't need to set the seed every time

GameController.Intermission = function(roundEnd)
    -- unnecessary code removed
    -- Choose map
    local map = GameController.ChooseMap()
    GameController.BeginGame()
end

math.randomseed(tick())
GameController.ChooseMap = function()
    local number = math.random(3)
    return number
end

-- more functions later

Why? The seed should only be set once, at the start of the game. Any other time is useless, as the seed is probably close enough to the original to be ineffective at choosing random numbers.

2 comments

0

Wow that worked. But shouldn't it still work even if I set it multiple times? Odd but correct answer I guess :D

0

No. If you set it multiple times, you're actually just messing with how Lua handles things internally and making sure that it doesn't do its job properly.

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