Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Remove Event Listener?

Asked by 9 years ago

Is there any way to make an even "stop listening" when a certain condition is met?

Maybe some code would help:

game.Players.PlayerAdded:connect(function(player)
    if player.Name == "Roblox" then
        --do something
        -- Now we don't need to listen for this event anymore.
        -- How do we remove the event listener?
    end
end)

1 answer

Log in to vote
0
Answered by 9 years ago

You used double **<pre class="brush: lua"> </pre> **, try to be carefull next time.

Okay so, the point here is that you can desactivate it in two ways.

Ill show first, wich is recommended: Shutdown event listener.

local PlayerJoinedEvent = game:service('Players').PlayerAdded -- to make an event controllable
PlayerJoinedEvent:connect(function(player) -- the event
if player.Name=="Roblox" then 
--do something
end
end)

You would want to use: disconnect()

PlayerJoinedEvent:disconnect() -- turn event off

or the classic laggy way: Variable Statements

local IsOn = true -- boolean

game.Players.PlayerAdded:connect(function(player)
if isOn==true then -- if its on
    if player.Name == "Roblox" then
        --do something
    end end
end)

IsOn = false -- shutting down

Hope this helps! If not, tell full output. ~marcoantoniosantos3

0
Using a variable as a debounce like that isn't laggy unless hundreds of functions each *second* are waiting like that. adark 5487 — 9y
0
Its 'laggy' in the meaning it will repeat and activate the event but doesn't run inside code of the if isOn==true then, if you understand me. ;) marcoantoniosantos3 200 — 9y
Ad

Answer this question