:Disconnect help

So I am currently writing a pet script, and I am needing to be able to stop and start a function whenever I am needing to…

This means I am going to be using the :Disconnect() RBXSignal which I very rarely use… The current wiki page is kinda vague when it comes to using it so I would appreciate some insight on the use…

The current script looks like.

function Start(plr, chr)

end

local connection
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		connection:Disconnect()
		connection = Start(plr, chr)
	end)
	plr:WaitForChild("Birds").ChildAdded:Connect(function()
		connection:Disconnect()
		connection = Start(plr, plr.Character)
	end)
end)

I have yet to test this since I aint really able to currently but I will not have time to debug this later in-time. So will this code work, and if it will not, how exactly would I go about fixing it?

1 Like

Your post is pretty vague about what you are trying to accomplish here. You need to describe in more detail about what you are trying to do and in what places you are doing it. The following information would be useful:

  • What are you trying to do here?
  • Under what circumstances do you want this to happen?
  • What purpose are you trying to disconnect the function for?

There are a lot of things wrong with this code, one being the fact that disconnecting functions is only for events. I can help you solve these if you just provide more information. It would also be really useful if you were to test this on your own and explain what went wrong and what needs to be fixed.

2 Likes

Alright, basically I am having a loop going on in the Start function. I am needing to be able to Stop that function to update the script and start a new loop.

Each one will be a bird moving inside to a random position and I don’t want 2 loops running at once each one starting a new loop, How exactly would I go about that?

I currently am not able to test this code since It relies on something else that needs to be done by another person.

Current code inside of the start funcion

function Start(plr, chr)
	local PlayerBirds = plr:WaitForChild("Birds")
	for i,v in pairs(PlayerBirds:GetChildren()) do
		local bird = game.ServerStorage.Birds[v.Name]:Clone()
		bird.Parent = workspace
		bird:SetPrimaryPartCFrame(chr.Head.CFrame + Vector3.new(0,1,3))
		bird.Name = chr.Name.."'s Bird"
		local loadedanim = bird.Controller:LoadAnimation(bird.Fly)
		loadedanim:Play()
		local BodyPos = Instance.new("BodyPosition", bird.PrimaryPart)
		BodyPos.P = 500
		BodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BodyPos.D = 350
		BodyPos.Name = "BodyPos"
		local BodyGryo = Instance.new("BodyGyro", bird.PrimaryPart)
		BodyGryo.P = 1000
		BodyGryo.D = 100
		BodyGryo.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		print("Done")
		spawn(function()
			while wait(.1) do
				if plr.Harvesting.Value == false then
					BodyPos.Position = chr.Head.Position+Vector3.new(4*(i),math.random(3.99,4),4)
					BodyGryo.CFrame = CFrame.new(bird.PrimaryPart.Position, chr.Head.CFrame.LookVector*100)*CFrame.Angles(-math.pi, -math.pi/2,-math.pi)
				end
			end
		end)
	end
end

So from my understanding all you are trying to do is break the all of the loops in the Start function whenever you call the Start function again. This would be a rather simple thing to do without disconnecting the function, which is completely the wrong thing to do.

All you have to do is keep a variable indicating to the script the number of times the start function has been fired. When creating a loop, the loop continues as long as that number remains the same. Here would be a code example:

local LoopNumber = 0
function Start(plr, chr) -- I've cut out some of your irrelevant code just for this example
	LoopNumber = LoopNumber + 1 -- changes the loopnumber so all previous loops stop
	local ThisLoopNumber = LoopNumber
	for i=1,10 do
		spawn(function()
			while LoopNumber == ThisLoopNumber do -- when the next start is called, this will no longer be true
				if plr.Harvesting.Value == false then
					BodyPos.Position = chr.Head.Position+Vector3.new(4*(i),math.random(3.99,4),4)
					BodyGryo.CFrame = CFrame.new(bird.PrimaryPart.Position, chr.Head.CFrame.LookVector*100)*CFrame.Angles(-math.pi, -math.pi/2,-math.pi)
				end
				game:GetService("RunService").RenderStepped:Wait() -- more efficient alternative for wait()
			end
		end)
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		Start(plr, chr)
	end)
	plr:WaitForChild("Birds").ChildAdded:Connect(function()
		Start(plr, plr.Character)
	end)
end)

I hope this solved your problem!
If this is not what you are trying to accomplish, please let me know.

3 Likes

I think to start with you’re a bit confused on what Disconnect does. It’s basically what is says on the tin, disconnects a signal you created with Connect

Example:

local connection
connection = foo:Connect(function(bar)
    connection:Disconnect()
end

In relation to your original posted code, you can’t make ‘connection’ a function unless that function returns a signal

1 Like

connection isn’t in the scope of the function, you would need to

local connection
connection = foo:Connect(function(bar)
    connection:Disconnect()
end

my bad, yes you would need to do that ^