Would it be possible to use Instance.new for audio, and how could I?

A while ago, a person told me instead of getting a gun to make a noise by just playing one audio, I can do Instance.new to make it sound more natural.
image
I can see where they’re going with this, but I’m not quite sure how to pull it off.

I am also having trouble parenting the Sound to the handle, and I have concerns if it’ll be laggy, and was wondering if there was a way to delete the sound after it’s added.

Thanks in advance.

Couldn’t you just first parent the sound to the handle, then Play() it afterwards? It could be something like this

local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://SoundIDHere"
Sound.Parent = Gun.Handle
Sound:Play()
1 Like

wow that was simpler than i thought

let’s give 'er a go and see if she set sails

local Handle = script.Parent -- change as needed

function PlaySound(Id)
   local Sound = Instance.new("Sound")
   
   Sound.SoundId = Id
   Sound.Parent = Handle
   Sound:Play()
   Sound.Ended:Wait()
   Sound:Destroy()
end

image

this is an error I never saw before, no idea what is

This means it’s expecting the end of the file, but you put an extra end somewhere.

Go to line 297 and remove an end. simply means end of file, as @MichaelEpicA stated. Rather than being the end of the script, you have typed out end, which is extra and errors the script.

local Audio = Instance.new("Sound", Gun)
Audio.SoundId = "rbxassetid://SoundID"
Audio:Play()
local Handle = script.Parent -- your gun part

function PlaySound(Id)
   local Sound = Instance.new("Sound")
   
   Sound.SoundId = "rbxassetid://" .. Id
   Sound.Parent = Handle
   Sound:Play()
   Sound.Ended:Connect(function()
      Sound:Destroy()
   end
end)

Code from @Galactiq, fixed by me

There’s no difference other than the concatenated string for the SoundId, which could be useful, but I don’t see a point in reposting an entire script for it.