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

Why is math.random the same each time I disable/enable my script?

I've noticed that although math.random is in fact random, I have noticed that the same object gets picked each and every time I disable/enable my script. How can I make math.random more random so that it doesn't replicate upon disabling/enabling the script. Here is what I have

players = game.Players:GetChildren() 
local randomplayer = players[math.random(1, #players)]

2 comments

0

Is there only one player? Lol

0

@k1nq No there is more than one player, this is just an example, I use these same lines for picking weapons, bricks etc. and it always picks the same one when disabling/enabling the script. Its as if Roblox "randomizes" what the script will pick only once and never again, if you get what I am saying.

2 answers

Make a function to retrieve the randomplayer. Then you will make sure it will always be random.

local function GetRandomPlayer()
	local players = game.Players:GetChildren()
	local randomplayer = players[math.random(1, #players)]
	return randomplayer
end

local randomplayer = GetRandomPlayer()

1 comment

0

Sorry, quick question why is there a return randomplayer in there what does it do?

math.random will be the same sequence or a similar one every time. One the wiki you can find a better explanation for this, but a simple solution would be putting math.randomseed(tick()) at the start of the script.

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