Wait() is wrong

Almost everything has events for useful things, you just need to find them in the Roblox API hub. Also, if you really want a wait and you want something accurate, you also need to use the Wait() function of a RBXScriptSignal, the RunService.HeartBeat event and tick() function (for precise time).

As grif_0 said, generally, you shouldn’t use wait() at all. Its a terrible function that is imprecise and doesn’t always work


Edit: Also, events are completely customizable in every way, though you need to read some API in addition to having a good grasp on a core programming concepts and logic. I'd recommend looking into alternatives to Wait(). As other posters have said, you shouldn't use it, and it's working exactly how it was meant to (poorly).

What most people do is they connect RBXScriptSignals and put the returned RBXScriptConnection into a table then use the RBXScriptConnection:Disconnect() function to stop the connection when they no longer need it. The RBXScriptSignals:Wait() function is also used for things similar to what you’re doing.


Example

Instead of:

game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = 0}):Play()
wait(.5)

Do:

local soundOnTween = game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = vol})
soundOnTween:Play()
soundOnTween.Completed:Wait()

The Completed event fires right as the tween completes. Wait(0.5) doesn’t always. Also, I would highly recommend naming your variables something more descriptive, as it helps when making bigger projects. You will thank yourself later.

3 Likes

People are being complicated
The duration in seconds left on an audio according to the wiki seems to be
(TimeLength-TimePosition)*PlaybackSpeed
Not the same as just spewing Sound.TimeLength

Try this for where you wait the sound length:
(script.Parent.ambience.TimeLength-script.Parent.ambience.TimePosition) * script.Parent.ambience.PlaybackSpeed

Hello,

Sound.TimeLength is what I want though

Hello,
Please consider the following, try my given example at least once.

You want the length left on the Sound so you can let it fade out at the end. Correct?
That’s not the exact same as using only TimeLength, you have to do a little bit more.

Sounds will play faster or slower than the TimeLength. Because they run by the PlaybackSpeed. Even if your TimePosition always starts at 0 for the sounds, you have to still multiply TimeLength by PlaybackSpeed to get the real time.

1 Like

@PseudoPerson is correct in all of this. Event-driven programming is perfect for this.

4 Likes

Hello,
I want to wait sound.TimeLength MINUS .5 seconds

Try this:
change

wait(script.Parent.ambience.TimeLength)

to

wait(script.Parent.ambience.TimeLength - 0.5)

then remove

wait(.5)

after the second tween

wait(((script.Parent.ambience.TimeLength-script.Parent.ambience.TimePosition) * script.Parent.ambience.PlaybackSpeed)-0.5)

Please make an effort to do this yourself

1 Like