I have a pathfinding script that makes the newly-spawned npc go to a table and sit on a chair. After some time he’s meant to stand up and leave. However one the way back he just keeps circling in one place.
The first script:
local npc = script.Parent
local pathfindingService = game:GetService("PathfindingService")
local restaurant = game.Workspace:WaitForChild("Restaurant")
local tables = restaurant:WaitForChild("Tables")
local humanoid = script.Parent:WaitForChild("Humanoid")
local replicatedStorage = game:GetService("ReplicatedStorage")
local seatedEvent = replicatedStorage.Seated
local serverStorage = game:GetService("ServerStorage")
local event = serverStorage:WaitForChild("Number")
local destinationTable
npc.PrimaryPart:SetNetworkOwner(nil)
local destination = nil
event.Event:Connect(function(number, check)
if npc == check then
destinationTable = tables:FindFirstChild(number)
local freeChairs = {}
for i,v in pairs(destinationTable.Chairs:GetChildren()) do
if v.Occupied.Value == false then
table.insert(freeChairs, v)
end
end
if #freeChairs > 0 then
local destinationChair = freeChairs[math.random(1,#freeChairs)]
destinationChair.Occupied.Value = true
local seat = destinationChair.Seat
destination = seat.Position - (2 * CFrame.new(seat.Position, destinationTable.TAble.Union.Position).LookVector)
local pathparams = {}
local path = pathfindingService:CreatePath(pathparams)
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local humanoid = npc:WaitForChild("Humanoid")
local function followPath()
local success, errorMessage = pcall(function()
path:ComputeAsync(npc.HumanoidRootPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followPath(destination)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
seat:Sit(npc.Humanoid)
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
followPath()
end
end
end)
The second script:
local npc = script.Parent
local pathfindingService = game:GetService("PathfindingService")
local restaurant = game.Workspace:WaitForChild("Restaurant")
local humanoid = script.Parent:WaitForChild("Humanoid")
local serverStorage = game:GetService("ServerStorage")
local event = serverStorage:WaitForChild("Back")
local destinationCar
npc.PrimaryPart:SetNetworkOwner(nil)
local destination = nil
event.Event:Connect(function(number, check)
if npc == check then
for i,v in pairs(restaurant.ParkingLot.Folder:GetChildren()) do
if v.Occupied.Value == number.Name then
destinationCar = v
end
end
destination = destinationCar.Position + Vector3.new(0,5,15)
local pathparams = {}
local path = pathfindingService:CreatePath(pathparams)
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local humanoid = npc:WaitForChild("Humanoid")
local seat = humanoid.SeatPart
local function followPath()
local success, errorMessage = pcall(function()
path:ComputeAsync(seat.Position - (2 * CFrame.new(seat.Position, number.TAble.Union.Position).LookVector), destination)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followPath(destination)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
print("rozjebane")
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
humanoid.Sit = false
seat:FindFirstChild("SeatWeld"):Destroy()
npc:SetPrimaryPartCFrame(seat.CFrame - (2 * CFrame.new(seat.Position, number.TAble.Union.Position).LookVector))
followPath()
end
end)
Why does the first function keep going if there’s :Disconnect()? The function is not called from event again. The pathfinding script is from developer page.