#Randomization only happens once?

1 messages · Page 1 of 1 (latest)

eager belfry
#

with this code:

    if collecting == false then
        collecting = true
        task.delay(1, function()
            if #clashers >= 2 then
                local winner = clashers[math.random(1, #clashers)]
                event:Fire(winner)
                collecting = false
            end
            if #clashers == 1 then
                local winner = clashers[1]
                event:Fire(winner)
                collecting = false
            end
        end)
    end
    table.insert(clashers,player)
end)```

focus on the if #clashers >= 2 part, its supposed to choose a random number between 1 and the amount of players in the array. It only randomizes ONE time and then proceeds to pick that number every single time afterwards. How do I make it roll a different random number every time?
eager nicheBOT
#

studio** You are now Level 5! **studio

elder flax
#

what number does it print?

eager belfry
#

it successfully randomizes the first time

#

but any time after that it always prints the same number that it first rolled

#

so 2, 2, 2, 2, 2 or 1, 1, 1, 1, 1 etc

#

✅ Figured it out, resolved with this code:

    if collecting == false then
        collecting = true
        task.delay(1, function()
            if #clashers >= 2 then
                local winningNumber = math.random(1, #clashers)
                print(winningNumber)
                local winner = clashers[winningNumber]
                event:Fire(winner)
                collecting = false
            end
            if #clashers == 1 then
                local winner = clashers[1]
                event:Fire(winner)
                collecting = false
            end
        end)
    end
    table.insert(clashers,player)
end)```

I guess put math.random in a local variable if you want more than one random number to come out?
languid wave
eager belfry
#

I made a local variable and set it to math.random

#

instead of directly finding the math.random in the array

junior nova
#

add syntax highlight :)

eager belfry
junior nova
#

Color

#

easier to read for us

languid wave
eager belfry
#
clashEvent.OnServerEvent:Connect(function(player)
    if collecting == false then
        collecting = true
        task.delay(1, function()
            if #clashers >= 2 then
                local winningNumber = math.random(1, #clashers)
                print(winningNumber)
                local winner = clashers[winningNumber]
                event:Fire(winner)
                collecting = false
            end
            if #clashers == 1 then
                local winner = clashers[1]
                event:Fire(winner)
                collecting = false
            end
        end)
    end
    table.insert(clashers,player)
end)```
#

like this?

eager belfry
#

in the 2nd one with the local variable it randomizes each time it's called

#

so you get a different roll each time

#

I might be misinforming so ask someone more experienced please

#

but that's what I understood

junior nova
#

What is clashers?

buoyant remnant
#

e.g you thought it was the #clashers >= 2, but actually it was the second one and you weren't adding everyone to the table properly

buoyant remnant
#

prints help a lot

#

guessing achieves nothing, you have to go for proof. prints are often the easiest way to do that