How to make sound play once then wait till it finishes playing

I have a weapon system that’s using humanoid collided so the sound plays multiple times when I hit someone so how would I make this sound play only once and not be able to play again until its finished I was thinking of using debounce but how would I put that into my script. Any help is appreciated.

Script

local clientCast = require(game.ServerStorage.ClientCast)

local db = false

local slash

for i = -0.9,2.9,.1 do
	local attachment = Instance.new("Attachment",script.Parent.Handle)
	attachment.Name = "DmgPoint"
	attachment.Position = Vector3.new(0,i,0)
end

local casterParams = RaycastParams.new()
casterParams.FilterDescendantsInstances = {script.Parent.Parent.Parent.Character or script.Parent.Parent.Parent.Parent.Character, script.Parent.Handle}
casterParams.FilterType = Enum.RaycastFilterType.Blacklist

local slashCaster = clientCast.new(script.Parent.Handle, casterParams)

slashCaster.HumanoidCollided:Connect(function(result, hithumanoid)
	local Tool = script.Parent
	local sound = Tool.Handle.Hit
	local h = Tool.Parent:FindFirstChild("Humanoid")
		sound:Play()

	local bv = Instance.new("BodyVelocity")
	local offset = Vector3.new()
	bv.MaxForce = Vector3.new(10000000,10000000,10000000)
	bv.Velocity = h.Parent.Head.CFrame.LookVector * 55
	bv.Parent = (hithumanoid.Parent.HumanoidRootPart)
	wait(0.01)
	bv:Destroy()
	if hithumanoid.Parent:FindFirstChild("Effects") then
		if not hithumanoid.Parent.Effects:FindFirstChild("Ragdoll") then
			local z = Instance.new("BoolValue", hithumanoid.Parent.Effects)
			z.Name = "Ragdoll"
			local Debris = game:GetService("Debris")
			Debris:AddItem(z, 3) 
			task.wait(0.1)
		end
	end
end)

script.Parent.Equipped:Connect(function()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	if not slash then slash = humanoid:LoadAnimation(script.Animation); slash.Priority = Enum.AnimationPriority.Action2 end	
end)


script.Parent.Activated:Connect(function()
	local Cooldown = script.Parent.Configuration.Cooldown
	if not db then
		db = true
		slash:Play()
		slashCaster:Start()
		wait(.5)
		slashCaster:Stop()
		wait(Cooldown.Value)
		db = false
	end
end)

script.Parent.Unequipped:Connect(function()
	if slash then slash:Stop() end
	slashCaster:Stop()
end)

as you can see under the humanoid collided event there is a sound that plays but sometimes it plays twice and sometimes it plays once.

Clip

From what I know you can simply do:

--do stuff
sound.Ended:Wait()
--do stuff

Also sorry but I don’t want to add this in the script, I’m too tired right now to do it

1 Like
local clientCast = require(game.ServerStorage.ClientCast)

local db = false
local debounce = false

local slash

for i = -0.9,2.9,.1 do
	local attachment = Instance.new("Attachment",script.Parent.Handle)
	attachment.Name = "DmgPoint"
	attachment.Position = Vector3.new(0,i,0)
end

local casterParams = RaycastParams.new()
casterParams.FilterDescendantsInstances = {script.Parent.Parent.Parent.Character or script.Parent.Parent.Parent.Parent.Character, script.Parent.Handle}
casterParams.FilterType = Enum.RaycastFilterType.Blacklist

local slashCaster = clientCast.new(script.Parent.Handle, casterParams)

slashCaster.HumanoidCollided:Connect(function(result, hithumanoid)
	local Tool = script.Parent
	local sound = Tool.Handle.Hit
	local h = Tool.Parent:FindFirstChild("Humanoid")

	local bv = Instance.new("BodyVelocity")
	local offset = Vector3.new()
	bv.MaxForce = Vector3.new(10000000,10000000,10000000)
	bv.Velocity = h.Parent.Head.CFrame.LookVector * 55
	bv.Parent = (hithumanoid.Parent.HumanoidRootPart)
	wait(0.01)
	bv:Destroy()
	if hithumanoid.Parent:FindFirstChild("Effects") then
		if not hithumanoid.Parent.Effects:FindFirstChild("Ragdoll") then
			local z = Instance.new("BoolValue", hithumanoid.Parent.Effects)
			z.Name = "Ragdoll"
			local Debris = game:GetService("Debris")
			Debris:AddItem(z, 3) 
if not debounce then
debounce = true
	sound:Play()
			task.wait(0.2)
debounce = false
		end
	end
end)

script.Parent.Equipped:Connect(function()
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	if not slash then slash = humanoid:LoadAnimation(script.Animation); slash.Priority = Enum.AnimationPriority.Action2 end	
end)


script.Parent.Activated:Connect(function()
	local Cooldown = script.Parent.Configuration.Cooldown
	if not db then
		db = true
		slash:Play()
		slashCaster:Start()
		wait(.5)
		slashCaster:Stop()
		wait(Cooldown.Value)
		db = false
	end
end)

script.Parent.Unequipped:Connect(function()
	if slash then slash:Stop() end
	slashCaster:Stop()
end)

Sorry for the sucky formatting, I don’t have an indent key on my phone.
But I added some debouncing.

1 Like

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