.Touched Event still continuing to check while coroutine is over

So I have a script for a battlegrounds game and I’m using a .Touched event for the damage dealing. Problem is, it stacks. I can just punch alot and the damage overtime increases. Here’s my script.

game.ReplicatedStorage.Events.M1.OnServerEvent:Connect(function(plr, combo)
	
	local pitch = math.random(1,3)
	if pitch == 1 then
		plr.Character.HumanoidRootPart.M1.PlaybackSpeed = 0.8
	elseif pitch == 2 then
		plr.Character.HumanoidRootPart.M1.PlaybackSpeed = 1
	elseif pitch == 3 then
		plr.Character.HumanoidRootPart.M1.PlaybackSpeed = 1.2
	end
	plr.Character.HumanoidRootPart.M1:Play()
	
	plr.Character.Values.Attacking.Value = true

	local anim = Instance.new("Animation", plr.Character.Humanoid)
	anim.AnimationId = 'rbxassetid://17270813124'
	plr.Character.Humanoid:LoadAnimation(anim):Play()
	plr.Character.Humanoid.WalkSpeed = 8
	plr.Character["Right Arm"].CanTouch = true

	local touchedEvent = coroutine.create(function()
		if not plr.Character.Values.Attacking.Value then return end
		plr.Character["Right Arm"].Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and hit.Name == "HumanoidRootPart" then
				plr.Character["Right Arm"].CanTouch = false
				hit.Parent.HumanoidRootPart.CanTouch = false
				hit.Parent.Humanoid:TakeDamage(3.2)
				wait(0.3)
				hit.Parent.HumanoidRootPart.CanTouch = true
			end
		end)
	end)
	
	coroutine.resume(touchedEvent)

	wait(0.5)
	plr.Character.Values.Attacking.Value = false
	plr.Character.Humanoid.WalkSpeed = 25
	coroutine.yield(touchedEvent)
	coroutine.close(touchedEvent)
end)

I don’t think ending the coroutine disconnects the touched event. To do this, do something like

local connection = part.Touched:Connect(function(hit)
			--Your code here
		end)

--When it's time to end the connection

connection:Disconnect()

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