Punch Hitbox works but humanoid lose life when I touch it also [SOLVED]

I’m trying to make sure that the humanoid doesn’t lose life when I’m not punching even if I’m not punching humanoid is still losing. My hitbox is perfect that’s not the problem it’s like the event is still playing in the background for no reason

I tried disconnecting the event not working though

Client Code

local Services = {
	UserInput = (game:FindService("UserInputService") or game:GetService("UserInputService")),
	ContextAction = (game:FindService("ContextActionService") or game:GetService("ContextActionService"))
}

local Events = {
	Punch = (game.ReplicatedStorage:FindFirstChild("Punch"))
}

local ReplicatedStorage = game.ReplicatedStorage
local CanPunch = true
local humanoid = script.Parent.Humanoid
local CurrentPlayer = nil
local Connection = nil
local Character = script.Parent
local LocalPlayer = game.Players.LocalPlayer


function punchMobile()
	if CanPunch == true then
		CanPunch = false
		local defaults = game.Players.LocalPlayer:WaitForChild("DefaultAnimations")
		local randomIndex = math.random(1, #defaults:GetChildren())

		local animation = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://" .. defaults:GetChildren()[randomIndex].Value
		--	animation.AnimationId = "rbxassetid://16164866387"

		local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)

		YourAnimationTrack.Priority = Enum.AnimationPriority.Action
		YourAnimationTrack:Play()
		
		
		
		
		wait(YourAnimationTrack.Length)
		
		Events.Punch:FireServer()
		CanPunch = true
	end
end


if Services.UserInput.TouchEnabled then
	local PunchMobileButton = Services.ContextAction:BindAction("Bench", punchMobile, true)
	Services.ContextAction:SetPosition("Bench", UDim2.new(0.62, -25, 0.30, -25))
	Services.ContextAction:SetImage("Bench", "rbxassetid://16199341429")

end

Services.UserInput.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if CanPunch == true then
			CanPunch = false
			local defaults = game.Players.LocalPlayer:WaitForChild("DefaultAnimations")
			
			if #defaults:GetChildren() > 1 then
				local randomIndex = math.random(1, #defaults:GetChildren())

				local animation = Instance.new("Animation")
				animation.AnimationId = "rbxassetid://" .. defaults:GetChildren()[randomIndex].Value
				--	animation.AnimationId = "rbxassetid://16164866387"

				local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)

				YourAnimationTrack.Priority = Enum.AnimationPriority.Action
				YourAnimationTrack:Play()

				wait(YourAnimationTrack.Length)

				Events.Punch:FireServer()
				CanPunch = true
			else
				local randomIndex = math.random(1, #defaults:GetChildren())

				local animation = Instance.new("Animation")
				animation.AnimationId = "rbxassetid://" .. defaults:GetChildren()[randomIndex].Value
				--	animation.AnimationId = "rbxassetid://16164866387"

				local YourAnimationTrack = humanoid.Animator:LoadAnimation(animation)

				YourAnimationTrack.Priority = Enum.AnimationPriority.Action
				YourAnimationTrack:Play()

				wait(YourAnimationTrack.Length)

				Events.Punch:FireServer()
				CanPunch = true
			end
		end
	end
end)

Server code :

local punchEvent = game.ReplicatedStorage.Punch
local debounce = false
local Connection = nil
local TouchedConnection = nil
local CurrentPlayer = nil
local Connection = nil
local counter = 0

local Properties = {
	Damage = 10
}


local function OnPlayerPunch(player)
	CurrentPlayer = game.Workspace:FindFirstChild(player.Name)
	
	local LeftLowerArm = CurrentPlayer.LeftLowerArm
	local LeftHand = CurrentPlayer.LeftHand
	
	print("PLAYER PUNCHED")
	
	print(Connection.Connected)

	LeftLowerArm.Touched:Connect(function(hit)
		local Hum,FF = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
		if not Hum or FF or Hum.Health <= 0 then return end
		
		
		print("nand"..hit.Parent.Name)
		print("nand"..hit.Name)
		if not debounce then

			debounce = true
			if hit.Parent.Name ~= "El Pueblo" then
				local current_sound = game.Workspace.Audio.PunchHumanoid
				current_sound:Play()
				Hum:TakeDamage(Properties.Damage)
			end

			wait(1)	
			debounce = false
		end
	end)
	
	LeftHand.Touched:Connect(function(hit)
		local Hum,FF = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
		if not Hum or FF or Hum.Health <= 0 then return end
		
		if not debounce then

			debounce = true


			if hit.Parent.Name ~= "El Pueblo" then
				local current_sound = game.Workspace.Audio.PunchHumanoid
				current_sound:Play()
				Hum:TakeDamage(Properties.Damage)
			end
			wait(1)
			debounce = false
		end
	end)
	
