Make NPC stand up / not fall

I made a simple pathfinding NPC that will walk toward the player it detects. The only problem is that when the NPC is knocked over it will stay knocked over and just walk in place. Any fixes? below is the code and a screenshot of the NPC falling over.
image

RunService.Heartbeat:Connect(function()
	local nearestPlayer, distance, direction = findNearestPlayer()
	if nearestPlayer then
		if distance <= targetDistance and distance >= stopDistance then
			-- runs when walking
			if not walkingTrack.IsPlaying then
				walkingTrack:Play()
			end
			humanoid:Move(direction)
		else
			-- runs when standing still
			if walkingTrack.IsPlaying then
				walkingTrack:Stop()
			end
			humanoid:Move(Vector3.new())
		end
end)
1 Like

Give it a jump mechanic when it’s orientation changes which could also be used to traverse other obstacles.

1 Like

i uh, don’t know how to :crying_cat_face: :crying_cat_face: :crying_cat_face: :crying_cat_face: :crying_cat_face:

Do you have AutoJumpEnabled set to true for your NPC Humanoid?
A humanoid should try to stand upright during normal movement, unless someone’s messed with the Humanoid Properties or maybe the Density of the Parts in the avatar.

auto jump is enabled, this is what the full code looks like

function findNearestPlayer()
local playerList = Players:GetPlayers()

local nearestPlayer = nil
local distance = nil
local direction = nil

for _, player in pairs(playerList) do
	local character = player.Character
	if character then
		local distanceVector = (player.Character.HumanoidRootPart.Position - root.Position)
		if not nearestPlayer then
			nearestPlayer = player
			distance = distanceVector.Magnitude
			direction = distanceVector.Unit
		elseif distanceVector.Magnitude < distance then
			nearestPlayer = player
			distance = distanceVector.Magnitude
			direction = distanceVector.Unit
		end	
	end
end

return nearestPlayer, distance, direction

end

RunService.Heartbeat:Connect(function()
local nearestPlayer, distance, direction = findNearestPlayer()
if nearestPlayer then
if distance <= targetDistance and distance >= stopDistance then
– runs when walking
if not walkingTrack.IsPlaying then
walkingTrack:Play()
end
humanoid:Move(direction)
else
– runs when standing still
if walkingTrack.IsPlaying then
walkingTrack:Stop()
end
humanoid:Move(Vector3.new())
end
end)

Just a guess, but you aren’t checking to see if the player is running, so when they are your code doesn’t run.

Also, try
print distance, " ",targetDistance, " ", stopDistance
right before this line
if distance <= targetDistance and distance >= stopDistance then