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

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

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

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!

3 comments

0

Thanks for the help !

0

Wait what if I want to add more options for it to choose from ??

0

then increase the second parameter of math.random

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)

1 comment

0

You are using math.randomseed 2 times without any math.random in between. Great job of wasting lines in your code.

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