Pathfinding script not working

  1. What do you want to achieve?
    Get the NPC to move to the part in workspace

  2. What is the issue?
    The NPC just stands still and doesn’t move, it isn’t anchored.

  3. What solutions have you tried so far?
    None, I added print statements to every line and it prints all of them except for the one after humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position), there is nothing in the output.

local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")

function roaming()
		local pathFindingService = game:GetService("PathfindingService")

		local rootPart = humanoid.Parent.HumanoidRootPart
		local path = pathFindingService:CreatePath()
		path:ComputeAsync(rootPart.Position, workspace.Map.Rooms.TestRoom.Spawn.Position)

		local waypoints = path:GetWaypoints()

		for i, waypoint in pairs(waypoints) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait(2)
		end

		humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position)
end

roaming()

image

image

2 Likes

looking at your script, it doesnt appear to print anything

Edit:

Try this:

local humanoid = script.Parent:FindFirstChild("Humanoid")

function roaming()
		local pathFindingService = game:GetService("PathfindingService")

		local rootPart = humanoid.Parent.HumanoidRootPart
		local path = pathFindingService:CreatePath()
		path:ComputeAsync(rootPart.Position, workspace.Map.Rooms.TestRoom.Spawn.Position)

		local waypoints = path:GetWaypoints()

		for i, waypoint in pairs(waypoints) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait(2)
		end

		humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position)
end

while wait() do
roaming()
end

Why would that change anything? You’re just executing the function repeatedly. It didn’t work.

you usually repeat it so the Pathfind constantly updates. so it doesnt freeze within the 8 seconds of MoveToFinished

1 Like

Strange, after testing with this script, it works :confused:

local humanoid = script.Parent.Humanoid
local pathFindingService = game:GetService("PathfindingService")

function roaming()
	

	local rootPart = humanoid.Parent.HumanoidRootPart
	local path = pathFindingService:CreatePath()
	path:ComputeAsync(rootPart.Position, workspace.SpawnLocation.Position)

	local waypoints = path:GetWaypoints()

	for i, waypoint in pairs(waypoints) do
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait(2)

	end

	humanoid:MoveTo(workspace.SpawnLocation)
end

while wait() do
	roaming()
end

I think the issue is because I have another while loop already in the script so the one that keeps running the function doesn’t work. I didn’t provide the full script in the original post but do you know how I could optimize this so it would work?

local players = game:GetService("Players")
local gameMain = game.ServerScriptService.GameMain

local status = {
	--[[1]]	"Idling", -- The ghost stays still
	--[[2]]	"Roaming", -- The ghost picks a random point within a defined sphere around it, then checks if the pathfinding distance is less than 5 or 10 metres. If successful, the ghost walks to that point, else it repeats the steps
	--[[3]]	"Interaction", -- The ghost interacts with an object nearby
	--[[4]]	"GhostEvent", -- The ghost performs a ghost event
	--[[5]]	"Hunt", -- The ghost initiates a hunt
	--[[6]]	"Ability" -- The ghost uses its ability, if any
}

local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")

local currentStatus = 0

function setActivity(number) -- Sets the activity in the truck to the given number 0-10
	gameMain.Variables.GhostActivity.Value = number
end

function ghostVisiblity(value)
	if value == true then
		for i, part in pairs(script.Parent:GetChildren()) do
			if part:IsA("BasePart") then
				part.Transparency = 0
			end
		end
	end
end

function idling()
	setActivity(0)
end

function roaming()
	local isRoaming = false
	
	spawn(function()
		isRoaming = true
		setActivity(math.random(5, 7))
		wait(20)
		isRoaming = false
		setActivity(0)
	end)
	
	repeat 
		local pathFindingService = game:GetService("PathfindingService")
		
		local rootPart = humanoid.Parent.HumanoidRootPart
		local path = pathFindingService:CreatePath()
		path:ComputeAsync(rootPart.Position, workspace.Map.Rooms.TestRoom.Spawn.Position)
		
		local waypoints = path:GetWaypoints()
		
		for i, waypoint in pairs(waypoints) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait(2)
		end
		
		humanoid:MoveTo(workspace.Map.Rooms.TestRoom.Spawn.Position)
		
	until isRoaming == false
