Hello there, I tried to create a hitbox that damage anything it touches once and I managed to that with a tool click. Now i tried to do the same thing for a tool Q keybind, but it does only damage players and not NPCs even though it's the same script I used for the tool click (which can damage NPCs). The hitbox is created at the end of my animation.

Here's the script for the hitbox creation and damage part:

heavypunchAnimationTrack:GetMarkerReachedSignal("Go"):Connect(function()
    local hitbox = Instance.new("Part")
    hitbox.Parent = game.Workspace
    hitbox.Name = "Hitbox"
    hitbox.Transparency = 0.5
    hitbox.Size = Vector3.new(10,30,15)
    hitbox.CanCollide = false
    hitbox.Anchored = true
    hitbox.Position = char.RightHand.Position + HRP.CFrame.lookVector * 16
    hitbox.Orientation = char.RightHand.Orientation
    local debounceDamage = false
    hitbox.Touched:Connect(function(part)
	    local stat2Store = DataStore2("Stat2", player)
	    local stat2Value = stat2Store:Get(1)
	    local target = part.Parent:FindFirstChild("Humanoid")
	    if target and target.Health > 0 and debounceDamage == false then 
		    part.Parent.Humanoid:TakeDamage(15 + stat2Value * 5)
		    debounceDamage = true
	    end
    end)
	wait(1)
	humanoid.WalkSpeed = 16
	hitbox:Destroy()
end)

As I said, this works perfectly fine on players but does not damage NPCs. This is what I used for the tool click:

local hitbox = Instance.new("Part")
hitbox.Parent = game.Workspace
hitbox.Name = "Hitbox"
hitbox.Position = char.RightHand.Position
hitbox.Orientation = char.RightLowerArm.Orientation
hitbox.Massless = true
hitbox.Size = Vector3.new(5,5,3)
hitbox.Transparency = 0.7
hitbox.Color = Color3.new(165,0,2)
hitbox.CanCollide = false
local weld = Instance.new("WeldConstraint")
weld.Parent = game.Workspace
weld.Part0 = char.RightHand
weld.Part1 = hitbox
-- Plays animation
punchAnimationTrack:Play()
-- Sets damage and gives rewards on kill
local debounceDamage = false
hitbox.Touched:Connect(function(part)
     local target = part.Parent:FindFirstChild("Humanoid")
     local experienceStore = DataStore2("Exp", player)
     local currencyStore = DataStore2("Currency", player)
     local stat2Store = DataStore2("Stat2", player)
     local stat2Value = stat2Store:Get(1)
     if target and target.Health > 0 and debounceDamage == false then
	 part.Parent.Humanoid:TakeDamage(10 + stat2Value * 2)
	   if player and target.Health <= 0 then
		     local expReward = part.Parent.ExpReward.Value
             local currencyReward = part.Parent.CurrencyReward.Value
		     experienceStore:Increment(expReward)
		     currencyStore:Increment(currencyReward)
	   end
	 debounceDamage = true
	 end
end)
wait(0.6)
-- Destroys hitbox
hitbox:Destroy()

I don't see what's so different about the second script that it can damage any humanoid, but the first one can only damage player humanoids. I hope one of you can help me with that problem.

1 comment

0

(Solved) Touch does not fire when parts overlap, it only fires when parts physically collide so i just had to make the hitbox move a bit to fire the touch event