I'm trying to make it so that when I swing a sword, I would like to hit everyone touched by the blade only once. When it was hitting only one person, it works fine, but if I hit multiple people, it instantly kills them, somehow passing the debounce. My guess is that it isn't checking for the target fast enough until Touched fires again. How do I fix this?

Slash function:

local function IsAlreadyAffected(affected, target)
	for _,v in pairs(affected) do
		if target == v then
			return true
		end
	end
	return false
end

function EvasionSlash()
	if inAction.Value == true then return end
	
	if cooldown == false then
		--This is the multiple player debounce.
		local affected = {}
		
		cooldown = true
		inAction.Value = true
		
		Humanoid.WalkSpeed = 0
		Humanoid.JumpPower = 0
		
		Evasion:Play()
		
		local MoveBack = RunService.RenderStepped:Connect(function()
			RootPart.CFrame = RootPart.CFrame - RootPart.CFrame.lookVector*0.2	
		end)
		
		local Attack
		
		local SlashBegin = Evasion.KeyframeReached:Connect(function(Keyframe)
			if Keyframe == "SlashBegin" then
				Trail.Enabled = true
				MoveBack:Disconnect()
				Attack = Character.Blade.Touched:Connect(function(hit)
					if not IsAlreadyAffected(affected, hit.Parent) then
						table.insert(affected, hit.Parent)
						game.ReplicatedStorage.Damage.Damage:FireServer(100, hit)
					else
						print(hit.Parent.Name, "already affected.")
					end
				end)
			end
		end)
		
		local SlashEnded = Evasion.KeyframeReached:Connect(function(Keyframe)
			if Keyframe == "SlashEnd" then
				Attack:Disconnect()
				Trail.Enabled = false
				inAction.Value = false
				Humanoid.WalkSpeed = 16
				Humanoid.JumpPower = 50
			end
		end)
		
		wait(COOLDOWN_TIME)
		cooldown = false
		
	end
end

This is the server side of the remote event:

DamageEvent.OnServerEvent:Connect(function(player, damage, enemy, class)
	local target = enemy.Parent:FindFirstChild("Humanoid") 
		if target then
			damage = damage * enemy.Parent.NormalResist.Value
			target:TakeDamage(damage)
			local p = math.random(1,2)
			if p == 1 then
				p = false
			else
				p = true
			end
			visuals.OverheadVisual(enemy, damage, "Physical", p)
		end
end)

2 comments

0

Make the debounce happen to the Touched event rather than the whole event itself.

0

I think the debounce is already for the touched event?