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

Post your latest code blocks so the forum can see where you are with them.

Yeah sure

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
}

Events.Punch.OnServerEvent:Connect(function(player, isPunching)
	print("PLAYER IS PUNCHING : " .. tostring(isPunching))
	
	CurrentPlayer = game.Workspace:FindFirstChild(player.Name)
	local LeftLowerArm = CurrentPlayer.LeftLowerArm
	local LeftHand = CurrentPlayer.LeftHand

	LeftLowerArm.Touched:Once(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:Once(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)











WOW I solved my problem by myself again dumb mistake lol

Alright you guys want a perfect hit box here’s your chance

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
}

Events.Punch.OnServerEvent:Connect(function(player, isPunching)
	print("PLAYER IS PUNCHING : " .. tostring(isPunching))
	
	CurrentPlayer = game.Workspace:FindFirstChild(player.Name)
	local LeftLowerArm = CurrentPlayer.LeftLowerArm
	local LeftHand = CurrentPlayer.LeftHand

	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
				if isPunching == true then
					local current_sound = game.Workspace.Audio.PunchHumanoid
					current_sound:Play()
					Hum:TakeDamage(Properties.Damage)
				end
			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
				if isPunching == true then
					local current_sound = game.Workspace.Audio.PunchHumanoid
					current_sound:Play()
					Hum:TakeDamage(Properties.Damage)
				end
				
				
			end
			wait(1)
			debounce = false
		end
	end)
end)
1 Like

Client

-- You do not need to use findservice, GetService unsures that the service has existed and return it.
-- https://create.roblox.com/docs/reference/engine/classes/ServiceProvider#GetService

local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local Events = {
	Punch = (ReplicatedStorage.Punch) -- You do not need to find or wait for it since it will always get loaded until the scripts are enabled.
}

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

local DebounceTime = 1

function DoPunch()
	if not CanPunch then
		return
	end
	CanPunch = false
	local defaults = 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()
	
	Events.Punch:FireServer()
	wait(YourAnimationTrack.Length)
	CanPunch = true

end


if UserInputService.TouchEnabled then
	local PunchMobileButton = ContextActionService:BindAction("Bench", DoPunch, true)
	ContextActionService:SetPosition("Bench", UDim2.new(0.62, -25, 0.30, -25))
	ContextActionService:SetImage("Bench", "rbxassetid://16199341429")

end

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		DoPunch()
		-- I removed all the functions, Correct me if i'm wrong but all of them are basically the same.
	end
end)

Server

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local punchEvent = ReplicatedStorage.Punch
local current_sound = game.Workspace.Audio.PunchHumanoid

--[[
You should make the debounce different by player. 

If you did not do this then imagine if a lot of player tries to punch. 
Some player cannot since the debounce is applied to all player.
]]
local PlayerDebounce = { }

local Properties = {
	Damage = 10,
	DebounceTime = 1
}

local function OnPlayerPunch(player : Player)
	-- instead of getting the player's character on workspace you can use the Character property.
	-- https://create.roblox.com/docs/reference/engine/classes/Player#Character
	
	if PlayerDebounce[player] then
		return -- do not punch since that player is currently punching
	end
	
	PlayerDebounce[player] = true
	
	local character = player.Character
	local LeftLowerArm : BasePart = character.LeftLowerArm
	local LeftHand : BasePart = character.LeftHand

	print("PLAYER PUNCHED")
	
	-- The touched connections will be stored here
	local touchConnections = {}
	
	-- If you want the punch only be hit once for every character.
	local characterTouched = {}
	
	-- Disconnects the touch connections and player debounce reference.
	local function disconnect()
		PlayerDebounce[player] = nil
		for _, connection in touchConnections do
			connection:Disconnect()
		end
	end
	
	local function damagePlayer(character, humanoid)
		if character.Name ~= "El Pueblo" then
			current_sound:Play()
			humanoid:TakeDamage(Properties.Damage)
		end
	end
	
	
	local function onHit(hit : BasePart)
		local character = hit.Parent
		local humanoid : Humanoid = character:FindFirstChild("Humanoid")
		if not humanoid or humanoid.Health <= 0 then -- not a character or dead then do not do anything 
			return 
		end
		
		-- the player has already been damaged then do nothing.
		if characterTouched[character] then
			return
		end
		characterTouched[character] = true
		damagePlayer(character, humanoid)
	end
	

	table.insert(touchConnections, LeftHand.Touched:Connect(onHit))
	table.insert(touchConnections, LeftLowerArm.Touched:Connect(onHit))
	
	-- Disconnect after the DebounceTime seconds has passed.
	-- If you do not disconenct then it will just keep damaging every player.
	task.delay(Properties.DebounceTime, disconnect)

end

-- instead of disconnecting the event, disconnect the touch connections instead.
punchEvent.OnServerEvent:Connect(OnPlayerPunch)

The issue is because you did not disconnect the touch events.
I have left some comments explaining how the scripts work. If you have any questions or issues feel free to reply!

DAMN I like your code man nice style maybe some things to inspire myself

1 Like

Yo that makes sense debounce on different player since the server script executes code for everyone therefore will cause problem if everyone spam punch

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.