"attempt to index nil with 'TakeDamage'"

So im not sure why this is happening, can someone help me

local damage = player:WaitForChild("Data").Damage
 hit.Parent:FindFirstChild("npchum"):TakeDamage(damage.Value)

Thank you

2 Likes

It means hit.Parent.npchum doesn’t exist. Consider changing FindFirstChild into WaitForChild.

2 Likes

In addition to what @Ty_Scripts mentioned, I’d advise checking if it was found before trying to call TakeDamage on the Instance just in case it never ends up in its intended spot:

local npchum = hit.Parent:WaitForChild("npchum")

if npchum then -- Only continues if "npchum" is found to ensure it doesn't error
    npchum:TakeDamage(damage.Value)
end
2 Likes

I have a wierder situation: it said attempt to index number with ‘TakeDamage’. What the **** is wrong here??? Isn’t :TakeDamage() supposed to take in number???

That error is indicating that you’re calling :TakeDamage() on a number rather than an Instance (which in this case, should be a Humanoid object).

no, i did humanoid:TakeDamage(player.Damage.Value). player.Damage.Value is 0 at the time. When I changed it to 10, it still had the error. I will try it again

it still doesnt work for some reason

The error isn’t originating from the Damage Value that you input into the method, it’s happening as a result of humanoid:TakeDamage().

Try printing typeof(humanoid) and see if it returns “number”. If that’s the case, then that means that the “humanoid” variable isn’t referring to an Instance, which should be the Humanoid object when using the TakeDamage method.


Example

When running the code in this example, the same error that you mentioned will appear in the Output, originating from the line that says humanoid:TakeDamage(10) because the “humanoid” variable contains a reference to a number and not an Instance, whereas the line that says realHumanoid:TakeDamage(50) works as intended because the “realHumanoid” variable contains a reference to a Humanoid object.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		
		local humanoid = 1
		local realHumanoid = Character:WaitForChild("Humanoid")
		
		realHumanoid:TakeDamage(50)
		print("The real Humanoid has taken 50 damage points")
		
		task.wait(5)
		
		humanoid:TakeDamage(10)
	end)
end)

oh wait HAHAHAHAHAHAHA IM SO DUMB

At first player.Damage.Value was 0. I was using humanoid.Health -= player.Damage.Value so it’s doing nothing. Then, I just changed it into humanoid.Health:TakeDamage(player.Damage.Value) and then I found out that player.Damage Value was 0 so I set it to 10 but I didnt realize that im calling takedamage on humanoid.Health LMFAOOOOOOOOOOO anyways thx for u help

1 Like