Hey y'all, I've been working on this punching tool for a while, it has a punching pattern, debounce, etc. Everything works fine except when you punch if you don't immediately hit someone it waits until it finds someone to damage, and then the damage function fires. This can cause problems if you punched the air, unequipped the tool and forgot about it, and ended up accidentally punching someone.
Here's the code:
local alreadyHit = false
local function dmg()
rhand.Touched:Connect(function(hit)
if alreadyHit then local ehum = hit.Parent:FindFirstChild("Humanoid") if ehum then ehum:TakeDamage(15)
else return end Hit:Play() alreadyHit = false end
end)
lhand.Touched:Connect(function(hit)
if alreadyHit then local ehum = hit.Parent:FindFirstChild("Humanoid") if ehum then ehum:TakeDamage(15)
else return end Hit:Play() alreadyHit = false end
end)
end
dmgEvent.OnServerEvent:connect(function()
swing:Play()
alreadyHit = true
dmg()
print("Fired")
end)
I've tried everything.
Any suggestions would be really appreciated!

4 comments
Where you have "else return" make it "else alreadyHit = false return". That might be the problem. You could also try disconnecting from the Touched function after damage has been dealt.
The damage being dealt is the issue, if you punch, the damage function doesn't run until it finds a player to damage, How would I make it where if the damage function is called and it doesn't detect anyone it cancels the damage?
check if it equals nil and if it does then do whatever you want it to do
Check if what = nil?