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 randomize the sounds played? [SOLVED]

I recently added some bullet snap audio to a gun to simulate the sonic boom a bullet makes whenever it travels through the air. The problem is I'd like the sounds to be randomized between two different sounds. The code below only plays ONE particular sound.

local SoundPart1 = Instance.new("Part")
			SoundPart1.Transparency = 1
			SoundPart1.Anchored = true
			SoundPart1.CanCollide = false
			SoundPart1.CFrame = CFrame.new(HitPos)
			SoundPart1.Parent = game.Workspace
			
			local BulletHit = Instance.new("Sound")
			BulletHit.Pitch = 1
			BulletHit.SoundId = SoundId1 --This only plays ONE sound I want to make it random between TWO sounds
			BulletHit.Volume = .6
			BulletHit.Parent = SoundPart1
			BulletHit:Play()
			
			delay(1 / 20, function()
				SoundPart1:Destroy()

This part of the code assigns each "variable" a soundId

local SoundId1 = "http://www.roblox.com/asset/?id=151284431"
local SoundId2 = "http://www.roblox.com/asset/?id=81116827"

3 answers

✓ Accepted answer

So first we need to define our sound IDs. Here's what you have:

local SoundId1 = "http://www.roblox.com/asset/?id=151284431"
local SoundId2 = "http://www.roblox.com/asset/?id=81116827"

This is a good place to start. However, we can make our lives a little bit easier, in case you decide you want to add more. A lot of that text is just repeating ("http://www.roblox.com/aseet/?id=") so we'll put it in a separate variable:

local prefix = "http://www.roblox.com/asset/?id="
local SoundId1 = prefix .. 151284431
local SoundId2 = prefix .. 81116827

You might know that you only want 2 sounds now, but that might change. Or maybe you'll want to use this somewhere else instead.

So we'll use a table (a list) of assets, rather than just stating the 2. So let's rewrite that again:

local prefix = "http://www.roblox.com/asset/?id="
local SoundIds = {prefix .. 151284431, prefix .. 81116827}

Okay, but there's still a bit of repetition. We're doing the prefix .. thing twice, even though we don't need to. We'll leave it alone for now.

So let's stick this code onto what you already wrote (at the top):


local prefix = "http://www.roblox.com/asset/?id="
local SoundIds = {prefix .. 151284431, prefix .. 81116827}

local SoundPart1 = Instance.new("Part")
            SoundPart1.Transparency = 1
            SoundPart1.Anchored = true
            SoundPart1.CanCollide = false
            SoundPart1.CFrame = CFrame.new(HitPos)
            SoundPart1.Parent = game.Workspace

            local BulletHit = Instance.new("Sound")
            BulletHit.Pitch = 1
            BulletHit.SoundId = SoundId1 --This only plays ONE sound I want to make it random between TWO sounds
            BulletHit.Volume = .6
            BulletHit.Parent = SoundPart1
            BulletHit:Play()

            delay(1 / 20, function()
                SoundPart1:Destroy()

Now we want to pick one at random. So what we'll do is first choose an index (which one we want) rather than just picking it. We'll use the built-in math.random function:

soundIndex = math.random( 1, #soundIDs )

Note that #soundIDs means "the length of soundIDs" so in this case it will be 2.

Next, we want the sound at that ID. So we'll just say:

BulletHit.SoundId = soundIDs[soundIndex]

And this will work when we put it together. So here we go:

local prefix = "http://www.roblox.com/asset/?id="
local SoundIds = {prefix .. 151284431, prefix .. 81116827}

local SoundPart1 = Instance.new("Part")
            SoundPart1.Transparency = 1
            SoundPart1.Anchored = true
            SoundPart1.CanCollide = false
            SoundPart1.CFrame = CFrame.new(HitPos)
            SoundPart1.Parent = game.Workspace

            local BulletHit = Instance.new("Sound")
            BulletHit.Pitch = 1

            local soundIndex = math.random( 1, #soundIDs ) -- here's our choosing bit
            BulletHit.SoundId = soundIDs[soundIndex]

            BulletHit.Volume = .6
            BulletHit.Parent = SoundPart1
            BulletHit:Play()

            delay(1 / 20, function()
                SoundPart1:Destroy()

4 comments

0

Thank you for offeringa solution I will look into this another time since I currently don't have my laptop with me.

1

This works but there was an error with line 14: it was supposed to be "local soundIndex" this was the only error

0

Oh, yes, thank you for pointing that out. I have edited the answer to include that correction (for posterity).

0

This works like a charm, and I had just been reading an explanation between the difference of math.random() and math.randomseed() and I thought if randomseed was a possible method of making the sounds random

Hey, I just threw this together, also read the 'Scripting Glossary' section it may help you understand my methods.

-- Variables
local SoundId
local SoundNum = math.random(1, 2)

-- Creates a new Instance/Object called 'Part' and sets its properties
local SoundPart1		= Instance.new("Part", workspace)
SoundPart1.Transparency	= 1
SoundPart1.Anchored		= true
SoundPart1.CanCollide	= false
SoundPart1.CFrame		= CFrame.new(HitPos)

-- Will decide at a random chance which sound it will select
if SoundNum == 1 then
	SoundId = '151284431'
else
	SoundId = '81116827'
end

-- Creates a new Instance/Object called 'Sound' and sets its properties
local BulletHit = Instance.new("Sound", SoundPart1)
BulletHit.SoundId = 'http://www.roblox.com/asset/?id='..SoundId
BulletHit.Volume = 0.6

-- Plays/Runs the 'Sound' object
BulletHit:Play()

-- Destroys the sounds parent along with the 'Sound' object.
delay(1 / 20, function()
	SoundPart1:Destroy()
end)

1 comment

0

Thank you for offering a solution and I will look into this another time as I have no access to my laptop at the moment

local SoundPart1 = Instance.new("Part")
SoundPart1.Transparency = 1
SoundPart1.Anchored = true
SoundPart1.CanCollide = false
SoundPart1.CFrame = CFrame.new(HitPos)
SoundPart1.Parent = game.Workspace
local BulletHit = Instance.new("Sound")
BulletHit.Pitch = 1
local SoundId1, SoundId2 = "http://www.roblox.com/asset/?id=151284431", "http://www.roblox.com/asset/?id=81116827"
for sound = 1, 2 do
local BulletHitSound = math.random(2)
end 
if BulletHitSound == 1 then 
BulletHitSound = SoundId1
else 
BulletHitSound = SoundId2 
end
BulletHit.SoundId = BulletHitSound
BulletHit.Volume = .6
BulletHit.Parent = SoundPart1
BulletHit:Play()
delay(1 / 20, function()
 SoundPart1:Destroy()

If you got questions, just ask what you'd like to ask.

2 comments

0

Line 13 registers as an 'Unknown Global' most likely because it was used in the function RIGHT before it. This I don't know how to fix.

0

Still won't work, it either breaks the gun or no sounds are played.

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