Currently having trouble disconnecting the following connection, am I missing something? I’ve been able to do this in the past but now it’s giving an error.
function internal:SetupCommunicator()
local communicator = game.ReplicatedStorage.Roborne.Communicator:WaitForChild("Sender")
local connection
connection = communicator.OnClientEvent:Connect(function(events)
connection:Disconnect()
self.Events = events
function internal:RetrieveEvent(name)
if self.Events[name] then
return self.Events[name]
else
warn("Client internal could not find event " .. name)
end
end
self:RunComponents()
end)
end
Getting error attempt to index nil with 'Disconnect'
Tried putting :Disconnect all the way at the end, doing it the way that the wiki shows it but still the same result. Don’t really have any other place that disconnects this event and it’s hard to assume it’s been disconnected already.
There’s a number of things that could be going wrong. Looking at this I would expect the connection variable to still be the reference to connection because its being used in the connected function and shouldn’t be garbage collected. I just tested to make sure by doing this, which worked.
local event = Instance.new("BindableEvent")
do
local connection
connection = event.Event:Connect(function()
connection:Disconnect()
print("was fired")
end)
end
wait()
event:Fire()
I guess I would check in both the function and after the connection is created using print to see what the connection variable currently is, I assume its nil because of the error statement. Since it looks like your trying to achieve a one time listen to the on client event occuring you could use the :Wait method of the event rather than connect and disconnect, although I’m not sure if having a connection is preferable to a having a yielding thread.
I think i’ll just yield and avoid the connection. Regarding printing the reference, I have checked that the connection exists after it is created but turns nil whenever the event fires, not really sure why this is.