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()
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.
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!