How do I select a new math.random number through each cycle of a repeat loop?

How do I select a new math.random number through each cycle of a repeat loop?

For example, my game mode selector gets glitched and keeps giving me the same game mode every loop. I’m assuming this is because the variable is stuck with the number it is given.

repeat wait()
	local random = math.random(1, 2)
	local selected = random
	if selected == 1 then
               -- code
               wait(600)
        end

	if selected == 2 then
               -- code
               wait(600)
        end

until not game

How do I make it so the variable selected selects a new number? If there is nothing wrong with this code and I am simply unlucky please let me know.

Do you need to keep it a repeat loop? I think a while loop would be better in this scenario:

while true do
    local Random = math.random(1,2)
    local selected = Random
    if selected == 1 then
        --your stuff
        wait(600)
    end

    if selected == 2 then
        --your stuff
        wait(600)
    end
end

Yea its necessary, there are other scripts involved in the game modes as well. I just created a simple script to demonstrate the possible random selection issue.

And if I remember correctly, your issue is that it won’t constantly repeat the random selection?

The issue is when the game modes get selected, say its “Deathmatch”, it keeps giving me deathmatch every round after. I’ve had some successful attempts in the past but now I’m starting to doubt it and think maybe there’s an issue.

Ok, don’t doubt yet, try this:

local numbers = {1,2}
local selected = math.random(1, #numbers)
--repeat loop here
1 Like

I removed the brackets but aside from that, this one works better, thank you.