Npc Runns in Place and cant walk up Stairs

Hello Fellow Devs :wave:
its me again so being my first time making proper movement system i ran into some issuses first my Npc run in place they dont move waht so ever and secong some of the smaller stairs the npc cant run upwards and they usually if the enemy is above them stand under there position instead of using the Stairs can someone explain me what im doing wrong?:

	local Moving = false
	
	local function Movement(targetPosition,Running)
		if isStunned.Value then return end
		if Moving then return end
		
		Moving = true
		
		if Running == nil then
			hum:MoveTo(root.Position) -- cancel movement
			hum.WalkSpeed = 0
			StopMovementAnimations() -- to stop animation
			Moving = false
			return
		end
		
		
		
		if Npc.Torso:FindFirstChild("MoveSound") then
			Movesound = Npc.Torso.MoveSound
			Movesound.Looped = true
			Movesound.Volume = 0.3
			
		else
			Movesound = Instance.new("Sound")
			Movesound.Looped = true
			Movesound.Parent = Npc.Torso
			Movesound.Volume = 0.3
			Movesound.Name = "MoveSound"
			Movesound.SoundId = "rbxassetid://1181265898"
		end
		
		

		
		if Running then
			hum.WalkSpeed = CharStats.SprintSpeed * CharStats.SpeedIncreaseDecrease
			if WeaponStats.WeponClass == "Spear" then
				--IdleTrack.Priority = Enum.AnimationPriority.Action2
			end

			if not runTrack.IsPlaying then
				walkTrack:Stop()
				runTrack:Play()
				Movesound:Play()
				Movesound.PlaybackSpeed = 1.4
			end
		else
			hum.WalkSpeed = CharStats.BaseWalkSpeed * CharStats.SpeedIncreaseDecrease
			if WeaponStats.WeponClass == "Spear" then
				--IdleTrack.Priority = Enum.AnimationPriority.Action
			end
			
			if not walkTrack.IsPlaying then
				runTrack:Stop()
				walkTrack:Play()
				Movesound:Play()
				Movesound.PlaybackSpeed = 1

			end
			
		end
		

		

		
		
		local Path = Pathfinding:CreatePath({
			AgentRadius = 2,
			AgentHeight = 10,
			AgentCanJump = true,
			AgentCanClimb = true,
			AgentJumpHeight = 10,
			cost = {
				CrackedLava = 2,
				Cobbelstone = 1,
				Brick = 1,
				Grass = 2,
				Ground = 1,
				Mud = 1,	
				Slate = 1,
				Rock = 2,
				LeafyGrass = 1,
				Climb = 0.5,
				ClimbHelper = 0.4,
				Stairs = 0.1,
				DangerZone = math.huge,
				Water = math.huge,
				
			},
		})
		Path:ComputeAsync(root.Position, targetPosition)

		
		local blocked = false
		
		Path.Blocked:Connect(function()
			blocked = true
		end)

		if Path.Status == Enum.PathStatus.Success then
			local waypoints = Path:GetWaypoints()
			for _, waypoint in ipairs(waypoints) do
				local distance = (root.Position - targetPosition).Magnitude
				local part = Instance.new("Part")
				part.Material = "Neon"
				part.Anchored = true
				part.CanCollide = false
				part.Shape = "Ball"
				part.Position = waypoint.Position
				part.Parent = game.Workspace
					
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true
				end
				

				
				if blocked then
					Moving = false
					return
				end



				if distance < 70 then 
					EnemyNpc = nil
					closestEnemy = nil
					closestDistance = math.huge
					detected = false
					break
				end
				
				hum:MoveTo(waypoint.Position)
				
				
				local Reached = hum.MoveToFinished:Wait(0.1)
				
				if not Reached then
					Moving = false
					return
				end
				
				RunService.Heartbeat:Wait()
			end
			Moving = false
		else
			warn(Npc.Name .. " failed to pathfind.")
		end
		Moving = false
	end

i hope you can help me :saluting_face:

1 Like
local Reached = hum.MoveToFinished:Wait(0.1)			
if not Reached then
   Moving = false
   return
end

Seem a bit quick, wait a 10th of a second then give up the whole path.

You got some things wrong:

  1. MoveToFinished timeout too short

2.Distance check compares to final target instead of waypoint

  1. Path blocked handling isn’t robust

  2. Agent size mismatch

  3. Jump timing

  4. Blocked / collision issues with debug parts

  5. Running in place

  6. Distance threshold too large

try this code instead it handles stairs, jumps, blocked paths, animations, and movement sounds properly

