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
How do I make a random audio play?
1 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
where do I put this script
wherever you want to, I assume you have a base level of scripting knowledge?
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
mark as answer if it worked if not reply and tell me what's wrong
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
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.

7 comments
and I don't want it to be looped
do you want it to be scary music
or just random music from the roblox catalog
or do you have the music already picked out?
I do have music picked out
Use Math.Random() to select a random audio, and have that fill in the number id for a sound in soundservice
oh and I only want the audio to play once and not be played again