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

What is :Wait() and :Disconnect() for an event?

Asked by 6 years ago

I've seen this before, but don't know what they are used for.

0
`:Wait()` listens (or waits) for an even to fire, and `:Disconnect()`, well, disconnects an event. TheeDeathCaster 2368 — 6y

2 answers

Log in to vote
2
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
6 years ago

: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

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Although I've posted comments, I feel it might be better to give an example for how they can be used. :P

:Wait

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.

:Disconnect

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

  1. CharacterAdded - Fires when a player's character loads.

  2. 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. :)

Answer this question