I need help with dealing damage to humanoid

I want to deal damage to the humanoid when the PunchAnim1 or PunchAnim2 Is played but it doesnt seem to deal damage and no errors show up

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RunServ = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
--if not Char.Parent then Char = Player.CharacterAdded:Wait() end
local Hum = Char:WaitForChild("Humanoid")
local HRP = Char:WaitForChild("HumanoidRootPart")
local Animator = Char:WaitForChild("Humanoid"):WaitForChild("Animator")
local Zone1Parts = game.Workspace.Map.Zone1.Model

local Mouse = Player:GetMouse()

local tool = script.Parent

local Storage = RS.Storage
local AnimationFolder = Storage.Animations
local PunchAnim1 = Hum:LoadAnimation(AnimationFolder.FloorPunch1)
local PunchAnim2 = Hum:LoadAnimation(AnimationFolder.FloorPunch2)

local Debounce = false
local lastAttackTime = 0
local comboStep = 0
local cd = .5

local function TakeDamage(humanoid, damage)
	local health = humanoid.Health
	print("Current health:", health)
	health = health - damage
	print("New health:", health)
	humanoid.Health = health
end

local function AttackPart(part)
	print("Attacking part:", part)
	if part:IsDescendantOf(Zone1Parts) then
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid then
			print("Dealing damage to humanoid:", humanoid)
			TakeDamage(humanoid, 10)
		end
	end
end


local function CheckKeyframeReached(keyframe)
	print("Keyframe reached:", keyframe)
	if keyframe == "Hit" then
		local touchingParts = HRP:GetTouchingParts()
		if #touchingParts > 0 then -- Check if the character is touching any parts
			local hitPart = touchingParts[1]
			AttackPart(hitPart)
		end
	end
end


PunchAnim1.KeyframeReached:Connect(CheckKeyframeReached)
PunchAnim2.KeyframeReached:Connect(CheckKeyframeReached)

tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if Debounce == false then
			Debounce = true
			local currentTime = tick()
			if currentTime - lastAttackTime <= 0.7 and comboStep == 1 then -- Check if it's a combo and on the second step
				PunchAnim1:Play()
				comboStep = 0 -- Reset the combo
				wait(cd)
				Debounce = false
			elseif currentTime - lastAttackTime <= 0.7 then -- Otherwise start the combo
				PunchAnim2:Play()
				comboStep = 1 -- Set the combo to the second step
				wait(cd)
				Debounce = false
			else -- Otherwise play the first punch
				PunchAnim1:Play()
				comboStep = 0 -- Reset the combo
				wait(cd)
				Debounce = false
			end
			lastAttackTime = currentTime
		end
	end)
end)

tool.Unequipped:Connect(function()
	print("Tool unequipped")
end)

I Highly Recommend you Handle the Damage on the Server, Because if you kill the NPC on the Client, it will not Replicate to the Rest of the Server.

It isnt taking Damage because you arent telling it to fire the AttackPart function

And also, Use Tool.Activated, not Mouse.Button1Down

Try to use this:

humanoid:TakeDamage(10)

Instead of this:

TakeDamage(humanoid, 10)

This is on the client, you can tell because the player is being grabbed here, on line 6.

You cannot deal damage accordingly when trying on the client, you must use RemoteEvents and deal the damage on the Server. if you’d like a more thorough explanation; the Client is only what one player can see, and the LocalScript runs on the Client meaning only the RobloxClient can preview it, however can send messages to RemoteEvents in order to sync to the Server allowing for code to be executed Server → Client or Client → Server. The Server is quite literally the server you’re in, calculating all physics, holding all information etc. and when it comes to Player Health, that is also only stored on the Server. This is because if one client affected other players’ Health Roblox could be vulnerable to exploiting and also in general that is bad practice.

2 Likes

im not dealing damage to another players health, im trying to deal damage to a part that you punch with the tool for e.g. in Punch Wall Simulator

Still the same… Health is in a humanoid, humanoids can’t really be affected on client.

1 Like