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