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.
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)
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)