Workspace.Dummy.FollowAndAttack:7: attempt to index nil with 'Humanoid'?
Asked by
1 year ago Edited 1 year ago
So I have a script that checks if the humanoid exists and then attacks it, but I get the error:
Workspace.Dummy.FollowAndAttack:7: attempt to index nil with 'Humanoid' on line 7 and I don't know how to fix it.
Here's the line that the error is on:
if v and v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v.Humanoid and v.Humanoid.DisplayName ~= script.Parent.Humanoid.DisplayName and v.Humanoid.Health > 0 and script.Parent.Humanoid.Health ~= 0 then
And here's the full code:
02 | local maxDistance = 1000 |
03 | local PathfindingService = game:GetService( "PathfindingService" ) |
05 | local nearestCharacter, nearestDistance |
06 | for i, v in pairs (workspace:GetChildren()) do |
07 | if v and v:FindFirstChild( "Humanoid" ) and v:FindFirstChild( "HumanoidRootPart" ) and v ~ = script.Parent and v.Humanoid and v.Humanoid.DisplayName ~ = script.Parent.Humanoid.DisplayName and v.Humanoid.Health > 0 and script.Parent.Humanoid.Health ~ = 0 then |
09 | local distance = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude |
10 | if not character or distance > maxDistance or (nearestDistance and distance > = nearestDistance) |
14 | nearestDistance = distance |
16 | local Destination = v.HumanoidRootPart.Position |
17 | local Path = PathfindingService:CreatePath() |
18 | local NPC = script.Parent |
19 | local Humanoid = NPC.Humanoid |
20 | Path:ComputeAsync(NPC.PrimaryPart.Position, Destination) |
22 | local activeCoroutine = nil |
26 | coroutine.wrap( function () |
35 | if script.Parent.Humanoid.Health ~ = 0 then |
36 | Path:ComputeAsync(NPC.PrimaryPart.Position, Destination) |
39 | local Waypoints = Path:GetWaypoints() |
40 | table.remove(Waypoints, 1 ) |
42 | coroutine.wrap(followPath)(Waypoints) |
44 | function followPath(waypoints) |
45 | activeCoroutine = coroutine.running() |
46 | for _, point in ipairs (waypoints) do |
47 | if activeCoroutine~ = coroutine.running() then |
50 | Humanoid:MoveTo(point.Position) |
51 | Humanoid.MoveToFinished:Wait() |
52 | if point.Action = = Enum.PathWaypointAction.Jump then |
57 | local viewray = Ray.new(script.Parent.Head.Position, nearestCharacter.Head.Position) |
58 | local hit, position = workspace:FindPartOnRayWithIgnoreList(viewray, { script.Parent } ) |
59 | local gunray = Ray.new(script.Parent.AssaultRifle.Shoot.Position, nearestCharacter.Head.Position + Vector 3. new(math.random( 0 , 2 ), math.random( 0 , 2 ), math.random( 0 , 2 ))) |
60 | local hit 1 , position 1 = workspace:FindPartOnRayWithIgnoreList(gunray, { script.Parent } ) |
62 | if hit and (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 900 then |
63 | script.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, nearestCharacter.Head.Position) |
64 | local anim = script.Parent.Humanoid:LoadAnimation(script.Pew) |
66 | script.Parent.AssaultRifle.Shoot.Shot:Play() |
67 | script.Parent.AssaultRifle.Muzzle.Muzzle 1 :Emit( 15 ) |
68 | script.Parent.AssaultRifle.Muzzle.Muzzle 2 :Emit( 15 ) |
69 | script.Parent.AssaultRifle.Muzzle.Muzzle 3 :Emit( 15 ) |
70 | script.Parent.AssaultRifle.Muzzle.MuzzleFlash:Emit( 15 ) |
71 | script.Parent.AssaultRifle.Muzzle.Muzzle.Enabled = true |
72 | if (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 900 and hit and hit 1 then |
73 | if hit 1. Parent:FindFirstChild( "Humanoid" ) then |
74 | hit 1. Parent.Humanoid:TakeDamage( 30 ) |
79 | if script.Parent.Humanoid.Health = = 0 then |
83 | script.Parent.FollowAndAttack:Destroy() |