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!
It sounds like the hand parts' Touched events are still connected to the functions you installed. The Connect
method of an event returns an RBXScriptConnection
object so that you can undo the connection later if you save that object.
local alreadyHit = false local rHandConn, lHandConn local function OnHandTouch(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 local function dmg() rHandConn = rhand.Touched:Connect(OnHandTouch) lHandConn = lhand.Touched:Connect(OnHandTouch) end script.Parent.Unequipped:Connect(function() if rHandConn then rHandConn:Disconnect() rHandConn = nil end if lHandConn then lHandConn:Disconnect() lHandConn = nil end end)
I also moved your identical functions for the two hands into a named function OnHandTouch
, so that for any future changes to that logic, you only need to make an edit in one place.
Ur script is quite messy dawg, I arranged it for u. Make sure to look through the script and change whatever u need to change. But make sure u know what u are doing there, Peace.
-- Make sure to look through the script and change whatever u need to change, cause im writing this script without the RemoteEvents, Hit animation and stuff. But also pls make sure u know what u are doing there. local alreadyHit = false function dmg() rhand.Touched:Connect(function(hit) if alreadyHit == false then -- Changed local ehum = hit.Parent:FindFirstChild("Humanoid") if ehum and alreadyHit == false then -- Changed ehum:TakeDamage(15) alreadyHit = true -- Added else return end Hit:Play() wait(1) -- Added alreadyHit = false end end) lhand.Touched:Connect(function(hit) if alreadyHit == false then -- Changed local ehum = hit.Parent:FindFirstChild("Humanoid") if ehum and alreadyHit == false then -- Changed ehum:TakeDamage(15) alreadyHit = true -- Added else return end Hit:Play() wait(1) -- Added alreadyHit = false end end) end tool.Equipped:Connect(function() -- Added Function, U can change "tool.Equipped" to whatever is clicked or pressed, example, "script.Parent.MouseButton1Click:Connect(function()". game.ReplicatedStorage.dmgEvent:FireServer(dmg) -- U can change ReplicatedStorage to ur RemoteEvent location as well or whatever, as long as u know what u are doing. end) dmgEvent.OnServerEvent:connect(function() swing:Play() -- Deleted alreadyHit = true, u can add it back if u want, but delete the "alreadyHit = true" in ~~function dmg()~~ -- -- Deleted "dmg()" print("Fired") end) -- Make sure to look through the script and change whatever u need to change, cause im writing this script without the RemoteEvents, Hit animation and stuff. But also pls make sure u know what u are doing there.
If any Error, Pls comment below.