end

print("attempted to roam")
while wait() do
	roaming()
end

function initiateHunt()

end

function ghostEvent()
	
end

while wait(gameMain.Variables.AverageSanityLoss.Value / 3) do
	if gameMain.Variables.AverageSanityLoss.Value >= 95 then
		spawn(function()
			wait(15)
			repeat wait() until currentStatus == status[1]
			if gameMain.Variables.AverageSanity.Value <= 40 then
				local randomNumber = math.random()
				if randomNumber >= 0.5 then
					setActivity(10)
					initiateHunt()
				else
					ghostEvent()
				end
			else
				local randomStatus = math.random(1, #status)
				if randomStatus == status[1] then
					currentStatus = randomStatus
					idling()
				elseif randomStatus == status[2] then
					currentStatus = randomStatus
					roaming()
				end
			end		
		end)
	else
		spawn(function()
			repeat wait() until currentStatus == status[1]
			if gameMain.Variables.AverageSanity.Value <= 40 then
				local randomNumber = math.random()
				if randomNumber >= 0.5 then
					setActivity(10)
					initiateHunt()
				else
					ghostEvent()
				end
			else
				local randomStatus = math.random(1, #status)
				if randomStatus == status[1] then
					currentStatus = randomStatus
					idling()
				elseif randomStatus == status[2] then
					currentStatus = randomStatus
					roaming()
				end
			end		
		end)	
	end
end

ill take a look, thank you for providing the whole script

1 Like

Had to edit it real quick because I didn’t copy part of the code, sorry!

Since i dont have access to the game, its missing some variables so can you send a file of the game?

I noticed this in your script

if gameMain.Variables.AverageSanity.Value <= 40 then
				local randomNumber = math.random()
				if randomNumber >= 0.5 then
					setActivity(10)
					initiateHunt()
				else
					ghostEvent()
				end
			else
				local randomStatus = math.random(1, #status)
				if randomStatus == status[1] then
					currentStatus = randomStatus
					idling()
				elseif randomStatus == status[2] then
					currentStatus = randomStatus
					roaming()
				end

The math.random isnt getting anything due to being no minimum or maximum amount

plus, the function cant get any decimals

math.random() returns a value between 0 and 1 with no args
image

I may have found your issue

while wait() do -- added the while loop here, works fine
	roaming()
end
-- below this may be your issue
while wait(gameMain.Variables.AverageSanityLoss.Value / 3) do
	if gameMain.Variables.AverageSanityLoss.Value >= 95 then

		spawn(function()
			wait(15)
			repeat wait() until currentStatus == status[1]
			if gameMain.Variables.AverageSanity.Value <= 40 then
				local randomNumber = math.random()
				if randomNumber >= 0.5 then
					setActivity(10)
					initiateHunt()
				else
					ghostEvent()
				end
			else
				local randomStatus = math.random(1, #status)
				if randomStatus == status[1] then
					currentStatus = randomStatus
					idling()
				elseif randomStatus == status[2] then
					currentStatus = randomStatus
					roaming()
				end
			end		
		end)
	else
		spawn(function()
			repeat wait() until currentStatus == status[1]
			if gameMain.Variables.AverageSanity.Value <= 40 then
				local randomNumber = math.random()
				if randomNumber >= 0.5 then
					setActivity(10)
					initiateHunt()
				else
					ghostEvent()
				end
			else
				local randomStatus = math.random(1, #status)
				if randomStatus == status[1] then
					currentStatus = randomStatus
					idling()
				elseif randomStatus == status[2] then
					currentStatus = randomStatus
					roaming()
				end
			end		
		end)	
	end
end

yep, after testing again, it this is the only thing that doesnt work

I never finished coding that part of the script so its not going to work I just want to know how I can make it work with two while loops.

yeah, the script works, its just that chunk that doesnt work, hope i helped
Edit:
You usually wait until the loop is done or you break the loop by adding break into it, or you call the functions inside the loop

Update: The issue was because the HumanoidRootPart was anchored :man_facepalming:

Lucky its just the anchor for u ToT mine just circles around

This was 8 months ago, no need for necroposting.