lemme know if it works I didn’t get a chance to test it btw.

local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")

local Moving = false

local function Movement(targetPosition, Running)
	if isStunned.Value then return end
	if Moving then return end
	Moving = true

	
	if Running == nil then
		hum:MoveTo(root.Position)
		hum.WalkSpeed = 0
		StopMovementAnimations()
		Moving = false
		return
	end

	
	local moveSound = Npc.Torso:FindFirstChild("MoveSound")
	if not moveSound then
		moveSound = Instance.new("Sound")
		moveSound.Name = "MoveSound"
		moveSound.Parent = Npc.Torso
		moveSound.Looped = true
		moveSound.Volume = 0.3
		moveSound.SoundId = "rbxassetid://1181265898"
	end

	
	if Running then
		hum.WalkSpeed = CharStats.SprintSpeed * CharStats.SpeedIncreaseDecrease
		if not runTrack.IsPlaying then
			walkTrack:Stop()
			runTrack:Play()
			moveSound:Play()
			moveSound.PlaybackSpeed = 1.4
		end
	else
		hum.WalkSpeed = CharStats.BaseWalkSpeed * CharStats.SpeedIncreaseDecrease
		if not walkTrack.IsPlaying then
			runTrack:Stop()
			walkTrack:Play()
			moveSound:Play()
			moveSound.PlaybackSpeed = 1
		end
	end

	
	local path = PathfindingService:CreatePath({
		AgentRadius = 2,
		AgentHeight = 5,
		AgentCanJump = true,
		AgentJumpHeight = 10,
		AgentMaxSlope = 45,
		AgentCanClimb = true
	})
	path:ComputeAsync(root.Position, targetPosition)

	if path.Status ~= Enum.PathStatus.Success then
		warn(Npc.Name .. " failed to pathfind.")
		Moving = false
		return
	end

	local waypoints = path:GetWaypoints()

	for _, waypoint in ipairs(waypoints) do
		
		if isStunned.Value then
			Moving = false
			return
		end

		
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
			repeat RunService.Heartbeat:Wait() until hum.FloorMaterial ~= Enum.Material.Air
		end

		 
		hum:MoveTo(waypoint.Position)
		local reached = hum.MoveToFinished:Wait() 

		if not reached then
			
			path:ComputeAsync(root.Position, targetPosition)
			waypoints = path:GetWaypoints()
			break
		end
	end

	Moving = false
end


1 Like

Thank you soo much that helped a lot i figured out on stairs the wedge has can collide to be on and the stairs has to be off for them to walk properly on it and managed to fix a lot and they repath if fixed version:

	local Path = Pathfinding:CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = true,
			AgentCanClimb = true,
			AgentJumpHeight = 10,
			AgentMaxSlope = 45,-- that helped a lot too
			cost = {
				CrackedLava = 2,
				Cobbelstone = 1,
				Brick = 1,
				Grass = 2,
				Ground = 1,
				Mud = 1,	
				Slate = 1,
				Rock = 2,
				LeafyGrass = 1,
				Climb = 0.5,
				ClimbHelper = 0.4,
				Stairs = 0.1,
				DangerZone = math.huge,
				Water = math.huge,
				
			},
		})
		Path:ComputeAsync(root.Position, targetPosition)


		if Path.Status == Enum.PathStatus.Success then
			local waypoints = Path:GetWaypoints()
			for _, waypoint in ipairs(waypoints) do
				local distance = (root.Position - waypoint.Position).Magnitude
				local part = Instance.new("Part")
				part.Material = "Neon"
				part.Anchored = true
				part.CanCollide = false
				part.Shape = "Ball"
				part.Position = waypoint.Position
				part.Parent = game.Workspace
					
				if waypoint.Action == Enum.PathWaypointAction.Jump then
					hum.Jump = true
				end

				hum:MoveTo(waypoint.Position)

				
				local Reached = hum.MoveToFinished:Wait(10) -- repath increased

				if not Reached then
					Path:ComputeAsync(root.Position, targetPosition)
					waypoints = Path:GetWaypoints()
					break
				end
				
				if distance >= 70 then -- and distance check also
					EnemyNpc = nil
					closestEnemy = nil
					closestDistance = math.huge
					detected = false
					break

				end
				
				RunService.Heartbeat:Wait()
			end
			Moving = false
		else -- here is it if succes failed
			warn(Npc.Name .. " failed to pathfind.")
			Moving = false
			return
		end

		Moving = false

Thanks for the help Apreciate it :saluting_face:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.