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 ------------------------------
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
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.
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?