How can I stop listening for a remote event to be fired?

Lets say I have a script that listens for a remote event to get fired.

local remote = game:GetService("ReplicatedStorage").Remote

remote.OnServerEvent:Connect(function(plr, ...)
    -- Do something
end)

How can I stop listening / waiting for the remote event to be fired? I assume I would do something like this?

local remote = game:GetService("ReplicatedStorage").Remote

local connection = remote.OnServerEvent:Connect(function(plr, ...)
    -- Do something
end)

connection:Disconnect()

Yeah you can call :Disconnect() on it:

2 Likes

Meta_data’s post should be the best option here, i just want to give my own solution for this, which is not very recommended, i guess:

What i would do is use a boolean thing, here is an example:

local remote = game:GetService("ReplicatedStorage").Remote
local Boolean = false

remote.OnServerEvent:Connect(function(plr, ...)
    if not Boolean then
        print("Event Recieved, you can write your code here")
    end
end)

wait(10)
Boolean = true

I think you know what everything inside the code means. Thanks for reading.

1 Like

Yep that is one way, you can also use a flag to determine if code can should continue

Pseudocode

flag = true
event:Connect(function(...)
    if flag then
        do the code 
end)

If you want to disconnect within the listener, you need to predeclare the variable

local connection
connection = event:Connect(function(...)
    if condition to disconnect is met
        connection:Disconnect()
end)
1 Like

Also, if you just want to fire it once, you can use Wait()

local remote = game:GetService("ReplicatedStorage").Remote

local plr, ... = remote.OnServerEvent:Wait()
-- Do something
1 Like