Wait() is wrong

Hello,

Well, this is the code the dude sent me in my other thead and he says it’s working perfectly…
I tried t recored a video about this but it seems like it doesn’t work randomly
And also it’s 1 and 40 seconds so I can’t uploda it hear but its is here is the outboard*outputst
image

Hello,

I am very humbles yes I am its obvious tha the only thin you hep me with is variabenane is is which is not a thing I need to wory about

ill look into it and see if i can maybe write you a better code then cause this guy (no offence to him) didnt make this with any consideration to readability. its very hard to follow and clearly not a very good code anyway

Well, I dont have problems with the variables names and stuff… But I have a problem understanding what u want do achieve.

At this point what I would do is dump entirely the code and try a different approach, having in mind exactly what u are trying to achieve… maybe that helps :v

Hello,

Here is place file
FILEFORDEVFORUMLOLOLOL1337H4XZ0RZ.rbxl (239.0 KB)
Repro:
Just click play, walk to biome with house, wait a bit, if it works proplery retry it again and again until it doesn’t work

wait() cannot always be reliable when waiting.

Hello,

Have you…Read anything in the post beside the title?

You really didnt describe much about whats going on at all inside your post. I think your dealing with playing audio? If you have the PlaybackSpeed different it may give diffent results with TimeLength and Position.

Hello,

I have given the roblox place file…Please go and check it out and follow my repro instructions

You shouldn’t use wait(). Instead, use event driven programming. To wait until a Sound is over use the Ended event and the Wait() function of a RBXScriptSignal (the object used for events).

For example, instead of
wait(script.Parent.ambience.TimeLength)
do
script.Parent.ambience.Ended:Wait()

4 Likes

I kinda think the same as @grif_0 and many others.

Even if I test ur file, I still dont understand what u want to achieve…
I understand better with text explanation than reading ur code.

“I want to create a playlist per biome in my game, each audio file will have a “hardcoded” delay, which I will store in a table, and depending on the biome they touch they will listen a different song, which gradually goes down in volume to blend with the next one.”

Something like that… thats enough. And I would reply an approach.

2 Likes

All your threads have the code in this manner, the variable names are really not understandable. Don’t expect us to know what each and every variable does that are named as ye, vol etc. Commenting and explaining what the variables do in the code would help, although not needed for this thread probably as you already explained it above.

Edit: Couldn’t identify the code properly earlier.

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