Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Remove Event Listener?

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

✓ Accepted answer

You used double **


**, 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

2 comments

0

Using a variable as a debounce like that isn't laggy unless hundreds of functions each second are waiting like that.

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. ;)

This discussion is preserved as it was. New answers, comments, and votes are closed.