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

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

Asked by
Seyfert 90
7 years ago

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)]
0
Is there only one player? Lol k1nq 30 — 7y
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. Seyfert 90 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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()
0
Sorry, quick question why is there a return randomplayer in there what does it do? UltraUnitMode 419 — 7y
Ad
Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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.

Answer this question