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
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 )
BulletHit.SoundId = soundIDs[soundIndex]
BulletHit.Volume = .6
BulletHit.Parent = SoundPart1
BulletHit:Play()
delay(1 / 20, function()
SoundPart1:Destroy()
4 comments
Thank you for offeringa solution I will look into this another time since I currently don't have my laptop with me.
This works but there was an error with line 14: it was supposed to be "local soundIndex" this was the only error
Oh, yes, thank you for pointing that out. I have edited the answer to include that correction (for posterity).
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