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
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)
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)
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
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
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