Strange interactions with using Connect on RemoteEvents

I’ve been trying to use a RemoteEvent to tell the server when dialogue has been completed. The thing is: weird errors are showing up and I’m not sure why.

In my code, I use a ModuleScript to return a function that’s used by a separate script. Everything works fine except for one part.

local init = -- Path to a RemoteEvent that starts dialogue
local dialoguefinished = -- Path to the DialogueFinished RemoteEvent

return function(player, name, data, object)

  init:FireClient(player, name, data)

  -- Past this point is where everything goes wrong.

  if object then
    local connection
    connection = dialoguefinished.OnServerEvent:Connect(function(p)
        print("test")
        connection:Disconnect()
    end)
  end

end

Whenever I run with function with the 4th parameter, the function that dialoguefinished is connected to runs! The output I get is:

> test
> Workspace.ModuleScript:14: attempt to index nil with 'Disconnect'

Even though dialoguefinished hasn’t even been called by the client yet, the function runs. Strangely enough, when I connect dialoguefinished from another script, everything works fine.
Like this:

dialoguefinished.OnServerEvent:Connect(somefunction)

After putting that, no errors pop up. Can someone tell me why?

You’re layering an OnServerEvent event listener inside of a callback function which also fires the client.

You’re also attempting to disconnect a connected event before the connection has been created.

local init = nil -- Path to a RemoteEvent that starts dialogue
local dialoguefinished = nil -- Path to the DialogueFinished RemoteEvent
local connection = nil

return function(player, name, data, object)
	init:FireClient(player, name, data)
	if object then
		connection = dialoguefinished.OnServerEvent:Connect(function(p)
			print("test")
		end)
	end
	task.wait(5)
	connection:Disconnect()
end

Obviously this isn’t the behavior which you intend to achieve, it’s just an example.

Is that why calling dialoguefinished.OnServerEvent:Connect from an outside script works? Even so, why does the connected function run in the first place even though no script has called on the RemoteEvent?

Found a solution; I used BindableEvents that are assigned to each player in the game. This script connects to those rather than the RemoteEvent being called.

For instance, I would have a folder like this on the workspace:

> Folder named "Bindables"
   > BindableEvent "TheRealCybop"
   > BindableEvent "Player2"
   > BindableEvent "somerandomplayer"

From an outside script, when the dialogue-ending RemoteEvent is called, it fires the player’s corresponding BindableEvent like this:

local bindables = -- Path to the Folder with all the BindableEvents.
local function onDialogueFinished(player)
  bindables[player.Name]:Fire()
end

Now, the script listens for the BindableEvent instead of the RemoteEvent like this:

local bindables = -- Path to the Folder with all the BindableEvents.

return function (player, name , data, object)
  init:FireClient(player, name, data)

  if object then
    local connection
    connection = bindables[player.Name].Event:Connect(function()

      -- Does something after dialogue completes.
      connection:Disconnect()    

    end)
  end
end