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

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

Why won't this work?

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

2 answers

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

2 comments

0

That's a function too complicated - just use math.random alone.

0

I kept this in a fuction just in case it was needed multiple times or it can be put into a modular script

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

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