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

SOLVED Math.random repeats the number one?

Asked by 4 years ago
Edited 4 years ago

can you guys tell me whats wrong i am having trouble with my math.random it always chooses the number 1 not 2 or 3.

Here is my script:

local random = math.random(1,3)

script.Parent.TextButton.MouseButton1Click:Connect(function()

if random == 1 then
    script.Parent.Picture1.Visible = true
    print(1)
end

if random == 2 then
    script.Parent.Picture2.Visible = true
    print(2)
end

if random == 3 then
    script.Parent.Picture3.Visible = true
    print(3)
end

wait(2)
script.Parent.TextButtonReplay.Visible = true

end)

0
Remember to click accept answer so that the website knows it is solved IncredibleTeamAidan 104 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago

Hi,

What you have done is made it so it chooses the random number once but never again. Plus I also suggest adding a debounce (or DB) to prevent players spam clicking and risk breaking your game/project.

If i have understood what you are after correctly, you need the code to be as follows:

local DB = false
script.Parent.Picture1.Visible = false
script.Parent.Picture2.Visible = false
script.Parent.Picture3.Visible = false
script.Parent.TextButton.Visible = true
-- the above code only happens once, when a new server is created, but is vital for the code to work correctly

script.Parent.TextButton.MouseButton1Click:Connect(function()
    if DB == false then -- this makes sure that it isn't being spam clicked
        DB = true -- this activates the debounce
        local random = math.random(1,3) -- this will pick a random number every time the function is activated
        script.Parent.TextButton.Visible = false -- this is an added layer of anti-spamclick
        if random == 1 then
            script.Parent.Picture1.Visible = true
            print(1)
        elseif random == 2 then
            script.Parent.Picture2.Visible = true
            print(2)
        elseif random == 3 then
            script.Parent.Picture3.Visible = true
            print(3)
        end
    wait(2) -- feel free to change this number to change the amount of time the picture is visible for
    script.Parent.Picture1.Visible = false
    script.Parent.Picture2.Visible = false
    script.Parent.Picture3.Visible = false
    script.Parent.TextButton.Visible = true
    DB= false -- this re-allows clicking
end)

Hope this helps, any other queries, don't hesitate to ask. :)

Note: I sometimes call scripts "code", it means the same thing

0
Thanks man this helped me out a lot i just had to edit the script a little bit and it works. Your amazing! MaxTheKiller120 0 — 4y
0
thanks, remember to click accept answer! IncredibleTeamAidan 104 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

The issue is that you used math.random function outside of the event, so the number that was randomly chosen always stays the same. You can fix this by making the variable global (remove the word 'local') or by putting math.random function inside the event.

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 years ago

When you create the variable "local random" you are storing the value that the math.random() gave only when the script loads. In your script you can avoid it by simply calling math.random inside the mouse event instead of the same value in the variable. That variable random is storing the result of the first math.random() instead of storing the function math.random(). So, make it like this:

script.Parent.TextButton.MouseButton1Click:Connect(function()
    local random = math.random(1,3)
    if random == 1 then
        script.Parent.Picture1.Visible = true
        print(1)
    elseif random == 2 then
        script.Parent.Picture2.Visible = true
        print(2)
    else
script.Parent.Picture3.Visible = true
print(3)
    end
end)

Now the random variable will be re-calculated everytime you click in the textbutton.

Answer this question