HumanoidRootPart is not a valid member of Model

Hello there,

I’m trying to make a tower defense game. I already make the towers and the enemies. It all work perfectly until there’s an error in my output. It says HumanoidRootPart is not a valid member of Model. I don’t know why. I tried adding an if line but it’s gonna break the for loop.

Here are the code:

function Round.patrol(map,enemy)
	local humanoid = enemy:WaitForChild("Humanoid")
	local paths = map.Paths

	-- using tween service method
	local TweenService = game:GetService("TweenService")
	
	for i=1, #paths:GetChildren() do
		if i > 1 then
			local at = enemy.HumanoidRootPart.Position
			local lookAt = paths[i].Position
			local cframe = CFrame.lookAt(at,lookAt)
			local tween = TweenService:Create(enemy.HumanoidRootPart.BodyGyro,TweenInfo.new(0.5),{CFrame = cframe})
			tween:Play()
			tween.Completed:Wait()
		end
		
		local distance = (enemy.HumanoidRootPart.Position - paths[i].Position).Magnitude
		local tweenInfo = TweenInfo.new(
			distance / enemy.Humanoid.WalkSpeed,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.InOut,
			0,
			false,
			0
		)
		local offset = enemy["Left Leg"].Size.Y + (enemy.HumanoidRootPart.Size.Y/2)
		local cframe = paths[i].CFrame * CFrame.new(0,offset,0)
		local tween = TweenService:Create(enemy.HumanoidRootPart,tweenInfo,{CFrame = cframe})
		tween:Play()
		tween.Completed:Wait()
	end
	
	-- deal damage
	local damage = enemy.Humanoid.MaxHealth
	map.Base.Humanoid:TakeDamage(damage)
	
	-- destroy enemy
	events.destroyHealthGui:FireAllClients(enemy.Head)
	enemy:Destroy()
end

Please tell me how to fix it. I appreciate your helps. :]

You might have to wait for the HumanoidRootPart to load by using :WaitForChild("HumanoidRootPart", timeOut).

ah yes I forgot to mention that I’m saying that it’s giving me the error after the enemy died

That is because when a character dies, the character parts lose their joints and since the HRP is non-collideable, it will fall through the floor. That is what I am assuming is happening when you use the default roblox death breakjoints().

1 Like

I tried setting the BreakJointsOnDeath to false and it still happening

Did you disable it on the client or server?
I also want to ask, did you check if the humanoid root part was in the explorer after the enemy died?

I did. But the humanoid root part destroyed along with the enemy

Yeah there’s not a lot of information that’s provided to pinpoint or narrow down where the problem underlies. I mean, you have four different areas where HumanoidRootPart is being pathed and then you have a lot of enemies on the map, so it’s hard to determine which is exactly the issue.

What you could do to minimize the footprint and perhaps even reduce the checks, you can implement Debris Service which acts like Destroy() but cleans up nicer because it has a delay after it reads through the entire function. Another way is to define the humanoidrootpart before you run the for loop. Your tweening could also be delaying the process in which your script runs during the destroy. You’re defining a tween info variable for each index on the loop, so that’s a memory leak itself.

I’d try to see if any of those would work or help narrow it down, but other than that, I have no idea how to assist other than trying to add an if statement.