How to use :wait()?

I know how the wait() function works, but there is another type that I am not familiar with. I am guessing that I need to put a true or false statement, and when the statement is true, the code will continue?
Example:

-- this is probably not how it works
(myVariable == true):Wait()

That won’t work, :Wait() only works with events (such as Sound.Ended or Tween.Completed, with those you can link :Wait() so the script will yield until those events are fired). What you could do to replicate what you’re trying to do correctly is using a repeat loop:

repeat
   task.wait(0.01) -- use the new task library instead of wait()
until myVariable == true
2 Likes

Sound.Ended Stopped the error from coming back, but the :play() seems to be not working. Do I have to set the sound id into the sound instance, or just play(soundId) is fine?

What are you trying to do? You just asked how to use :Wait() with events in your OP. And yes, :Play() will not work if your sound doesn’t have a SoundId obviously.

I am asking if the :play() function has a paremeter of the soundID, like will it set the soundId for you when you use the function

Oh nope, you have to manually set it with Sound.SoundId.

it says “unable to download sound data”. Is there a way to fix that?

:Wait() will pause a script until a certain event happens. Commonly used to wait for a player’s character to be added:

local character = player.Character or player.CharacterAdded:Wait()

Another example:

script.Parent.Touched:Wait()
print("This part was touched.")
3 Likes

Not really, basically that usually means that the audio was deleted or copyrighted so it can’t be used anymore.

Edit: Nevermind, it doesn’t really mean that the audio was deleted but rather it’s broken. When it gets deleted it usually says [Content Deleted] or something similar.

Wait() is actually a function of RBXScriptSignal. That means, when you have an event, such as Part.Touched, you can wait until the event is fired with <event>:Wait().

2 Likes