How To Disconnect Run Service

Basically I’m using run service to make the Player’s pets to follow the player. I was wondering if it was possible to disconnect the run services for each of the pets. The reason why I want to disconnect the Run Service is because whenever the Player, unequips a pet, it deletes the pets, then recreates the clones of the players new equipped pets. As a result, I think the run service is still running the move function but for the old pet clones that were deleted.

local run

local function move()
	for i, v in pairs(FighterPos) do 
		run = game:GetService("RunService").Heartbeat:Connect(function()
			wait()			
			local success, errormsg = pcall(function()
				if not v:FindFirstChild("Attacking") then
					if character.FighterStuff:WaitForChild("PositionPart"..string.sub(v.Name, #v.Name, #v.Name)) then
						followPath(v, character.FighterStuff:WaitForChild("PositionPart"..string.sub(v.Name, #v.Name, #v.Name)).Position)								
					end
				end

			end)	
		end)
	end
end

Player:WaitForChild("EquippedPets").ChildRemoved:Connect(function()
    if run ~= nil then
		run:Disconnect()
	end		
	move()
end)

try this

local run
local function move()
	for i, v in pairs(FighterPos) do 
		run = game:GetService("RunService").Heartbeat:Connect(function()
			wait()			
			local success, errormsg = pcall(function()
				if not v:FindFirstChild("Attacking") then
					if character.FighterStuff:WaitForChild("PositionPart"..string.sub(v.Name, #v.Name, #v.Name)) then
						followPath(v, character.FighterStuff:WaitForChild("PositionPart"..string.sub(v.Name, #v.Name, #v.Name)).Position)								
					end
				end

			end)	
		end)
	end
end

Player:WaitForChild("EquippedPets").ChildRemoved:Connect(function()
	run:Disconnect()
	move()
end)

Where is run initially defined? You probably need to check if it’s nil before disconnecting it.

1 Like

Didn’t seem to work. It’s still running the move function for the deleted pet models.

It will still run but after the whole Script inside RunService is finished it will not run again, connection disconnects the function so u cant run it again i will send u a script now

Attempt this:

local runs = {}

local function move()
 for i, v in pairs(FighterPos) do
       coroutine.wrap(function()
		runs[(#runs + 1)] = game:GetService("RunService").Heartbeat:Connect(function()
			wait()			
			local success, errormsg = pcall(function()
				if not v:FindFirstChild("Attacking") then
					if character.FighterStuff:WaitForChild("PositionPart"..string.sub(v.Name, #v.Name, #v.Name)) then
						followPath(v, character.FighterStuff:WaitForChild("PositionPart"..string.sub(v.Name, #v.Name, #v.Name)).Position)								
					end
				end
              end

			end)	
          end)()
		end)
	end
end

Player:WaitForChild("EquippedPets").ChildRemoved:Connect(function()
    for i,v in pairs(runs) do
          v:Disconnect()
          table.remove(runs, i)
    end	
	move()
end)

Handling Events.
It seems like this is what the majority of replies are trying to help you with. Another way, if you can’t get disconnecting to work, you could just make the code inside your RunService only run when a variable is equal to true, and when you want the RunService loop to stop, just make the variable equal to false.

This is the way to go (a table of ‘RBXScriptConnection’ objects) but the use of the ‘coroutine’ library is unnecessary (event connections are already ran in their own threads). I also disagree with the use of pcall in this particular case.