Add damage to my punching tool

I want to make it so when you activate the tool, it hurts any humanoid that I punch.

I have tried YouTube videos, seeing what other do but I can’t seem to figure it out…

I basically wanting do do, is when I activate the tool, and it plays the punch animation, it also damages any humanoid that I have punched. Herse my current code:

local Player = game.Players
local plr = Player.LocalPlayer
local PunchSound = script.Parent.Punch

local Character = plr.Character

local Humanoid = Character:WaitForChild("Humanoid")

local rightAnimation = Humanoid.Animator:LoadAnimation(script.Parent.RightPunchAnimation)
local leftAnimation = Humanoid.Animator:LoadAnimation(script.Parent.LeftPunchAnimation)

local debounce = false
local waitTime = 0.5

script.Parent.Activated:Connect(function()
	if not debounce then
		debounce = true
		local rn = math.random(1,2)
		if rn == 1 then
			PunchSound:Play()
			rightAnimation:Play()	
		elseif rn == 2 then
			PunchSound:Play()
			leftAnimation:Play()
		end
		wait(waitTime)
		debounce = false
	end
end)

(Going to bed so if I don’t respond that’s why.)

Raycast from the hand in the direction the hand is moving. When a ray is a certain length, you can use the result of that raycast to determine whether or not a humanoid has been hit. If you want to be more accurate, as in the hand hits something while in motion and not just before or after, you can raycast continuously until it hits a humanoid.

1 Like

To make it so that when you activate the tool, it hurts any humanoid that you punch, you’ll need to add code to deal damage to the humanoid.

local Player = game.Players
local plr = Player.LocalPlayer
local PunchSound = script.Parent.Punch
local DamageAmount = 10 – You can adjust this value based on how much damage you want to deal

local Character = plr.Character
local Humanoid = Character:WaitForChild(“Humanoid”)

local rightAnimation = Humanoid.Animator:LoadAnimation(script.Parent.RightPunchAnimation)
local leftAnimation = Humanoid.Animator:LoadAnimation(script.Parent.LeftPunchAnimation)

local debounce = false
local waitTime = 0.5

script.Parent.Activated:Connect(function()
if not debounce then
debounce = true
local rn = math.random(1,2)
if rn == 1 then
PunchSound:Play()
rightAnimation:Play()
elseif rn == 2 then
PunchSound:Play()
leftAnimation:Play()
end

    wait(waitTime)

    -- Check if the tool is still in the player's hand (optional)
    if script.Parent:IsA("Tool") and script.Parent.Parent == plr.Backpack then
        -- Check if the humanoid is still present (could have been destroyed)
        if Humanoid and Humanoid.Health > 0 then
            -- Deal damage to the humanoid
            Humanoid:TakeDamage(DamageAmount)
        end
    end

    debounce = false
end

end)
im a newbei i dont know much but if this helps then great!

1 Like

This makes the punch deal damage to itself.

There are a few ways to do this. My go-to method is just to create a hitbox in front of the player and get everything touching it. If there is any item that is the child of a player character/rig then I deal damage. You can pull parts of code from kill bricks to figure out how to do this hitbox. You can also find tutorials on how to do this on Youtube.

2 Likes

The first thing you’ll need is to set up a hitbox. To ensure the smoothest experience, you should start by invoking a RemoteFunction to the client. The client will then run a function to determine what was hit based off the given parameters (position, size, rotation, range, ect), and then return that back to the server. Lastly, before you start dealing damage, you should run a sanity check to make sure the client wasn’t hitting targets way out of it’s acceptable range.

Here’s a quick guide to the types of hitboxes you can use:

  • Touched: Bad. It’s an event that fires whenever you hit a certain part, which sounds good, but in practice it is very unreliable. I would only ever reccomend using this for killbricks in obbies, since you might have hundreds of killbricks at a time.
  • Magnitude: Simple but gets the job done. Magnitude checks just check the distance from the center of the attack to all possible targets, and returns the target(s) if they are within range. However, the hitbox is always in the shape of a sphere.
  • Raycasts: More trouble than it’s worth. For a set of points on the sword, the check will raycast to where the blade was a moment before, and where it is now. If anything is intercepted, it returns the target. It’s hard to adjust hitboxes since this relies on animations, but if you want uber-realism, this is what you’re looking for.
  • GetPartsInBoundsBox: A hitbox in a literal sense. Give a CFrame and size, it will return all parts in a specific area. The main advantage is that you can have hitboxes that only hit low or high, while being able to easily adjust hitboxes. But you may not need this level of complexity in your game.

The other thing you need to do is make a custom health system. This sounds scary, but all you need to do is make a modulescript that replaces Roblox’s default health. Like this:

--//ModuleScript
local HPmodule = {}

HPmodule.Health = 100

function HPmodule.TakeDamage(Damage)
	--[[
	If you want more variables like status effects or stun,
	you should send a dictionary instead. This works for
	plain damage though.
	]]
	HPmodule.Health -= Damage
	
	if HPmodule.Health <= 0 then
		--die
	end
end
return HPmodule 

and to use a module:

--//Module Script/Server Script
local targetModule = require(HitPerson.HP_Module)  --Path to module.

targetModule.TakeDamage(20) --80 hp
targetModule.TakeDamage(30) --50 hp
targetModule.TakeDamage(100) --Death

This also prevents exploiters from setting their humanoid health way higher than it should be or constantly regenerating it.

2 Likes

Alright, thank you! I just found a tutorial on YouTube about hitboxes for melee tools and I will put that into my punch tool. Is the health system required? Or just to prevent exploiters from taking advantage of the Roblox one.

1 Like

I’d recommend creating a part in front of the player offset to the front by a little bit then start detection of the hitbox with this module:

Also, when detection time has ran out you should delete it or just use debris service

2 Likes

Custom health is not required, but I heavily reccomend it. Besides protecting against exploiters, you can add other functions that are run when you hit someone, like apply a status effect, play an animation, deal knockback, and so on.

2 Likes

Thanks! My plan is to have a part on the firsts of the arms, sort of like a small hitbox, just so that players who don’t get touched by the arms don’t get damaged.

This is how I would do this using the module I provided in the recent comment.

local Module = require(script.Parent)

local Arm:BasePart;
local HitboxPart:BasePart;
local Connection:RBXScriptConnection;

local HitboxSettings = {
	1,
	true,
	"BoundingBox",
	nil,
	true,
	true
}

local HitboxClass = Module.New(HitboxPart, 0) --Damage set to 0 only for character detection purposes
HitboxClass:Init()
HitboxClass:Start(table.unpack(HitboxSettings))

Connection = HitboxClass.CharacterDetected:Connect(function(DetectedCharacter: Model) 
	--Whatever you want to do with the character
	
	
	Connection:Disconnect() --Only to disconnect if we want only one character detected
	Connection = nil
	HitboxClass:End()
end)

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