Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

(SOLVED) Attempt to index nil with "TakeDamage"? On Humanoid

I SOLVED IT MYSELF, I JUST PUT:

script.Parent AS THE HUMANOID VARAIABLE AND IT HAD TO BE: Hit.Parent

Hello, devs!

So I was making a script to the bullet of my gun. Everything worked until I tried to use :TakeDamage(). I don't really know what's the problem and I'll really enjoy any help. Thanks in advice.

CODE:

if script.Parent.Parent == game.Workspace then
	script.Parent.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Humanoid = script.Parent:FindFirstChild("Humanoid")
			if Hit.Name == "Head" then
				Humanoid:TakeDamage(120)
				script.Parent:Destroy()
			elseif Hit.Name == "LowerTorso" or Hit.Name == "UpperTorso" then
				Humanoid:TakeDamage(70)
				script.Parent:Destroy()
			elseif Hit.Name == "LeftUpperArm" or Hit.Name == "RightUpperArm" or Hit.Name == "LeftLowerArm" or Hit.Name == "RightLowerArm" then
				Humanoid:TakeDamage(40)
				script.Parent:Destroy()
			elseif Hit.Name == "LeftLowerLeg" or Hit.Name == "RightLowerLeg" or Hit.Name == "LeftUpperLeg" or Hit.Name == "RightUpperLeg" then
				Humanoid:TakeDamage(30)
				script.Parent:Destroy()
			elseif Hit.Name == "RightHand" or Hit.Name == "LeftHand" or Hit.Name == "RightFoot" or Hit.Name == "LeftFoot" then
				Humanoid:TakeDamage(10)
				script.Parent:Destroy()
			end 
		end
	end)
	wait()
	script.Parent:Destroy()
end	

ERRORS:

13:24:10.715 - Workspace.ErktikyYT's Bullet.BulletDamage:18: attempt to index nil with 'TakeDamage' Line 18

DETAILS:

Bullet part in the workspace, script getting cloned from ReplicatedStorage to the bullet.

1 answer

✓ Accepted answer

Double check your Humanoid variable.

In line 2, it was script.Parent.Touched:Connect(function(Hit). So, In your variable, instead of, local Humanoid = script.Parent:FindFirstChild("Humanoid"), you must do, local Humanoid = Hit.Parent:FindFirstChild("Humanoid").

Lemme know if it helps!

1 comment

0

I solved it myself yet, but you found the problem, so I'll accept your answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.