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

Script that does math.random not working with 3 or more items, why ?

On this code once you click the imagelabel it should pick a random image to show but it doesnt work with 3 or more and only works with 2 options.

plr = script.Parent.Parent.Parent.Parent.Parent
income = script.Parent.income
--1053468311 -- hitleft
--1053474997 -- hitright

math.randomseed(tick() - wait() % wait() ^ wait()) --just some randomseed to randomize math.randoms
script.Parent.MouseButton1Click:connect(function()
    local choosepunch = math.random(1, 2, 3)
    plr.leaderstats.Hits.Value = plr.leaderstats.Hits.Value + income.Value
    if choosepunch==1 then
            script.Parent.Parent.Parent.HitRight.Visible= false
            script.Parent.Parent.Visible= false
			script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
			script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
        elseif choosepunch== 2 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
			script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
            script.Parent.Parent.Visible= true
			script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
        elseif choosepunch== 3 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
			script.Parent.Parent.Parent.HitRight.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
			script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
    end
end)

1 answer

✓ Accepted answer

The reason this is not working is because you are giving math.random() 3 arguments. This function only takes 2 arguments and it chooses a random number from those two numbers+anything in between. so

--instead of
math.random(1,2,3)
--which is incorrect

--this would be correct
math.random(1,3)
--this gives you either 1, 2, or 3

So to correct your code we need to give that function 2 arguments.

plr = script.Parent.Parent.Parent.Parent.Parent
income = script.Parent.income
--1053468311 -- hitleft
--1053474997 -- hitright

math.randomseed(tick() - wait() % wait() ^ wait()) --just some randomseed to randomize math.randoms
script.Parent.MouseButton1Click:connect(function()
    local choosepunch = math.random(1, 3)--two arguments
    plr.leaderstats.Hits.Value = plr.leaderstats.Hits.Value + income.Value
    if choosepunch==1 then
            script.Parent.Parent.Parent.HitRight.Visible= false
            script.Parent.Parent.Visible= false
            script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
            script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
        elseif choosepunch== 2 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
            script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
            script.Parent.Parent.Visible= true
            script.Parent.Parent.Parent.Uppercut.Visible= false
            wait(.5)
        elseif choosepunch== 3 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
            script.Parent.Parent.Parent.HitRight.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
            script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
    end
end)

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