I am making a kill sound effect but I don't know how to stop it after it plays

For my game I wanted to make a gun system and I made the gun shoot and other features but one I wanted to add is a kill sound effect. Below this is my script where once the humanoid I am shooting goes to 0 or below 0 hp it plays the sound but if I keep shooting the enemy it keeps playing the kill sound effect. I am not entirely sure how to make a system to stop the kill sound effect from playing after a kill. I am new so forgive me if I am making a simple mistake.

local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local RunService = game:GetService("RunService")
local Tool:Tool = script.Parent
local Handle = script.Parent.Handle
local u1 = script.Parent.Union
local u2 = script.Parent.Union2
local ShootPart = script.Parent.Part
local RemoteEvent = Tool:FindFirstChild("RemoteEvent")
local OnCooldown = false
local pe = ShootPart.ParticleEmitter
local sl = ShootPart.SpotLight
local chamber = script.Parent.Chamber
local hud = game.StarterGui.gunhud.hitmarker
local bullet = game.ReplicatedStorage.bullet
local tool = script.Parent
local Debris = game:GetService("Debris")



local Do = {
	Damage = 18;
	Cooldown = 0.3;
	Visualize = true;
	ShootSound = "rbxassetid://5238024665";
}
while true do
	local Origin = ShootPart.Position
	local eject = script.Parent.BulletEjection
	wait(0.01)
	
	
	
RemoteEvent.OnServerEvent:Connect(function(Player,Received)
	if not OnCooldown then
		if chamber.Value > 0 then	
		OnCooldown = true
		task.delay(Do.Cooldown,function()
			OnCooldown = false
		end)	
			chamber.Value = chamber.Value - 1
			local Direction = (Received.Position-Origin).Unit*3000
			local Raycast = workspace:Raycast(Origin,Direction)
			
	

		local Intersection = Raycast and Raycast.Position or Origin + Direction
		local Distance = (Origin - Intersection).Magnitude


		pe.Enabled = true
		sl.Brightness = 28
		wait(0.1)
		pe.Enabled = false
		sl.Brightness = 0
		local beject = bullet:Clone()
		beject.Parent = game.Workspace
		beject.Position = eject.Position
		beject.Rotation = eject.Rotation
		beject.Anchored = false

		Debris:AddItem(beject, 3)
		if (Do.ShootSound ~= "" or Do.ShootSound ~= nil) then
			local Sound = Instance.new("Sound")
			Sound.SoundId = Do.ShootSound
			Sound.Volume = .65
			Sound.PlayOnRemove = true
			Sound.Parent = Handle
			Sound:Destroy()
	

		if Raycast then
			local Hit:Part = Raycast.Instance
			local Humanoid = (Hit.Parent:FindFirstChildOfClass("Humanoid") or Hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))

			if Humanoid and Humanoid.Parent ~= Player.Character then
						Humanoid:TakeDamage(Do.Damage)
						script["HitMarker Sound Effect"]:Play()
						hud.Visible = true
						wait(0.1)
						hud.Visible = false
						if Humanoid.Health <= 0 then
								script["Kill sound"]:Play()	
								script["arsenal kill sound"]:Play()
						
					end
				end
			end
		end
		end
		end
	end)
	end

Any suggestions or help would be great thank you

So to absolutely ensure a sound is not playing anymore do following:

local sound = -- path to sound
sound:Play() -- sound now plays
wait(sound.TimeLength) -- wait until song is done playing
--following steps are to ensure that the sound stops playing
sound:Pause()
sound.Looped = false
sound.Playing = false

this should work

Thank you for the idea but I need the kill sound to work for other players when they die. Maybe I can clone the sound and destroy it for every kill?

Is the sound a person screaming or is it some type of “WinSound”. This is important because of course you can clone the sound but if its just some type of winsound, only the person who actually killed the player/entity should hear this. You will have to play it locally then.

But else, yes, clone it.

local sound = --path to sound
local soundC = sound:Clone()

-- if the death sound is some type of scream from the entity dying make sure to parent the sound to the dead entity, so the sound plays from the entity dying and not from the tool of the player who killed it
-- soundC.Parent = soundOrigin
soundC:Play()
wait(soundC.TimeLength)
soundC:Pause()
soundC.Looped = false
soundC.Playing = false

To awnser your question its like a win sound. Just a question have you ever played popular fps games before? Because when you kill a enemy player a sound plays only once and even if you keep shooting the dead player it won’t play anymore. But if I kill another player it will do the same. So thinking of it I don’t think cloning it can work well. Is there like a way to get the player you killed keep track of it and won’t register the sound anymore until they respawn?

if you want a better idea of what I want I can post a video

First of all, yep I played popular FPS but I meant difference for example between Arsenal and Deadline.

Anyways I looked at your code and found the fatal error you have made a very bad typo which causes an invisible “bug”.

You wrote this:

if Humanoid.Health <= 0 then
	script["Kill sound"]:Play()	
	script["arsenal kill sound"]:Play()
end

Problem is you check if the Humanoids Health is less or the same then zero.
Your script literally only plays the sound when the player/entity is already dead.
This should fix it:

if Humanoid.Health >= 1 then
	script["Kill sound"]:Play()	
	script["arsenal kill sound"]:Play()
end

Edit: If you want to be able to play it even in the most rarest times when the HP is at 0.5 for example you would need to check if the HumanoidStateType is .Dead.

if Humanoid.HumanoidStateType ~= Enum.HumanoidStateType.Dead then
	script["Kill sound"]:Play()	
	script["arsenal kill sound"]:Play()
end
1 Like

Thank you so much I knew it was something like that have a good day :]

You too, and I know typos like this too good. They are the most annoying because the script does not show any output at all.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.