Attempt to index nil with 'Health'

Alright so i had this problem for some reason it says to attempt to index nil with “Health” (My limb system)
I try to make like when the raycast hit it will check if it had limbs folder and had humanoid if no limbs folder and had humanoid is for npcs, i tried like tries to use if not and if but its not working please help!!

local tool = script.Parent
local jamchance = 1
local SPREAD = 1000


--//Client\\--
tool.Fire.OnServerEvent:Connect(function(player, mouseHit)
	    if tool.Config.Ammo.Value <= 0 or tool.Config.Jammed.Value == true then
			coroutine.wrap(function()
				tool.Handle.Empty:Play()
		end)()
	elseif not tool.Config.Fired.Value == true and not tool.Config.Jammed.Value == true then
		tool.Config.Ammo.Value -= 1
		tool.Config.Fired.Value = true
		    --//RayCast\\--
		local spreadPosition = Vector3.new(
			tool.Handle.Attachment.WorldCFrame.Position.X + math.random(-SPREAD, SPREAD)/500,
			tool.Handle.Attachment.WorldCFrame.Position.Y + math.random(-SPREAD, SPREAD)/500,
			tool.Handle.Attachment.WorldCFrame.Position.Z + math.random(-SPREAD, SPREAD)/500
		)
			local ray = Ray.new(tool.Handle.Attachment.WorldPosition, (mouseHit.p - spreadPosition).unit * 300)
			local hit, position, normal = game.Workspace:FindPartOnRay(ray, tool.Parent, false, true)
		    local distance = (position - tool.Handle.Attachment.WorldPosition).magnitude
			local part = Instance.new("Part")
			part.Anchored = true
			part.CanCollide = false
			part.Transparency = 1
			part.BrickColor = BrickColor.new("Institutional white")
			part.Material = Enum.Material.Neon
			part.Size = Vector3.new(0.2, 0.2, distance)
			part.CFrame = CFrame.new(position, tool.Handle.Attachment.WorldPosition) * CFrame.new(0, 0, -distance / 2)
		    part.Parent = workspace
			--//Damage\\--
		if hit then
			if hit.Parent:FindFirstChild("Humanoid") then
				if not hit.Parent:IsA("Accessory") or not hit.Name == "Handle" or not hit.Parent:IsA("Accoutrement") or not hit.Parent:IsA("Tool") then
					if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Limbs")then
						local limb = hit.Name
						if not hit.Parent.Limbs:FindFirstChild(limb).Health and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Limbs:FindFirstChild(limb) then
							return
						else
							hit.Parent.Limbs:FindFirstChild(limb).Health.Value -= script.Parent.Config.Limb.Value
							hit.Parent:FindFirstChild("Humanoid"):TakeDamage(tool.Config.Damage.Value)
						end
					end
					if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Limbs") then
						hit.Parent:FindFirstChild("Humanoid"):TakeDamage(tool.Config.Damage.Value)
					end
				end
			end
		end
		    --//Fire Animation\\--
		    local fire = script.Parent.Parent:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animations.Fire)
		    fire:Play()
			--//Sounds\\--
		    tool.Handle.Fire:Play()
		    --//Particles\\--
		    for i,v in pairs(tool.Particles:GetChildren()) do
			   if v:IsA("ParticleEmitter") then
				   local particles = v:Clone()
				   particles.Parent = tool.Handle.Attachment
				   particles:Emit(10)
				   game.Debris:AddItem(particles,5)
			   end
		    end
		    --//Hole\\--
		    if hit then
			if hit.Parent:FindFirstChild("Humanoid") then
			local Hole = game.ReplicatedStorage.Hole2:Clone()
			Hole.Parent = workspace
			Hole.Position = mouseHit.p
			local Weld = Instance.new("Weld")
			Weld.Part0 = hit
			Weld.Part1 = Hole
			Weld.C0 = hit.CFrame:Inverse()
			Weld.C1 = Hole.CFrame:Inverse()
			Weld.Parent = Hole
			game:GetService("Debris"):AddItem(Hole, 10)	
		else
		    local Hole = game.ReplicatedStorage.Hole:Clone()
		    Hole.Parent = workspace
		    Hole.Position = mouseHit.p
		    Hole.Size = Vector3.new(0.1,0.1,0.2)
		    Hole.CFrame = CFrame.new(position, position - normal)
		    local Weld = Instance.new("Weld")
		    Weld.Part0 = hit
		    Weld.Part1 = Hole
		    Weld.C0 = hit.CFrame:Inverse()
		    Weld.C1 = Hole.CFrame:Inverse()
			Weld.Parent = Hole
			game:GetService("Debris"):AddItem(Hole, 10)	
			end
			end
			--//Debris?\\--
		    game.Debris:AddItem(part, 0.02)
		    --//Cooldown\\--
		    wait(tool.Config.Cooldown.Value)
		    tool.Config.Fired.Value = false
		    --//Jam System\\--
		    local number = math.random(jamchance, 10)
		    if number == jamchance then
			tool.Config.Jammed.Value = true
		    end
	else
		return
	end
