Randomized Sound Not Working

I’m new to scripting and im trying to make a script that makes 1 random sound from a table play but it wont work.

–Here’s The Script

local Workspace = game.Workspace

local soundEffect = {
Workspace.troll1,
Workspace.troll2,
}

Random(soundEffect:Play)

I would be extremely grateful if you would help me out.

1 Like

Get the Instance(audio in this case) from a table by a number(like: soundEffect[1]).
You want it random, so we use math.random(gets a number between a,b).
So 1 obviously and number of how many is in the table by # .
Results:

Code
local randomSong = soundEffect[math.random(1,#soundEffect)]
if randomSong then
      randomSong:Play()
end

If you want a random sound to play, use math.random()

local soundEffect = {
workspace.troll1,
workspace.troll2,
}

function RandomSoundEffect()
   return soundEffect[math.random(1, #soundEffect)]
end

local randomSound = RandomSoundEffect()

randomSound:Play()

Basically, RandomSoundEffect will return a sound effect, by picking a number, 1 to #soundEffect

‘#’ basically returns the number of how many items are in a table

i dont get an error or anything but the sound just doesnt play,
do you have any other suggestions?

saying that your sounds are in workspace, you sure that you’re near them?

sorry about that lol, your fix worked, my audio just wasnt working.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.