Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Stopping sounds, before adding to debris ?

function Fire_Gun() 
    if FireSound then
    local ff = Instance.new("Sound")
    local bb = Instance.new("Sound")
    bb.SoundId =""
    ff.SoundId =""
    bb.Volume = 1
    ff.Volume = 1
    bb.Pitch = 1
    ff.Pitch = 1
    bb.Parent = script.Parent.Handle
    ff.Parent = script.Parent.Main
    bb:Play()   
    ff:Play()
    game:GetService("Debris"):addItem(bb,1.3) --here 
    game:GetService("Debris"):addItem(ff,5) -- & here

This script is just from my gun no need to fix anything here just a question on the last 2 lines when my gun fires adds alot of sounds it's good and then adds to debris them, but first i want them to stop the sound and THEN add it to debris, cuz if it doesn't stop before added to debris it still plays the sound so how can i fix that ?

P.S i removed the soundids cuz don't want to show them.

AND NO the wait can't be added it will make the gun delay shoot to the time u put wait()

1 answer

✓ Accepted answer

So I this is an example of how sound can be stopped when using the debris.

This is only an example so you will have to decide how best to use this within your game

local debris = game:GetService('Debris') -- just the service

local soundHolder = Instance.new('Model', game.Workspace) -- we need a holder for the event
soundHolder.Name = 'holdSound'

for i=0, 5 do -- just add some sound, it sound better if you change this to 500
	local tmpSound = Instance.new('Sound', soundHolder)
	tmpSound.Name = 'sound'..tostring(i)
	--tmpSound.SoundId = 'rbxassetid://165065112' -- some cool music :P
	tmpSound.SoundId = 'rbxassetid://304656533' -- I just wanted to change the song
	tmpSound:Play()
	
	debris:AddItem(tmpSound, math.random(1, 60)) -- add sound to the debris 
end

soundHolder.DescendantRemoving:connect(function(ins) -- this will run 'Before' the instance is removed so we can stop it
	print(ins.Name .. ' is being removed  stopping sound')
	ins:Stop()
end)

Hope this helps.

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