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

How do I make a random audio play?

Asked by 4 years ago

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

0
and I don't want it to be looped ConorPodboy 14 — 4y
0
do you want it to be scary music Nervousmrmonkey2 118 — 4y
0
or just random music from the roblox catalog Nervousmrmonkey2 118 — 4y
0
or do you have the music already picked out? Nervousmrmonkey2 118 — 4y
View all comments (3 more)
0
I do have music picked out ConorPodboy 14 — 4y
0
Use Math.Random() to select a random audio, and have that fill in the number id for a sound in soundservice 2Loos 168 — 4y
0
oh and I only want the audio to play once and not be played again ConorPodboy 14 — 4y

1 answer

Log in to vote
2
Answered by
Glacitron 239 Moderation Voter
4 years ago

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()
0
where do I put this script ConorPodboy 14 — 4y
1
wherever you want to, I assume you have a base level of scripting knowledge? Glacitron 239 — 4y
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 Glacitron 239 — 4y
1
mark as answer if it worked if not reply and tell me what's wrong Glacitron 239 — 4y
View all comments (2 more)
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 ConorPodboy 14 — 4y
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 Glacitron 239 — 4y
Ad

Answer this question