Error with my script, check if something exists?

Like I’m trying to check if the humanoid exists, otherwise don’t run the below code. Not sure what I’m doing wrong?


if not enemyHumanoid.Parent:FindFirstChild("HumanoidRootPart") then return end
				bloodEffect.Parent = enemyHumanoid.Parent.HumanoidRootPart
				game:GetService("Debris"):AddItem(bloodEffect, 3)

Error:

Workspace.NPCs.Team2.KnightTeam2.Script:44: attempt to index nil with 'FindFirstChild' - Server - Script:44

2 Likes

probably killed and removed before the code ran, happened to me before

local bloodPart = Instance.new("Part")
bloodPart.Size = Vector3.new(0.1, 0.1, 0.1)
bloodPart.Anchored = true
bloodPart.Transparency = 1
bloodPart.CanCollide = false
bloodPart.CFrame = enemy:GetPivot().Position 
-- I guessed, the variable won't go nil, it just sets the parent to nil
bloodPart.Parent = workspace

bloodEffect.Parent = bloodPart
game:GetService("Debris"):AddItem(bloodPart, 3)

Add before the first check:

if not enemyHumanoid.Parent then return end
if not (enemyHumanoid and enemyHumanoid.Parent and enemyHumanoid.Parent:FindFirstChild("HumanoidRootPart")) then return end

bloodEffect.Parent = enemyHumanoid.Parent.HumanoidRootPart
game:GetService("Debris"):AddItem(bloodEffect, 3)

Yea even if I add these, if the instance is nil then it errors. Idk

It seems like the problem is that you’re trying to run FindFirstChild() with an instance that doesn’t exist. Maybe you can try to check if “enemyHumanoid.Parent” exists first:

if not enemyHumanoid.Parent or not enemyHumanoid.Parent:FindFirstChild("HumanoidRootPart") then return end
    bloodEffect.Parent = enemyHumanoid.Parent.HumanoidRootPart
    game:GetService("Debris"):AddItem(bloodEffect, 3)

I tried that too haha, same error.

Error is literal, enemyhumanoid doesn’t exist. Have you tried debugging?

Can you give me the path (or variable) that represents enemyHumanoid.Parent? With that I may be able to fix it.

Still errors, btw it’s 100% that it errors because it doesn’t exist. But isn’t that what this if statement is supposed to check for.

if not enemyHumanoid and not enemyHumanoid.Parent and not  nemyHumanoid.Parent:FindFirstChild("HumanoidRootPart") then return end
enemyHumanoid:TakeDamage(55)
				bloodEffect.Parent = enemyHumanoid.Parent.HumanoidRootPart
				game:GetService("Debris"):AddItem(bloodEffect, 3)
			break
		else

Can you provide the entire script / function where the error occurs

The thing is that if enemyHumanoid is nil, it won’t have a parent, therefore you would need to check if the parent exists first, then enemyHumanoid, and then HumanoidRootPart.

task.wait(5)
print("running")




humanoid = script.Parent:WaitForChild("Humanoid")
destination = workspace:WaitForChild("EnemyDestination2")





local Animator = humanoid:WaitForChild("Animator")
local shockAnimation = Instance.new("Animation")
shockAnimation.AnimationId = "rbxassetid://12201480713"
local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
shockAnimationTrack.Priority = Enum.AnimationPriority.Action
shockAnimationTrack.Looped = true
shockAnimationTrack:Play()

function scanArea()
	local bloodEffect = game:GetService("ReplicatedStorage"):WaitForChild("Effects"):WaitForChild("Blood"):WaitForChild("Attachment"):Clone()
	local NPCs = workspace:WaitForChild("NPCs")
	local team1 = NPCs:WaitForChild("Team1")
	print("Scanning")
	local gatherPlayers = team1:GetChildren()
	if #gatherPlayers == 0 then
		humanoid:MoveTo(destination.Position)
		--print("0")
	end
	for i = 1, #gatherPlayers do
		if (gatherPlayers[i]:WaitForChild("HumanoidRootPart").Position - humanoid.RootPart.Position).Magnitude < 200 and
			(gatherPlayers[i]:WaitForChild("HumanoidRootPart").Position - humanoid.RootPart.Position).Magnitude > 25 
			and
			humanoid.Parent ~= gatherPlayers[i] then
			--print("In range attack")
			humanoid:MoveTo(gatherPlayers[i]:WaitForChild("HumanoidRootPart").Position)
			break
		elseif (gatherPlayers[i]:WaitForChild("HumanoidRootPart").Position - humanoid.RootPart.Position).Magnitude < 25 and humanoid.Parent ~= gatherPlayers[i] then
			--print("Damage")
			local enemyHumanoid = gatherPlayers[i]:WaitForChild("Humanoid")
			
			if not enemyHumanoid and not enemyHumanoid.Parent and not enemyHumanoid.Parent:FindFirstChild("HumanoidRootPart") then return end
enemyHumanoid:TakeDamage(55)
				bloodEffect.Parent = enemyHumanoid.Parent.HumanoidRootPart
				game:GetService("Debris"):AddItem(bloodEffect, 3)
				
			break
		else
			-- continue to castle
			--print("go to castle")
			humanoid:MoveTo(destination.Position)
			break
		end
	end
end

humanoid.HealthChanged:Connect(function(health)
	if health <= 0 then
		script.Parent:Destroy()
	end
end)


while true do
	wait(2)
	scanArea()
end

Maybe this will work?:

if not workspace.NPCs.team1:FindFirstChild(gatherPlayers[i].Name) or not gatherPlayers[i]:FindFirstChild("Humanoid") or not gatherPlayers[i]:FindFirstChild("HumanoidRootPart)

Edit: I forgot to mention that it is essential to use the or operator because and will check everything, thus, causing the same error, because of the use of :FindFirstChild() on nil