What are the benefits of using :Once

So until now I set up event listeners using this method

connection = nil
connection = UIS.InputEnded:Connect(function(input)
end)

and I would disconnect them whenever needed.
however I just came accross this thing called :once that does not have that much documentation on it and seems to be better however this raised my question of why I’ve never seen it be used in other people code that I’ve read.

are there any differences in peformance or other areas?

local TweenService = game:GetService("TweenService")

local Tween1 = TweenService:Create(blabla, TweenInfo.new(blabla), {blabla})

Tween1.Completed:Once(function(state)
	if state == Enum.PlaybackState.Completed then
		--DoStuff
	end
end)
Tween1:Play()

its just stuff that only listens once, yes u can always disconnect the function, but its easier when u only need it once, for example

Part.Touched:Once(function()
--change color or open the door that once requires 1 touch to open
end)

I only use it when I know for certain that there’s going to be an event signal and don’t want to bother with disconnecting. I’d imagine it might save on memory a little but any actual gain would be negligible

(on the page it says it still returns the connection and you could still :Disconnect() it)

I think Once is quite new since I also didn’t notice it until some months ago. Its functionality is really in its name, once, you will only find it useful when you need to wait for something once. It also makes your code more readable and gets rid of the need of handling one-time events like this:

local connection
connection = someEvent.Event:Connect(function()
	--some code
	connection:Disconnect()
end)

You will find it particularly useful when waiting for things to load or finish before performing an action.

so basically its a connection that once it’s fired once disconnects?

1 Like

Yes, exactly that

:Once is just :Connect but it only Connects until it has been fired, then it will disconnect (it will run the code inside of it before that)

So basically:
:Once created
:Once has been fired
Code inside :Once runs
:Once does not work anymore (automatically disconnects)

:Connect created
:Connect has been fired
Code inside :Connect runs
:Connect still works

This is what you would do without :Once

task.spawn(function()
    event:Wait() -- wait until event fires, then run code
    -- code
end)

or

local connection

connection:Connect(function()
   -- code
   connection:Disconnect()
end)

This is what you would do with :Once

event:Once(function()
    -- code
end)

See how much more simpler it is?

thank you for the explanation I understand them now

Yeah, as the other people have stated above,
:Once is the method like Connect but disconnects automatically after one fire. Say you might need one remote fire, you can use it like this:

Remote.OnClientEvent:Once(function()
       Do stuff here
end)

This is useful and automatically handles the disconnection instead of making some more variables like you did above. This is avoiding Bloat, or unnecessary code. You may wanna replace some things, and I use this pretty often!