end)

tool.Bolt.OnServerEvent:Connect(function(player)
	if tool.Config.Jammed.Value == true then
		tool.Handle.Bolt:Play()
		wait(0.5)
		tool.Config.Jammed.Value = false
	end
end)

1 Like
  1. Can you PLEASE format your code so it isn’t just a single line going down.
  2. What line does console say the issue is occuring on?
1 Like

13:53:41.780 Players.Player2.Backpack.AK47.Server:39: attempt to index nil with ‘Health’ - Server - Server:39

1 Like
if hit then
if hit.Parent:FindFirstChild("Humanoid") then
if not hit.Parent:IsA("Accessory") or not hit.Name == "Handle" or not hit.Parent:IsA("Accoutrement") or not hit.Parent:IsA("Tool") then
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Limbs")then
local limb = hit.Name
if not hit.Parent.Limbs:FindFirstChild(limb) then print("limb not found") return end
if not hit.Parent.Limbs:FindFirstChild(limb).Health and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Limbs:FindFirstChild(limb) then
return
else
hit.Parent.Limbs:FindFirstChild(limb).Health.Value -= script.Parent.Config.Limb.Value
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(tool.Config.Damage.Value)
end
end
end
if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Limbs") then
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(tool.Config.Damage.Value)
end
end
end
end

can’t be bothered reformatting it all

1 Like

Are you trying to find a part named “Limbs” in a player’s character?

1 Like

No i used the name of the limb to search thru the limbs folder

1 Like

I edit again i show the full gun code

1 Like

Sorry guys if something is wrong its my first time creating a post in this forum

1 Like
if not hit.Parent.Limbs:FindFirstChild(limb).Health and hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Limbs:FindFirstChild(limb) then
    return
else

This errors because if not hit.Parent.Limbs:FindFirstChild(limb).Health returns nil, then it will instantly error if you try to find a child/property inside of that nil.
Oh also you don’t need to check for the humanoid so many times, so here’s the code from below --//Damage\\-- to --//Fire Animation\\-- fixed

if hit then
	local hitHumanoid = hit.Parent:FindFirstChild("Humanoid")
	if hitHumanoid then
		--Accessories are Accoutrements, so you don't need to check for them separatley
		if not hit.Name == "Handle" or not hit.Parent:IsA("Accoutrement") or not hit.Parent:IsA("Tool") then
			local Limbs = hit.Parent:FindFirstChild("Limbs")
			if Limbs then
				local limb = hit.Name
				local foundLimb = Limbs:FindFirstChild(limb)
				if not foundLimb and not foundLimb:FindFirstChild("Health") then
					return
				else
					foundLimb.Health.Value -= script.Parent.Config.Limb.Value
					hitHumanoid:TakeDamage(tool.Config.Damage.Value)
				end
			else --If Limbs wasn't found
				hitHumanoid:TakeDamage(tool.Config.Damage.Value)
			end
		end
	end
end
2 Likes
if not hit.Parent:IsA("Accessory") or not hit.Name == "Handle" or not hit.Parent:IsA("Accoutrement") or not hit.Parent:IsA("Tool") then

This line of code (Up) is completely useless as you already use this one (Down) which automatically exclude everything that are not a body part.

if hit.Parent:FindFirstChild("Humanoid") then
2 Likes

Thanks i test it its working :grinning: