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

How to make a script choose 1 sound to play out of a possible 2?

Asked by
Nidoxs 190
8 years ago

Why won't this work?

while true do
    wait(8.6)
    script.Parent.Explosion:Play() or script.Parent.Explosion2:Play()
end

2 answers

Log in to vote
1
Answered by 8 years ago

depending upon how you want to use this you could move the random number generator inside the while loop Math Random

In your code an "or" is a comparison not a selection e.g a = true or b = true result = true. Take a look at this for more info Operators

while true do
    wait(8.6)

    local ranNumber = genRan(2) -- get a random number

    --select the sound to play
    if ranNumber == 1 then script.Parent.Explosion:Play()
        elseif ranNumber == 2 then script.Parent.Explosion2:Play()
    end
end


--random number genorator
function genRan(maxNum) --input the max number
    return math.random(1, maxNum) -- genorates random number and returns it
end
0
That's a function too complicated - just use math.random alone. Marios2 360 — 8y
0
I kept this in a fuction just in case it was needed multiple times or it can be put into a modular script User#5423 17 — 8y
Ad
Log in to vote
0
Answered by
Marios2 360 Moderation Voter
8 years ago

or is for statements like while, repeat or if:

if script.Parent:FindFirstChild("Humanoid") or foo == 1 then
    --result goes here
end

Here's a very simple solution to your predicament - add a math.random variable, and have an if statement to check it's value and act accordingly:

local rnd -- Place this at the beginning of your script for effeciency. Instead of re-creating the variable every time, it sets it. And yes, you can set a variable as a blank.
while true do
    rnd = math.random(1,2) --math.random, a very important tool for randomization. In here we choose 1 or 2. You can change those numbers to any other, as long as the first is smaller.
    wait(8.6)
    if rnd == 1 then script.Parent.Explosion:Play() else script.Parent["Explosion2"]:Play() --Brackets because the number will be mistaken if not put in as a string

Answer this question