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

Math.Random only picking 1 option for some reason why ?

Asked by 6 years ago

In my script when I click the image label it should pick between 2 images(I am going to add more). But it only ever picks the first option and never the second. Any reason why ??

plr = script.Parent.Parent.Parent.Parent.Parent
income = script.Parent.income
local choosepunch =math.random(1,2)
--1053468311 -- hitleft
--1053474997 -- hitright


script.Parent.MouseButton1Click:connect(function()
    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
            wait(.5)
            script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
        elseif choosepunch== 2 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
    end
end)

2 answers

Log in to vote
1
Answered by 6 years ago

ChoosePunch is only defined once every game. You want it to chose another number every time clicked. Do the following

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)
    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
            wait(.5)
            script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
        elseif choosepunch== 2 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
    end
end)

Remember, variables get affected by where they are placed.

Hope this helps!

0
Thanks for the help ! 1Messi3903 -5 — 6y
0
Wait what if I want to add more options for it to choose from ?? 1Messi3903 -5 — 6y
0
then increase the second parameter of math.random hiimgoodpack 2009 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Believe it or not, math.random isn't actually random! math.random() produces an equal sequence of numbers according to its seed.

For example, if you have played Minecraft, you've probably seen the option that allows you to put a seed in for your world. Minecraft worlds with the same seed have completely identical worlds.

You can set a new seed every time the game starts by doing math.randomseed(tick()). This sets a seed for the pseudo-random generator.

This should do the trick, hope this helps!

math.randomseed(tick())

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)
    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
            wait(.5)
            script.Parent.Parent.Parent.HitRight.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
        elseif choosepunch== 2 then
            script.Parent.Parent.Parent.HitLeft.Visible= false
            script.Parent.Parent.Visible= false
            wait(.5)
            script.Parent.Parent.Parent.HitLeft.Visible= true
            script.Parent.Parent.Visible= true
            wait(.5)
    end
end)

0
You are using math.randomseed 2 times without any math.random in between. Great job of wasting lines in your code. hiimgoodpack 2009 — 6y

Answer this question