Reconnecting a remote event listener after the remote event is deleted + recreated

This remote event is working fine until my game loop resets.

Listener:

local function gone(player, n)
-- stuff
end
game.ReplicatedStorage:WaitForChild("takeperson").OnServerEvent:Connect(gone)

The problem comes after the game loop deletes the remote event “takeperson” from replicated storage and then at the end of the loop uses instance.new to create a new identical “takeperson” remote event. After it recreates the remote event the listener no longer does the code in the gone function. I think this is because the function is only connected to the original takeperson remote event and after it gets deleted the first time it permanently becomes disconnected.

Is there any way to reconnect the remoter event after “takeperson” is recreated with instance.new in ReplicatedStorage?

1 Like

Just have a function that connects the remote and call it every time the game loop resets:

local function ConnectTakePersonEvent()
	local function gone(player, n)
	-- stuff
	end
	game.ReplicatedStorage:WaitForChild("takeperson").OnServerEvent:Connect(gone)
end

Then each time your game loop resets call the function:

ConnectTakePersonEvent()

Also, what is the reason for deleting the remote event?

I think there’s no need to delete it, why are you doing this?

and @cjjdawg,
I actually think it would be way more logical to just use an if statement to determine if the listener should do the stuff in the function anyways, but for some reason in the past, if statements just straight up didn’t work or I was not implementing them right. This was just a workaround that I thought could possibly work.

You know that when an object is destroyed all connections to the object are removed, in this case, the OnServerEvent connection will be disconnected so the listener will no longer get called.

Ok, I’ll have to think of something else other than deleting it.

What if I use “:Remove()” instead of “:Destroy()”?