Pathfinding service slow/ something slow

Hello, Ive started to use pathfinding service to make npcs and for some reason they start just pacing and stopping and I’m not really sure why.

This is what they do.

Im not really sure what isn’t working because while testing they were just fine. If anyone who knows more about pathfinding service help me on this id appreciate it.

getPosition() is a function which returns an appropriate position to pathfind to based on the time of day, and then bedSit() is simply just for the NPCs to sleep in beds on touch. There isn’t much else to this script. They find a point to pathfind to and then go and wait and repeat. THeres no errors, but the issue comes from when they keep stopping and pausing before they move again.

local moveToMod = require(game:GetService("ReplicatedStorage").Modules.MoveToPoints)
local houseNum = "House3"
local reigon = "Creekside"
local humanoid = script.Parent:FindFirstChild("Humanoid")
local character = script.Parent
local torso = script.Parent:FindFirstChild("Torso")
local bed = false
local sleeping = false
local currentAt
local player = script.Parent
local collectionService = game:GetService("CollectionService")

local bedWeld = character.HumanoidRootPart.BedWeld

function getTime()
	return game:GetService("Lighting"):GetMinutesAfterMidnight()
end

function move()
	local function getPosition()
		local Table
		if getTime() > 480 and getTime() < 1080 then
			-- Day
			repeat 
				Table = moveToMod[reigon]["Outside"][math.random(1,#moveToMod[reigon]["Outside"])]
				wait(.01)
			until Table["Taken"] == false
			return Table		
		elseif getTime() > 420 and getTime() < 480 then
			-- Morning
			repeat
				local Num = math.random(1,#moveToMod[reigon][houseNum]) 
				Table = moveToMod[reigon][houseNum][Num]
				wait(.01)
			until Table["Taken"] == false and Num ~= 1
			if sleeping == true then
				sleeping = false
			end
			return Table
			
		elseif getTime() > 1080 and getTime() < 1140 then
			-- Evening
			repeat
				local Num = math.random(1,#moveToMod[reigon][houseNum]) 
				Table = moveToMod[reigon][houseNum][Num]
				wait(.01)
			until Table["Taken"] == false and Num ~= 1
			return Table
		else
			-- Sleep Time :)
			Table = moveToMod[reigon][houseNum][1]
			if Table["Bed"].Taken.Value == true then
				repeat
					local Num = math.random(1,#moveToMod[reigon][houseNum])
					Table = moveToMod[reigon][houseNum][Num]
					wait(.01)
				until Table["Taken"] == false and Num ~= 1
			elseif sleeping == false then
				sleeping = true
			end
			return Table
		end
	end
	
	local endPosition = getPosition()
	if endPosition ~= false then
		local shouldContrinue = false
		if currentAt then
			currentAt["Taken"] = false
		end
		currentAt = endPosition
		endPosition["Taken"] = true
		local path = game:GetService("PathfindingService"):CreatePath()
		path:ComputeAsync(torso.Position, endPosition["Pos"])
		local waypoints = path:GetWaypoints()
		
		if sleeping == false and bed == true then
			if bedWeld.Part1 ~= nil then
				bedWeld.Part1.Taken.Value = false
				bedWeld.Part1 = nil
				humanoid.Jump = true
				wait(2)
				bed = false
			end
		end
		if humanoid:GetState() == Enum.HumanoidStateType.Seated then
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		end
		for i,v in pairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				humanoid.Jump = true
			end
			if humanoid.Sit == true then
				if i/#waypoints < .15 then
					humanoid.Sit = false
				else
					wait(math.random(5,20))
					endPosition["Taken"] = false
					break
				end
			end
			if sleeping == true then
				if endPosition["Bed"] ~= nil and endPosition["Bed"].Taken.Value == true then
					wait(math.random(5,10))
					endPosition["Taken"] = false
					break
				end
			end
			humanoid:MoveTo(v.Position)
			humanoid.MoveToFinished:Wait(1)
		end
	end
end

function bedSit(part)
	local function bedTouched(hit)
		if hit.Parent == script.Parent then
			if bed == false and sleeping == true then
				bed = true
				local taken = game:GetService("ServerScriptService").Events.MapEvents.BedUpdate:Invoke(part,true)
				if taken == false then
					if bedWeld.Part1 ~= nil then
						bedWeld.Part1.Taken.Value = false
						bedWeld.Part1 = nil
						humanoid.Jump = true
						wait(2)
						bed = false
					end
				else
					part.Taken.Value = true
					character.HumanoidRootPart.CFrame = part.CFrame + Vector3.new(0,(part.Size.Y/2) + (character.HumanoidRootPart.Size.Z/2),0)
					character.Torso.CFrame = character.Torso.CFrame:ToWorldSpace(CFrame.Angles(math.rad(90), math.rad(0), math.rad(270)))
					bedWeld.Part1 = part
				end
			end
		end
	end
	part.Touched:Connect(bedTouched)
end

for i,v in pairs(collectionService:GetTagged("Bed")) do
	bedSit(v)
end

move()	
while wait(math.random(1,1)) do
move()	
end

Edit: Changed tags to scripting support from code review cause I put the wrong one
Edit 2: I did some research and found that I wasn’t setting the network ownership to the server which resolved the issue, incase anyone else has this same issue.

2 Likes

Thank you so much I had this problem for 2 days straight and could never find the solution you are the best!

1 Like

np, make sure you set the network ownership to nil once they stand up again because being anchored/welded to an anchored part resets ownership

3 Likes