Function keeps running after :Disconnect()

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.

The problem may lay somewhere else as it keeps happening even if I do :Destroy() on the first script.

followPath(destination) whats the purpose of this line and can you print something between them

I’m sorry I’m so dumb the second function was setting npc CFrame to the beginning every time it fired.

so that was the issue? well i thought maybe its because path is being constantly blocked or something

1 Like

i forgot to mention if you calling that function and disconnecting block means starting a whole new pathfinding process then I suggest you disconnect reachedConnection too with blockedConnection else it can cause memory leak and maybe at end of the event too so next time it is fired old ones are disconnected

1 Like