Issue with coming up with a debounce

im using the basic sword script from the toolbox with a few modifications, the blow function is connected through a .touched event, but im having trouble with debouncing the count, and it makes the audio play way more times than necessary, i tried adding a task.wait, it sort of helps but unsure as it goes off of the body parts it hits, it counts multiple times as well if i wiggle into other body parts, any help would be appreciated!

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)
	if humanoid.Died then
		task.wait(0.5)
		count = count+1
		print(count)
		if count == 1 then
			Sounds.Kill1:Play()
		elseif count == 2 then
			Sounds.Kill2:Play()
		elseif count == 3 then
			Sounds.Kill3:Play()
		elseif count == 4 then
			Sounds.Kill4:Play()
		elseif count == 5 then
			Sounds.Kill5:Play()
		elseif count >= 5 then
			Sounds.Kill5:Play()
		end
	end
end

Just making sure, the sound plays when the target dies, correct?

yes, it does play, i made sure of that before anything else

If so, Roblox’s Humanoid.Died event can be quite buggy somtimes, firing off multiple times at once. I would personally set up a local debounce for every player that dies, so when that specific player dies, it will check if the debounce for that specific player is false, if so, continue and play the sound. I’ll show you what I mean, just gotta add onto your function. Give me a moment :sweat_smile:

Something like this would work. Let me know if it does, or if you have any issues! :smile:

local debounce = {}

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	TagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)
	
	if humanoid.Died then
		if not debounce[humanoid.Parent.Name] then
			debounce[humanoid.Parent.Name] = true
			delay(3, function()
				debounce[humanoid.Parent.Name] = false
			end)
			count = count+1
			print(count)
			if count == 1 then
				Sounds.Kill1:Play()
			elseif count == 2 then
				Sounds.Kill2:Play()
			elseif count == 3 then
				Sounds.Kill3:Play()
			elseif count == 4 then
				Sounds.Kill4:Play()
			elseif count == 5 then
				Sounds.Kill5:Play()
			elseif count >= 5 then
				Sounds.Kill5:Play()
			end
		end
	end
end

ill try it out and see if that works, thank you for typing that all up LOL

i noticed something, ill try to see if i can explain it, but, if i slash and then swipe into them or wiggle alternatively, i notice it doesnt play the sound or count, but it did stop from playing multiple times and touching more times than it needed to, i can send a video as an example if you’d like

oh, i think its the 3 second delay you added if im not mistaken which is why, besides that it works flawlessly, if i just modify the time in between to be lets say 0.5 of a second it should work pretty good given the time to kill is very fast sometimes if its multiple people

Alright. Glad to hear it works!

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