Attempt to index nil with disconnect

When I run a test server, it gives me an error: Players.Player1.PlayerScripts.MainUI:32: attempt to index nil with 'Disconnect'
However, when i click play it works fine.

Here is the code:

-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Module Scripts
local ModuleScripts = ReplicatedStorage:WaitForChild("ModuleScripts")
local GuiHandler = require(ModuleScripts:WaitForChild("LocalGuiHandler"))

-- Remote Events
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local updateTimedRewardsUIEvent = RemoteEvents:WaitForChild("UpdateTimedRewardsUI")
local showPlayerAbbreviationEvent = RemoteEvents:WaitForChild("ShowPlayerAbbreviationEvent")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local timedRewardsGui = playerGui:WaitForChild("TimedRewards")

local timedRewardsHolder = timedRewardsGui.Holder
local timedRewardsCloseButton = timedRewardsHolder.CloseButton

local connections = {}

---------------------------------- TIMED REWARDS ----------------------------------
connections["TimedRewardsCloseButtonPressed"] = timedRewardsCloseButton.Activated:Connect(function()
	timedRewardsGui.Enabled = false
	connections["TimedRewardsCloseButtonPressed"]:Disconnect()
end)

connections["updateTimedRewardsUIEvent"] = updateTimedRewardsUIEvent.OnClientEvent:Connect(function(rewardMessages)
	print("e")
	GuiHandler.updateTimedRewardsUI(player, rewardMessages)
	print(connections, connections["updateTimedRewardsUIEvent"])
	connections["updateTimedRewardsUIEvent"]:Disconnect()
end)
------------------------------- END OF TIMED REWARDS ------------------------------

---------------------------------- ABBREVIATION INFO ----------------------------------
connections["showPlayerAbbreviation"] = showPlayerAbbreviationEvent.OnClientEvent:Connect(function(tenPower)
	GuiHandler.displayPlayerAbbreviation(tenPower)
	connections["showPlayerAbbreviation"]:Disconnect()
end)
------------------------------- END OF ABBREVIATION INFO ------------------------------
3 Likes

This is not a direct solution, but instead of attempting to disconnect after a connection, use :Once() instead. Using :Once() instead of :Connect() will only play the event once, and then disconnect itself

2 Likes

for the showPlayerAbbreivation event it can be called more than once
but im going to try for now and see if it works

1 Like

It works but do you know why the code with the connections isn’t working?

1 Like

Does your function updateTimedRewardsUI edit the connections table in any way?

1 Like

No, it only changes some ui.

char limit

1 Like

Interesting, I noticed when I put a task.wait() over here:

connections["UpdateTimedRewardsUIEvent"] = updateTimedRewardsUIEvent.OnClientEvent:Connect(function(rewardMessages)
	print("e")
	GuiHandler.updateTimedRewardsUI(player, rewardMessages)
	print(connections, connections["UpdateTimedRewardsUIEvent"])
	task.wait(5)
	connections["UpdateTimedRewardsUIEvent"]:Disconnect()
end)

it doesnt give me any errors.

1 Like

I think I solved the issue. I wrote this:

connections["UpdateTimedRewardsUIEvent"] = updateTimedRewardsUIEvent.OnClientEvent:Connect(function(rewardMessages)
	print("e")
	GuiHandler.updateTimedRewardsUI(player, rewardMessages)
	print(connections, connections["UpdateTimedRewardsUIEvent"])
	repeat task.wait()
		print("waiting")
	until connections["UpdateTimedRewardsUIEvent"] ~= nil
	print(connections, connections["UpdateTimedRewardsUIEvent"])
	connections["UpdateTimedRewardsUIEvent"]:Disconnect()
end)

and it worked.

Waiting only printed once so I think the issue was that I had to wait a tick.

I won’t mark this as solution since I don’t think this is the most intuitive way to solve this. I’m still looking for a better way!

1 Like

That is so strange, I would have to guess that the client is processing events immediately after the connection is made. Since events will be stored until they can be processed or deleted a queue can form on test servers where the client takes longer to load in.

Another solution might be to set the workspace’s SignalBehavior to Deferred, which I recommend doing anyways; but this can break your game elsewhere if you are unknowingly dependent on Immediate behavior. An old announcement on this feature which will soon be default

Eitherway using :Once over this connect and disconnect pattern is a good idea, you could remove the entire connections table.

1 Like

I am going to use :Once for the first two connections but for the last one which is showplayerabbreviatoinevent it can fire many times so should I not use disconnect for it at all?

If it can fire more than once you should not disconnect it. Is there any reason an event would be sent to it that it should NOT process the abbreviation?

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