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

How come my math.random script only does the first if statement?

Asked by 8 years ago
Edited 8 years ago
function onTouched()
    local partchoice = math.random(1,3)
    if partchoice == 1 then
        game.Workspace.Part1:Destroy()
        script.Parent:Destroy()
    end
    if partchoice == 2 then
        game.Workspace.Part2:Destroy()
        script.Parent:Destroy()
    end
    if partchoice == 3 then
        game.Workspace.Part2:Destroy()
        script.Parent:Destroy()
    end
end

script.Parent.Touched:connect (onTouched)

I put this script inside a part, that when touched, picks a random choice to do... but my problem is that is always does the first one. Part 1 is the only one that gets removed every time, and i dont know why :( help anyone? :/

0
Have you checked that it actually comes up with a different random number? GoldenPhysics 474 — 8y

3 answers

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

The reasoning

There is no issue, the only problem is the human mind. Why? Because humans have the power to see patterns in everything1. Something that machines don't have. So even though something is as random as possible it may not seem random at all because we spot a pattern. That's why making something random is so difficult. So roblox has something called a randomseed which is a seed that the pseudo-random generator refers to. Kind of like how minecraft you need a seed to load a certain maps. Equal seeds create equal outcomes.


math.randomseed

Math.random has it's own default seed. Whenever you run it you'll always get the same sequence of numbers.

print(math.random(100))

89

When you restart the game it'll still print

89

How do you get this to change? You change the randomseed.

math.randomseed(2) --changed the seed, it must be a number value.
print(math.random(100))

1

But we return to the original problem, it doesn't stop printing 1 the first time! We need to change the random seed to a never changing number, what's a number that never stops... Time! Time never stops. But how do we get time? tick() or os.time. We'll use tick() because it's easier. tick() is the local or UNIX time. Basically the time on your computer right now. tick() returns the number of seconds since the epoch or 1 January 1970, 00:00:00. So we can make the random seed that value.


Final Product

Please note that math.randomseed does NOT have to be placed at the beginning of the script.

function onTouched()
    math.randomseed(tick())
    local partchoice = math.random(1,3)
    if partchoice == 1 then
        game.Workspace.Part1:Destroy()
        script.Parent:Destroy()
    end
    if partchoice == 2 then
        game.Workspace.Part2:Destroy()
        script.Parent:Destroy()
    end
    if partchoice == 3 then
        game.Workspace.Part2:Destroy()
        script.Parent:Destroy()
    end
end

script.Parent.Touched:connect (onTouched)


Hope it helps!


  1. What is Random? by VSause 

Ad
Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

I have this issue alot, first at the beginning of your script, you should do: math.randomseed(tick()) also, make a variable called like lastpartchoice, and set that to part choice. Then check if partchoice is equal to lastpartchoice, call math.random again. Also, you can replace line 6 - 14 with:

else
            game.Workspace.Part2:Destroy()
            script.Parent:Destroy()
end

and you can make math.random to math.random(2) however you may have ment to destroy part3 when partchoice = 3.

0
@FrostTaco This is a little confusing, could you perhaps give me an example of what it would look like? ThatOneKid7590 45 — 8y
0
Sure, also did you mean for if partchoice == 3 then it would destroy part3 or 2? FrostTaco 90 — 8y
0
That's not it, he's asking why it ALWAYS does the first section EzraNehemiah_TF2 3552 — 8y
0
Ah, never mind you're right. EzraNehemiah_TF2 3552 — 8y
Log in to vote
-1
Answered by 8 years ago

First, IT IS RANDOM WHAT DO YOU WANT FROM IT. Second, can't we have better script like this?? You can use some elseif's, it is not going to blow up your game.

function onTouched()

    local partchoice = math.random(1,3)

    if partchoice == 1 then
        game.Workspace.Part1:Destroy()
        script.Parent:Destroy()
        elseif partchoice == 2 then
        game.Workspace.Part2:Destroy()
        script.Parent:Destroy()
    elseif partchoice == 3 then
        game.Workspace.Part2:Destroy()
        script.Parent:Destroy()
    end
end

script.Parent.Touched:connect (onTouched)

Answer this question