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"
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()
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)
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.