"HumanoidRootPart is not a valid member of stats?"

I was trying to script an Npc for the first time, and came across an error. I don’t know if this is roblox being wacky but it said that humanoidrootpart and humanoid isn’t a child of status. I know it isn’t supposed to be a valid member of status but rather its supposed to be a valid member of the character instead of status. The error is on line 12.

Script:

local pathfindingservice = game:GetService("PathfindingService")
local dummy = script.Parent
local dummyHumanoid = script.Parent:WaitForChild("Humanoid")
local dummyRootPart = script.Parent:WaitForChild("HumanoidRootPart")

local target = nil

function findTarget()
	if dummyHumanoid.Health > 0 then
		for i,v in pairs(game.Workspace:GetDescendants()) do
			if v:IsA("Model") then
			if v.Name ~= dummy.Name and v.HumanoidRootPart and v.Humanoid.Health > 0 then
				vRootPart = v.HumanoidRootPart
				vHumanoid = v.Humanoid
				local path = pathfindingservice:CreatePath()
					path:ComputeAsync(dummyRootPart.Position,v.HumanoidRootPart.Position)
					if path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()
				for i,waypoint in pairs(waypoints) do
					if check() <= 100 then
						dummyHumanoid:MoveTo(waypoints)
						dummyHumanoid.MoveToFinished:Wait()
							end
						end
					end
				end
			end
		end
	end
end

function check()
	if vHumanoid.Health > 0 then
		magnitude = (dummyRootPart.Position - vRootPart.Position).Magnitude 
		return magnitude
	end
end

while wait() do
	findTarget()
end

Instead of indexing a part to check for validity, use :FindFirstChild(), which will check if there IS a part with the name provided.

if v.Name ~= dummy.Name and v:FindFirstChild("HumanoidRootPart") and v:FindFirstChild("Humanoid").Health > 0 then

Your code assumes that in every model in the workspace, there is a part called HumanoidRootPart, which may throw an error

1 Like

Thank you for helping! It is fixed

1 Like