How to make a part do damage

I assume you’re trying to damage the NPC?

The Touched event is on Server correct?

1 Like

that is correct (Random letters)

In your screenshots of the warnings I don’t see Hitname or debounce can you confirm it’s getting passed?

1 Like

Something you can test is changing the Damaging health from

humanoid.Health -= 20

this to this

humanoid.Health = humanoid.Health - 20
1 Like

-= is the same of doing health = health - 20

1 Like

well he can test it cause right now I have no idea how to fix this

1 Like

Yea same, I’m so confused ahaha

1 Like

this works for me and It also does not damage me.

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
    local debounce = false
    local cooldown = 0.5
    
    if hit.Parent ~= script.Parent.Parent.Parent then
        debounce = true
        if humanoid then
            humanoid.Health -= 20
        end
        wait(cooldown)
        debounce = false
    end

end)
1 Like

Sorry I was sleeping but I found the problem when I did

if not script.Parent:GetAttribute("Owner") == hit.Parent.Name then

I had to just remove the not and rewrite some code.

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	local debounce = false
	local cooldown = 5
	
	if humanoid and debounce == false then
		if script.Parent:GetAttribute("Owner") == hit.Parent.Name then
			humanoid.Health +=10
		else
			debounce = true
			humanoid.Health -= 1
			wait(cooldown)
			debounce = false
		end
	end
end)

But this also created another problem for testing purposes it’s supposed to take 1 damage every second but it sends 200 touch requests through and kills them instantly even with my debounce.

I also did checks:

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	local debounce = false
	local cooldown = 5
	
	if humanoid and debounce == false then
		if script.Parent:GetAttribute("Owner") == hit.Parent.Name then
			humanoid.Health +=10
		elseif debounce == false then
			debounce = true
			print("Changed Debounce")
			humanoid.Health -= 1
			print("Removed Health")
			wait(cooldown)
			print("Waiting Complete")
			debounce = false
			print("Changed Debounce Back")
		end
	end
end)

image
Only 2 went through why is it not waiting for the debounce?

Edit:
image
The other checks go through after the cooldown but it still sends 200 requests when it should only send 1 then wait and then can do it again

I DID IT!!! All I had to do was move the variables outside the function so that they are there all the time and not making a bunch of variables when its touched.

Working Code:

local debounce = false
local cooldown = 0.2

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if debounce == false then
		if humanoid then
			if script.Parent:GetAttribute("Owner") == hit.Parent.Name then
				humanoid.Health +=10
			elseif debounce == false then
				debounce = true
				humanoid.Health -= 15
				task.wait(cooldown)
				debounce = false
			end
		end
	end
end)

if not script.Parent:GetAttribute("Owner") == hit.Parent.Name then

The reason this doesn’t work is because not script.Parent:GetAttribute("Owner") gets evaluated immediately as not true as anything which isn’t false/nil is truthy (that includes whatever value :GetAttribute returns. not true is false and this is equated to hit.Parent.Name which is truthy true so the expression essentially becomes if true == false which will never be satisfied.

To fix this you could either use brackets to appropriately split up boolean expressions or a different comparison (relational) operator, i.e;

if not (script.Parent:GetAttribute("Owner") == hit.Parent.Name) then
if script.Parent:GetAttribute("Owner") ~= hit.Parent.Name then

The ~= operator, similar to the == operator, returns true if its operands (values on either side of the operator) are unequal.

1 Like