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:

local d = false
local maxDistance = 1000
local PathfindingService = game:GetService("PathfindingService")
while wait() do
	local nearestCharacter, nearestDistance
	for i, v in pairs(workspace:GetChildren()) do
		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
			character = v
			local distance = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude
			if not character or distance > maxDistance or (nearestDistance and distance >= nearestDistance)
			then
				continue
			end
			nearestDistance = distance
			nearestCharacter = v
			local Destination = v.HumanoidRootPart.Position
			local Path = PathfindingService:CreatePath()
			local NPC = script.Parent
			local Humanoid = NPC.Humanoid
			Path:ComputeAsync(NPC.PrimaryPart.Position, Destination)

			local activeCoroutine = nil
			local follow=true

			-- Loop update
			coroutine.wrap(function()
				while (follow) do
					wait(1/3)
					update()
				end
			end)()


			function update()
				if script.Parent.Humanoid.Health ~= 0 then
					Path:ComputeAsync(NPC.PrimaryPart.Position, Destination)
				end

				local Waypoints = Path:GetWaypoints()
				table.remove(Waypoints, 1)  -- First waypoint seems to always be origin

				coroutine.wrap(followPath)(Waypoints)
			end
			function followPath(waypoints)
				activeCoroutine = coroutine.running()
				for _, point in ipairs(waypoints) do
					if activeCoroutine~=coroutine.running() then
						return end

					Humanoid:MoveTo(point.Position)
					Humanoid.MoveToFinished:Wait()
					if point.Action==Enum.PathWaypointAction.Jump then
						Humanoid.Jump = true 
					end
				end
			end
			local viewray = Ray.new(script.Parent.Head.Position, nearestCharacter.Head.Position)
			local hit, position = workspace:FindPartOnRayWithIgnoreList(viewray, {script.Parent})
			local gunray = Ray.new(script.Parent.AssaultRifle.Shoot.Position, nearestCharacter.Head.Position + Vector3.new(math.random(0, 2), math.random(0, 2), math.random(0, 2)))
			local hit1, position1 = workspace:FindPartOnRayWithIgnoreList(gunray, {script.Parent})
			local mag = 30
			if hit and (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 900 then
				script.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, nearestCharacter.Head.Position)
				local anim = script.Parent.Humanoid:LoadAnimation(script.Pew)
				anim:Play()
				script.Parent.AssaultRifle.Shoot.Shot:Play()
				script.Parent.AssaultRifle.Muzzle.Muzzle1:Emit(15)
				script.Parent.AssaultRifle.Muzzle.Muzzle2:Emit(15)
				script.Parent.AssaultRifle.Muzzle.Muzzle3:Emit(15)
				script.Parent.AssaultRifle.Muzzle.MuzzleFlash:Emit(15)
				script.Parent.AssaultRifle.Muzzle.Muzzle.Enabled = true
				if (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 900 and hit and hit1 then
					if hit1.Parent:FindFirstChild("Humanoid") then
						hit1.Parent.Humanoid:TakeDamage(30)
					end
				end
			end
		end
		if script.Parent.Humanoid.Health == 0 then
			break
		end
	end
	script.Parent.FollowAndAttack:Destroy()
	end

7 comments

0

why are you using v:FindFirstChild("Humanoid") and then v.Humanoid?

0

Hey! I see your problem. in the for loop at line 6 it says "workspace:GetChidren()". What are you trying to do? maybe use :GetDescendants().

0

I'm trying to see if the children of the workspace have a humanoid.

0

yes i know that lol

0

I think its something about the script.Parent.Humanoid.Health

0

Removed that, and it still gives me the same error

0

replace line 7 with if v and v:FindFirstChildOfClass("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v:FindFirstChildOfClass("Humanoid").DisplayName ~= script.Parent:FindFirstChildOfClass("Humanoid").DisplayName and v:FindFirstChildOfClass("Humanoid").Health > 0 and script.Parent:FindFirstChildOfClass("Humanoid").Health > 0 then