Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Punching script damages even if player isn't punching?

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

0

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.

0

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?

0

check if it equals nil and if it does then do whatever you want it to do

0

Check if what = nil?

3 answers

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.

1 comment

0

Thank you for the answer! But that's not really working, it still damages the player and all but it doesn't fix the initial issue, the problem is if you attack, the function doesn't fire until you've touched a player, so even if you disconnect the function is still waiting to be fired.

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.

8 comments

0

Hey man! Thanks for the answer, but this still doesn't fix my initial problem. I don't think people are understanding the issue. Thanks for your time though! (P.S I think you should leave all of the white space out of your code, causes a lot of unnecessary latency, I like to keep things organized with --Comments.)

0

May i know what do u press to activate the "Punch" function?

0

Can u give me more details here?? cause i dont really understand what u are trying to do here. I can do the Fire RemoteEvent script for u, but i just need more details and explanation, Thanks!

0

Because from what i understand from ur script, what u are trying to do is: Theres no punch animation, the hand doesnt move, so when it touches humanoid, it will just deal damage, no matter the tool is equipped or unequipped.

0

That's understandable, I'll just edit it and paste the whole script here so you can tell me what I'm doing wrong lol.

0

oof cant edit it

0

The only issue with the script is this part, everything else works fine and the only problem is that I want to know how to make the punching script not damage someone after they swung and didn't hit anyone.

0

are u using localscript or normal script for this script?

This discussion is preserved as it was. New answers, comments, and votes are closed.