Can't Damage Mobs

Hey Devs,

I have an issue here. I made a magic ability that damages you when you touch it but for some reason when a mob hits it, it doesn’t affect it’s health.

Can anyone help me?


Here is a screenshot of the code:

And here’s the code:

Attack_Clone.Touched:Connect(function(hit) -- Attack clone is my attack

	local DamageCooldown = 0.5
	local DamageDebounce = false

	local Damage = DefaultDamage + player.Attributes.Attack.Value -- This caculates the damage with how much Attack the player has

	local xLifetime = 5

	if hit.Parent.Name == player.Name then return end -- Makes it so you can't damage yourself

	if not hit.Parent:FindFirstChild("HitFolder") then
		local Folder = Instance.new("Folder", hit.Parent)
		Folder.Name = "HitFolder"
	end

	if not hit.Parent:FindFirstChild("HitFolder"):FindFirstChild(player.Name) then
		local x = Instance.new("IntValue", hit.Parent:FindFirstChild("HitFolder"))
		x.Name = player.Name
		game.Debris:AddItem(x, xLifetime)
		hit.Parent.Humanoid:TakeDamage(Damage)
		wait(DamageCooldown)
		DamageDebounce = false
	end
end)

Is the Attack_Clone anchored? If so, .Touched event only fire from physic collision.

2 Likes

Yes it is but if I don’t anchor it, it just falls off

The issue is that you are checking something which is unnecessary;

if not hit.Parent:FindFirstChild("HitFolder"):FindFirstChild(player.Name) then

I am still quite confused how you came up with this and how this would work.
Note that using :FindFirstChild() on something which possibly could be nil (aka on another :FindFirstChild() would break the script.

Instead it should be:

local hum = hit.Parent:FindFirstCHildWhichIsA("Humanoid")
if hum then

Note that the DamageCooldown, DamageDebounce, Damage and Lifetime should be kept outside of the .Touched event.

1 Like

Is there an event working for anchored items?

Why should it be outside the event? Just asking :slight_smile:

As else the DamageDebounce would not work, due to a new function being called every time.
For the other variables it is pointless to repeatly store them in different functions even though they are one and the same.

And note you have not used an if statement to check for the DamageDebounce

Please mark my previous post as the solution if that answers your question.

Would you be able to provide the full script for more context? It would help with understanding the direction of the current code you have provided.

I changed it because someone said that I need to find the HitFolder

That’s my current code now:

		Attack_Clone.Touched:Connect(function(hit)
			
			local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
			
			if hit and humanoid then
					humanoid.Health -= 5
				end
		end)

Which also doesn’t do damage

local player = script.Parent.Parent.Parent --i made this up assuming this script is inside a tool
local character = player.Character --this is retrieved from the previous reference

local DamageCooldown = 0.5
local DamageDebounce = false
local DefaultDamage = 10 --i just made this up
local IntValueLifetime = 5
local Attack_Clone = script.Parent.Handle --i made this up to

Attack_Clone.Touched:Connect(function(hit) -- Attack clone is my attack
	local Damage = DefaultDamage + player.Attributes.Attack.Value -- This caculates the damage with how much Attack the player has

	if DamageDebounce then
		return
	end

	if hit.Parent == character then
		return
	end

	if hit.Parent:FindFirstChild("Humanoid") then
		DamageDebounce = true
		local enemyModel = hit.Parent
		local enemyHumanoid = enemyModel:WaitForChild("Humanoid")
		if not enemyModel:FindFirstChild("HitFolder") then
			local Folder = Instance.new("Folder")
			Folder.Parent = enemyModel
			Folder.Name = "HitFolder"
			if not Folder:FindFirstChild(player.Name) then
				local IntValue = Instance.new("IntValue")
				IntValue.Parent = Folder
				IntValue.Name = player.Name
				game.Debris:AddItem(IntValue, IntValueLifetime)
				enemyHumanoid:TakeDamage(Damage)
			else
				local IntValue = Folder:FindFirstChild(player.Name)
				game.Debris:AddItem(IntValue, IntValueLifetime)
				enemyHumanoid:TakeDamage(Damage)
			end
		else
			local Folder = enemyModel:FindFirstChild("HitFolder")
			if not Folder:FindFirstChild(player.Name) then
				local IntValue = Instance.new("IntValue")
				IntValue.Parent = Folder
				IntValue.Name = player.Name
				game.Debris:AddItem(IntValue, IntValueLifetime)
				enemyHumanoid:TakeDamage(Damage)
			else
				local IntValue = Folder:FindFirstChild(player.Name)
				game.Debris:AddItem(IntValue, IntValueLifetime)
				enemyHumanoid:TakeDamage(Damage)
			end
		end
		task.wait(DamageCooldown)
		DamageDebounce = false
	end
end)

I made some variables up at the beginning of the script to try and fill in the gaps, you’ll likely need to replace them but the Touched event itself should be good to go.

1 Like

I think this is the issue. If you see its not humanoid.Health = -5 it is humanoid.Health -= 5

I think its that. Maybe i could be wrong but… i saw and i needed to tell you.
Captura de pantalla 2021-12-03 174230

I am not sure about a good one, but the way I did it is using workspace:GetPartsInPart(Part). I put this in a loop

What do you mean “humanoid.Health = -5” ?
It’s “humanoid.Health -= 5” in the script.

Hey guys, thanks all for helping.

I found a solution, I decided to put the damage code inside the attack / vfx mesh instead of the attack script and it worked.