I've seen this before, but don't know what they are used for.
:Wait() yields a script until the connected event is fired. It will also return what was sent.
local player = game.Players.LocalPlayer local char = player.CharacterAdded:Wait()
The script will wait until CharacterAdded is fired, and will also return the player's character.
:Disconnect() will stop execution of the function connected to the event.
local con = script.Parent.Touched:Connect(function(hit) print(hit:GetFullName()) end) wait(10) con:Disconnect()
After 10 seconds, the connected function will be disconnected to stop execution of it
Although I've posted comments, I feel it might be better to give an example for how they can be used. :P
What :Wait
does is that it listens (or waits) for an event to fire. For example, you've probably seen this before:
local Character = Player.Character or Player.CharacterAdded:Wait() -- Assuming that `Player` is already defined.
As shown in the above, the Character
variable's set to be defined to the, well, player's character. XP If the player's character's loaded, it'll define itself to it right away; however, if not, it'll listen for when the player's character loads.
What :Disconnect
does is that it, well, disconnects an event. XP An example of this would be for when a player touches a brick:
-- Assuming that `Part` is already defined. local TouchConnection TouchConnection = Part.Touched:Connect(function() TouchConnection:Disconnect() -- Disconnects the event. print('I was stepped on. :c') end)
As what will show, the Touched
event will only fire once, as its event will be disconnected after it was touched.
Stuff touched on, but never really explained
CharacterAdded
- Fires when a player's character loads.
Touched
- An event of a part that which will fire when a part touches another part, and will return what part touched it.
If you have any questions, please let me know! ^^ I hope this helped. :)