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

How do I make a random audio play?

I have an SCP game project. In SCP CB random announcements happen randomly so. How do I make random music play randomly can I get a script or something and if someone does where do I put the script and is it a local script or script or module script

7 comments

0

and I don't want it to be looped

0

do you want it to be scary music

0

or just random music from the roblox catalog

0

or do you have the music already picked out?

0

I do have music picked out

0

Use Math.Random() to select a random audio, and have that fill in the number id for a sound in soundservice

0

oh and I only want the audio to play once and not be played again

1 answer

✓ Accepted answer

Alright so one way you can achieve this is by having an array of different sounds,

local Sounds = {
	"rbxassetid://5706135664",
	"rbxassetid://5706135664",
	"rbxassetid://5706135664" 
} --obviously not all of these are gonna have the same ID just replace them with whatever ID's you have

So now that we have a table of all the sound effects that you want to play choosing a random one is very simple, you can just use the math.random function in order to get a random index.

local SoundIndex = math.random(1,3) --this would choose a random number between 1 and 3

Obviously you'd change it from 1,3 to whatever your minimum/maximum is depending on how many sound effects you have, and then after you have gotten your random index you can just create a sound object the normal way you usually do, but this time for the SoundID you'd want to use the table and reference the index from math.random so it'd look something like this

local Sound = Instance.new("Sound") --instance or create the sound object
	Sound.Parent = game.Workspace.part --set its parent
	Sound.SoundId = Sounds[SoundIndex] --and finally use the random index so that your 
	
	Sound:Play()

So once you have all that done, the script should look something like this (Please don't just copy the script, try to read what I said before as it will be more beneficial to you in the long run.)


local Sounds = {
	"rbxassetid://5706135664",
	"rbxassetid://5706135664",
	"rbxassetid://5706135664" 
} --obviously not all of these are gonna have the same ID just replace them with whatever ID's you have

local SoundIndex = math.random(1,3) --this would choose a random number between 1 and 3

local Sound = Instance.new("Sound") --instance or create the sound object
	Sound.Parent = game.Workspace.part --set its parent
	Sound.SoundId = Sounds[SoundIndex] --and finally use the random index so that your 
	
	Sound:Play()

6 comments

0

where do I put this script

1

wherever you want to, I assume you have a base level of scripting knowledge?

0

if you don't just put it in a server script if you want it to play for everyone in workspace and make sure you have a brick named part, if thats where you want it to play from if not just remove Sound.Parent

1

mark as answer if it worked if not reply and tell me what's wrong

0

can I make a lot of the audios and will it play again? because I do not want the audios to play again only once

0

I'm guessing it worked, if it did then mark this as the answer, also you can just use a for i = 1, 10 do loop to do it 10 times for example

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