end

Connection = punchEvent.OnServerEvent:Connect(OnPlayerPunch)
1 Like

The connection never seems to be disconnected after the player punches, try doing that and see if it works then.

When you run the Connection for OnPlayerPunch you activate:

• LeftLowerArm.Touched
• LeftHand.Touched

These two functions will stay active.

You need to either try and reset the LeftLowerArm and LeftHand to nil or setup some kind of bool value that will tell the two function to run or not run.

Problem is when I disconnect the event after that I can’t even make damage to humanoid after I tried putting it in the function OnPunch didn’t work either and wasn’t disconnecting the event

Well I tried this

local Events = {
	Punch = (game.ReplicatedStorage:FindFirstChild("Punch")),
	PunchEnded = (game.ReplicatedStorage:FindFirstChild("PunchEnded"))
}


local debounce = false
local Connection = nil
local TouchedConnection = nil
local CurrentPlayer = nil
local Connection = nil
local counter = 0

local Properties = {
	Damage = 10
}


local function OnPlayerPunch(player, isPunching)
	CurrentPlayer = game.Workspace:FindFirstChild(player.Name)
	
	local LeftLowerArm = CurrentPlayer.LeftLowerArm
	local LeftHand = CurrentPlayer.LeftHand
	
	print("PLAYER IS PUNCHING : " .. tostring(isPunching))
	
	if isPunching == true then
		LeftLowerArm.Touched:Connect(function(hit)
			local Hum,FF = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
			if not Hum or FF or Hum.Health <= 0 then return end


			print("nand"..hit.Parent.Name)
			print("nand"..hit.Name)
			if not debounce then

				debounce = true
				if hit.Parent.Name ~= "El Pueblo" then
					local current_sound = game.Workspace.Audio.PunchHumanoid
					current_sound:Play()
					Hum:TakeDamage(Properties.Damage)
				end

				wait(1)	
				debounce = false
			end
		end)

		LeftHand.Touched:Connect(function(hit)
			local Hum,FF = hit.Parent:FindFirstChildOfClass("Humanoid"),hit.Parent:FindFirstChildOfClass("ForceField")
			if not Hum or FF or Hum.Health <= 0 then return end

			if not debounce then

				debounce = true


				if hit.Parent.Name ~= "El Pueblo" then
					local current_sound = game.Workspace.Audio.PunchHumanoid
					current_sound:Play()
					Hum:TakeDamage(Properties.Damage)
				end
				wait(1)
				debounce = false
			end
		end)
	end
end



Events.Punch.OnServerEvent:Connect(OnPlayerPunch)











Didn’t work either

You put this statement: if isPunching == true then before the function LeftLowerArm.Touched

It need to be after not before.

I’ll try this then let’s see what it does

Nop I put it in the if isPunching condition didn’t work either

The two functions will stay running once they have been activated.

They are no longer listening for the event that started them.

That is why you need the variable if Punching = true to be within the two functions.

The event, at that point, is only useful for changing which player the functions are working for.

Oh wait I maybe have an idea about how to solve

This is because you never connect the function again, you will need to create a system where you connect and disconnect the function depending on if the user is allowed to punch or not.

Problem is when you use :Disconnect() function it’s like you can never reconnect the remote event again unless you create an instance of the remote event

You should no longer need to disconnect the function.

Ah so you are using remove events then.

No I don’t I just have no clue how do you reconnect an event

I was reading about :Once a few days ago but did not understand very much.

I think it connects/disconnects for you.

Oh yeah I think Once is a good idea for my case then because it’s like the function never disconnect or it’s because of the nature of Touched event that is there by default the problem

Wait a minute if I apply Once to touched event on my thing will that work since it’s will only triggered once each time I fire the event technically which is why the word is Once

I didn’t understand the Once very well. I read the documentation then a bunch of forum post on the subject. Made my head hurt. :grimacing:

Yayt I was quite close then I realized I still had the issue