How to tell when remote event is over?

I’m making a punch script, and I dont know how to make the punch ONLY damage when I punch.

Here’s my code.

I want to know how, not full scripts.

punchEvent.OnServerEvent:Connect(function(plr,part)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local debounce = false
	
	part.Touched:Connect(function(hit)
		if not debounce and hit.Parent:FindFirstChild("Humanoid") then
			debounce = true
			hit.Parent.Humanoid.Health-=10
			task.wait(1)
			debounce = false
		end
	end)
end)
1 Like

Do not put debounce = false at the end
The code should look like this:

punchEvent.OnServerEvent:Connect(function(plr,part)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local debounce = false
	
	part.Touched:Connect(function(hit)
		if not debounce and hit.Parent:FindFirstChild("Humanoid") then
			debounce = true
			hit.Parent.Humanoid.Health-=10
		end
	end)
end)

The debounce will reset the next punch

Do what @dutycall11 said, but keep in mind that: connecting events takes up memory. If you connect the touch function without disconnecting, you will eat up your game’s memory, making it very slow.

How would I faithfully and joyfully disconnect it?

When parts are destroyed, they automatically disconnect all events, but heres how to do it in your situation:

local event
	event = part.Touched:Connect(function(hit)
		if not debounce and hit.Parent:FindFirstChild("Humanoid") then
			debounce = true
			hit.Parent.Humanoid.Health-=10
event:Disconnect()
		end
	end

Another way to do it is by replacing Connect with Once. This will only connect the event one time.

This works! Thank you so much, although, I had a solution in a server, it used returning. But this works as well.

1 Like