How to make a damage script for when a player touches the Hitbox, Punch Script

I’m not that sure if making damage scripts for the hitbox is Extremely Complicated or not as I’m quite new but I just need to know how to make one for this and future reference.

All the damage scripts ive tried to make before always only damage My player and not the Dummy’s or Enemy Player

Script is for Cloning the Hitbox the rest Above it is just for Animations, Sound so most likely not useful.

Tool.Activated:Connect(function()
	wait(0.25)
	local Hitbox = RS.HitBox:Clone()
	Hitbox.CFrame = HRP.CFrame * CFrame.new(0,0,-2)
	Hitbox.Parent = workspace
    
	local weld = Instance.new("ManualWeld")
	weld.Part0 = Hitbox
	weld.Part1 = HRP
	weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
	weld.Parent = weld.Part0
	wait(0.25)
	Hitbox:Destroy()
end)

Anyone help?
(Filler moment for Filler)

To detect whether the player who is touching does not have the same name as the player, try getting the player first once they equip the tool. Then, use a variable “Event” to start a touch function for the hitbox.

Im not so sure if this code will work, but try this:

local Tool = script.Parent
local Event = nil
local Player
local Character


Tool.Equipped:Connect(function()
	Character = Tool.Parent
	Player = game.Players:GetPlayerFromCharacter(Character)
end)


Tool.Activated:Connect(function()
	wait(0.25)
	local Hitbox = RS.HitBox:Clone()
	Hitbox.CFrame = HRP.CFrame * CFrame.new(0,0,-2)
	Hitbox.Parent = workspace

	Event = Hitbox.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.Name ~= Player.Name then -- checks if player touching does not have the same name as tool owner
			plr.Character.Humanoid:TakeDamage(100)
			end
	end)


	local weld = Instance.new("ManualWeld")
	weld.Part0 = Hitbox
	weld.Part1 = HRP
	weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
	weld.Parent = weld.Part0
	wait(0.25)
	Event = nil
	Hitbox:Destroy()
end)

I tried this, put a couple prints in its arriving at Take Damage but is not doing anything to dummy nor me(Which is good) But I need the dummy to take damage

You can use .Touched events and damage the other players and NPCs that way.

New code:

--//Controls
local damage = 20
local attackLength = 1
local touchedEvent = nil

--//Tables
local humanoidDebounces = {}

--//Functions
Tool.Activated:Connect(function()	
	task.wait(0.25)
	
	local Hitbox = RS.HitBox:Clone()
	Hitbox.CFrame = HRP.CFrame * CFrame.new(0, 0, -2)
	
	local weld = Instance.new("ManualWeld")
	weld.Part0 = Hitbox
	weld.Part1 = HRP
	weld.C0 = Hitbox.CFrame:ToObjectSpace(HRP.CFrame)
	weld.Parent = Hitbox
	
	Hitbox.Parent = workspace
	
	touchedEvent = Hitbox.Touched:Connect(function(hit)
		if hit.Parent == Tool.Parent then
			return
		end
		
		local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		
		if not Humanoid or humanoidDebounces[Humanoid] then
			return
		end
		
		humanoidDebounces[Humanoid] = true
		
		Humanoid.Health -= damage
		
		task.wait(1)
		humanoidDebounces[Humanoid] = nil
	end)
	
	task.delay(attackLength, function()
		touchedEvent:Disconnect()
		touchedEvent = nil
	end)
	
	task.wait(0.25)
	Hitbox:Destroy()
end)

I did Damage but then i got the error Players.watermelon10876.Backpack.Fists.Animator:153: attempt to index nil with ‘Disconnect’

Hmm ok, so what i did in my code i checked whether it is a player, not a dummy. So if i were to change it, it’ll be like this:

	Event = Hitbox.Touched:Connect(function(hit)
		local hum = hit.Parent:FindFirstChildOfClass("Humanoid")
if hum and hum.Parent.Name ~= Player.Name then -- checks if player touching does not have the same name as tool owner
			hum:TakeDamage(100)
			end
	end)
1 Like

THANKS SO MUCH, Your the best And also Katrist Thanks so much For both of your responses :smiley:

1 Like

You’re welcome! Also, I haven’t added a debounce so you could try adding that to the code. If you still find it hard, I could help.

1 Like

I Have done Debounce before so i will try right now, If i struggle i’ll ask but thanks

1 Like

Got Debounce Working Thanks so much.

1 Like