Adding a pathfinding system

local PARTS = MovementParts:GetChildren()

local IsStunned = false
local StunDuration = 5


local function WaiterMoving()
	
	DayFootSteps.Playing = true
	DayAnim:Play()
	
	TouchedLightEvent.OnServerEvent:Connect(function()

		if IsLightValue.Value == false and IsStunned == false then
			IsStunned = true
			ScreamSound:Play()
			Humanoid.WalkSpeed = 0
			DayAnim:Stop()
			NightAnim:Stop()
			StunAnim:Play()
			WhisperingSound.Playing = false
			DayFootSteps.Playing = false
			NightFootSteps.Playing = false

			wait(StunDuration)

			IsStunned = false
			Humanoid.WalkSpeed = 13
			StunAnim:Stop()
			if IsLightValue.Value == true then
				DayFootSteps.Playing = true
				DayAnim:Play()
			end
			if IsLightValue.Value == false then
				WhisperingSound.Playing = true
				NightFootSteps.Playing = true
				NightAnim:Play()
			end
		end	

	end)
	
	IsLightValue.Changed:Connect(function()
		if IsLightValue.Value == true then
			WhisperingSound.Playing = false
			DayFootSteps.Playing = true
			NightFootSteps.Playing = false
			NightAnim:Stop()
			DayAnim:Play()
			Humanoid.WalkSpeed = 13
		end
		if IsLightValue.Value == false then
			WhisperingSound.Playing = true
			DayFootSteps.Playing = false
			NightFootSteps.Playing = true
			DayAnim:Stop()
			NightAnim:Play()
			Humanoid.WalkSpeed = 13
		end
	end)

	
	while NPCSCanWalkValue.Value == true do
		wait()
		if IsStunned == false then
			local randomPart = PARTS[math.random(1, #PARTS)]
			local randomWait = math.random(20, 80) / 100

			wait(randomWait)
			
			Humanoid:MoveTo(randomPart.Position)
			Humanoid.MoveToFinished:Wait()

		else

		end	
	end
end

StartNPCSEvent.Event:Connect(WaiterMoving)

This code works perfectly well until the npc runs into a wall.

I don’t want this npc to chase or do damage, all I want is for it to go around walls with the use of PathFindingService, how do I do that?

Any help is appreciated!

4 Likes

Basically, you will be using PathfindingService to compute a path so it doesn’t run into walls.

Basic Pathfinding Script:

local PS = game:GetService("PathfindingService")
local path = PS:CreatePath()

local HumanoidRP = --reference to humrp
local Humanoid = nil --reference to humanoid
local points = {workspace.Part1, workspace.Part2} -- points to move to

while true do
    local randomPos = points[math.random(1, #points)].Position
    path:ComputeAsync(HumanoidRP.Position, randomPos)
    local waypoints = path:GetWaypoints()
    for i, v in waypoints do
        Humanoid:MoveTo(v.Position)
        Humanoid.MoveToFinished:Wait()
    end
    task.wait(1)
end
2 Likes

Sorry for the late response, but this didn’t work.

while NPCSCanWalkValue.Value == true do
		wait()
		if IsStunned == false then
			
			local randomPart = PARTS[math.random(1, #PARTS)]
			local randomWait = math.random(20, 80) / 100
            print("1") -- Prints
			wait(randomWait)
			
			path:ComputeAsync(HumanoidRP.Position, randomPart.Position)
			local waypoints = path:GetWaypoints()
            print("2") -- Prints
			for i, v in waypoints do
                print("3") -- Doesn't Print
				Humanoid:MoveTo(v.Position)
				Humanoid.MoveToFinished:Wait()
                print("4") -- Doesn't Print
			end
		end
	end

What I put instead of my other while loop.
There are no errors in output, and the npc just stands there.

What else do I do???

try adding a status check

if path.Status ~= "Success" then continue end
--------------------------

while true do
    local randomPos = points[math.random(1, #points)].Position
    path:ComputeAsync(HumanoidRP.Position, randomPos)
    if path.Status ~= "Success" then continue end
    local waypoints = path:GetWaypoints()
    for i, v in waypoints do
        Humanoid:MoveTo(v.Position)
        Humanoid.MoveToFinished:Wait()
    end
    task.wait(1)
end

Also make sure the NPC isn’t anchored

1 Like
while NPCSCanWalkValue.Value == true do

		if IsStunned == false then
			
			local nextindex = 0
			
			print("Not Stunned")
			local randomPart = PARTS[math.random(1, #PARTS)]
			local randomWait = math.random(20, 80) / 100
			print(tostring(randomPart).." Is the Part to Walk To")
			wait(randomWait)
			
			local Character = Waiter
			
			path:ComputeAsync(Character.PrimaryPart.Position, randomPart.Position)
			print("Computed Path")
			
			local waypoints = path:GetWaypoints()
			nextindex += 1
			for i, waypoint in pairs(waypoints) do
				print("nextindex Set to: 1")
				Humanoid:MoveTo(waypoint.Position) 
				print("Moved")
				Humanoid.MoveToFinished:Wait()
			end
			
		else
			task.wait()
			warn("Stunned")
		end
	end
	task.wait()
end

I was able to fix it, thank you for your help!

Can you mark my answer as aa solution?

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