Disconnecting a connection in itself

Hello,
I want to achieve what the title says.

So I have some lines in a LocalScript.

local connection = nil

function someFunction()
	if connection then connection:Disconnect() end
	
	local part = Instance.new("Part")
	RemoteEvent1:FireServer()
		
	connection = RemoteEvent2.OnClientEvent:Connect(function()
		part:Destroy()
	end)

end

The issue here is, the someFunction() function is ran repeatedly at a fast rate, in which it will disconnect connection and replace it with a new one before the previous one finishes.

Hold on, why does the connection variable even exist? Why not leave the connection without a variable? Simple. To solve memory leak… I say.

But here I want to have the previous ones ending themselves instead of deleting them mid-run.

I’ve tried this, but the system doesn’t allow that:

function someFunction()

	local part = Instance.new("Part")
	RemoteEvent:FireServer()
		
	local connection = RemoteEvent.OnClientEvent:Connect(function()
		part:Destroy()
		connection:Disconnect() -- Unknown global bbzbbbvz#*%#&@4
	end)

end

And… how do I solve this? :thinking:

use connection:Once() it connects to the connection and once called for the first time, it disconnects

	local connection 
	connection = RemoteEvent.OnClientEvent:Connect(function()
		connection:Disconnect()
		part:Destroy()
	end)

this works fine, but its essentially the same as using Event:Once()

1 Like

Ah. I was wondering what that function does. I did it like this, but… it’s not giving out any output.

RemoteEvent.OnClientEvent:Once(function()
	part:Destroy() -- Not destroying
end)

Hold on. It does give outputs, but most of the time it doesn’t… why is that?
And just to remind:

Maybe instead of using a remote event, you could use a remote function for this.

	local Part = Instance.new("Part")
	RemoteFunction:InvokeServer()
	Part:Destroy()
2 Likes

Never learnt about RemoteFunctions… and I don’t think I can use it because there’s some complicated RemoteEvent chaining on the server-side… I’m not changing that.

Try using RemoteEvent.OnClientEvent:Wait()

Sorry, but that makes it worse.

I tried that. I need the function thread, and with that :Wait() it will delay the upcoming lines.

I believe remote functions are your best bet then unsure of other solutions to that since you have different threads

1 Like

I made some mistakes on the rewrote structure here, unsure if that changes stuffs. There are 2 different remotes:

local ProjectileFiring = remoteEvent1
local ProjectileDestroying = remoteEvent2

function someFunction()
	if connection then connection:Disconnect() end
	
	local part = Instance.new("Part")
	ProjectileFiring:FireServer()
		
	connection = ProjectileDestroying.OnClientEvent:Connect(function()
		part:Destroy()
	end)

end

I believe on the server side is ProjectileFiring.OnServerEvent, send a signal somewhere, and then that signal has it’s own .Event. And last, it’s ProjectileDestroying:FireClient() inside.

@CyberNinjaScripter I’m not sure how the client-server-client invoke will work like this, and I don’t see, or maybe just blind, about how that will solve the main issue.

I was thinking you could do something like this.

Client

local Part = Instance.new("Part")
RemoteFunction:InvokeServer()
Part:Destroy()

Server

RemoteFunction.OnServerInvoke = function(Player)
	--Do your stuff
	
	return -- Return when you want to destroy the part on the client
end

Okay guys. The :Once() function is the best solution here. Thank you for all the feedbacks :grinning:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.