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

How do I make a one sound play randomly at random times?

Asked by 6 years ago

So I want a background ambient sound to play at random times, can someone help me for the script, it has to be played at random and the minimum gap is around 30 seconds, and remember I want the same ID to be played.

Help will be very appreciated! :)

1
Learn on wiki.roblox.com ... http://wiki.roblox.com/index.php?title=Random_numbers arshad145 392 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

math.random() is your friend. It can generate random numbers for you. It's quite easy to use.

math.random(min, max)

So in your case to generate a random number from 30 to, let's say, 60:

math.random(30,60)

You want to use these numbers as delays. For that you need the wait function.

wait(seconds)

Putting it together:

wait(math.random(30,60))

I'm going to assume that you already have a sound instance in a variable called ambient. Here's how to play it.

ambient:Play()

Finally, you should put all this in an (infinite in this example) loop, so it doesn't just play once. Let's use a while loop.

while CONDITION do

end

Putting it all together:

while true do
    wait(math.random(30,60))
    ambient:Play()
end
Ad

Answer this question