Punch Script Punches without button being pressed

I’m trying to make a punching script where you press F to punch. The script works, but if you stand next to somebody and your arm touches them it’ll still deal damage.

--ServerScriptService Script
local punchdb = {}
-- Punchdb is a antiexploit thing for if a remotevent is being fired too quickly.
local punch = game:GetService("ReplicatedStorage"):WaitForChild("PunchEvent")
punch.OnServerEvent:Connect(function(player)
	if punchdb[tostring(player.UserId)] == nil then
		punchdb[tostring(player.UserId)] = false
		local debounce = false
		local PunchEvent = game:GetService("ReplicatedStorage")["PunchEvent"]
		 PunchEvent.OnServerEvent:Connect(function(plr,Touch)
			plr.Character.RightHand.Touched:connect(function(hit)
				if hit.Parent ~= plr.Character then
					if hit.Parent:FindFirstChild("Humanoid") then
						if debounce == false then
							debounce = true
							local dividedby =( plr.leaderstats.Strength.Value / 5 ) + 5
							hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dividedby 
							wait(.6)
							debounce = false
						end

					end

				end
			end)
			plr.Character.LeftHand.Touched:connect(function(hit)
				if hit.Parent ~= plr.Character then
					if hit.Parent:FindFirstChild("Humanoid") then
						if debounce == false then
							debounce = true
							local dividedby = ( plr.leaderstats.Strength.Value / 5 ) + 5

							hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dividedby
							wait(.6)
							debounce = false
						end
					end
				end
			end)
		end)
		wait(2) --cooldown
		
		punchdb[tostring(player.UserId)] = nil
	else
		player:kick("CL Studios | You were kicked due to unusual client behaviour. Please contact an Admin if you think this is a error.")
	end
	
end)

--LocalScript
local USI = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local DB = false
local Touched = false

USI.InputBegan:Connect(function(key,chat)
	if chat then return end
	if key.KeyCode == Enum.KeyCode.F then
		if DB == false then
			DB = true
			Touched = true
			local random = math.random(1,2)
			if random == 1 then
			player.Character.Humanoid:LoadAnimation(script.LeftPunch):Play()
				game.ReplicatedStorage:FindFirstChild("LePunchLeKid"):FireServer(Touched)
			end
			if random == 2 then
				player.Character.Humanoid:LoadAnimation(script.RightPunch):Play()
				game.ReplicatedStorage:WaitForChild("LePunchLeKid"):FireServer(Touched)
			end
			wait(3.5)
			DB = false
			Touched = false
		end
	end
end)

There aren’t any errors, and the localscript is located in starterpack, and there are two animations inside of the localscript.

Thanks for the help in advanced!

you need to disconnect the touched event

Issue’s with the .Touched function not disconnecting , but here is a few stuff I recommend doing:

  • Do not put keybinds code in StarterPack It’s only meant for tools, put it in StarterPlayerScripts instead. (its under StarterPlayer)
  • You’re using a deprecated way of loading animations, more info on that here: Deprecating LoadAnimation on Humanoid and AnimationController
    Adding onto the animation case, avoid loading the animations everytime a player clicks, just load it out of the input function, and play it when needed.
  • When using touched function, you aren’t disconnecting which means the function stays, so I suggest using :Disconnect() whenever you’re done with punching.
  • Last but not least, please do not use .Touched, it is extremely unreliable and bugs out a lot, if you want a better melee system look into Raycasting or use RaycastHitbox module (Raycast Hitbox) or use Region3 ( Region3)

Alright, thanks so much! I’ve been stuck on this for a while.

Much appreciated! Have a great weekend and happy holidays! :santa:

1 Like