Enemy get damaged when hitting

Hello, how can I make it when I punch Tool.Activated or when the PunchGetDamageEvent has been activated anyways I got the tool and handle variable. How would I get the humanoid from the handle touch?

So not me, the one who gets damaged when I punch. I want the enemy because now when I activate the tool, Im the one who gets damaged.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PunchGetDamageEvent = ReplicatedStorage.RemoteEvents.PunchGetDamage
local PunchSoundEvent = ReplicatedStorage.RemoteEvents.PunchSound

local Enable = false

PunchGetDamageEvent.OnServerEvent:Connect(function(player)
	if Enable == false then
		Enable = true
		local Tool = player.Character:WaitForChild("Boxer") or player.Backpack:WaitForChild("Boxer")
		local Handle = Tool.Handle
		local humanoid = player.Character.Humanoid
		local damage = 1
		if humanoid.Health > 0 then
			humanoid:TakeDamage(damage)
			PunchSoundEvent:FireAllClients()
			wait(0.5)
			Enable = false
		end
	end
end)

1 Like

Use a Touched event:


local canTouch = true

local function onTouch(otherPart)
	print(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") then
		if canTouch == true then
			canTouch = false
			otherPart.Parent.Humanoid.Health = otherPart.Parent.Humanoid.Health - 10
			wait(1) -- however long you want to wait before more damage can happen
			canTouch = true
		end
	end
end

See Touched events:

I would recommend against using wait(). You can find the complications of using wait here.

Instead, I would recommend using task.delay() if you want your code to perform a function after a specified delay. In this case, that would be setting canTouch to true.

Other than that, this should be a good basis to start off with.