Wait() is wrong even though I defined the time

Hello,
In my script, it is a biome music changer that changes the music based on biome.,
image

local r = {}
local new = false
local loopId = 0


script.Event.Event:Connect(function(s,vol,ye)
	new = false
	if #r~=#s then
		new = true
	else
		for i=1,#r do
			if new==false then
				if r[i]==s[i] then
					new = false
				else
					new = true
					loopId = loopId + 1
				end
			end
		end
	end
	if new==true then
		r = s
		loopId = loopId + 1
		local id = loopId



		if id ~= loopId then return end

		while true do
			for i = 1,#r do
				if id ~= loopId then return end
				script.Parent.ambience.SoundId = r[i]
				if script.Parent.ambience.IsLoaded==false then
					script.Parent.ambience.Loaded:Wait()
				end
				if ye==true then
					script.Parent.ambience.Volume = 0
					script.Parent.ambience:Play()
					game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = vol}):Play()
					print(script.Parent.ambience.TimeLength.." leng")
					repeat wait() until script.Parent.ambience.TimePosition>=((script.Parent.ambience.TimeLength-script.Parent.ambience.TimePosition) * script.Parent.ambience.PlaybackSpeed)-0.5
					print(tostring(script.Parent.ambience.TimeLength).." and "..tostring(script.Parent.ambience.TimePosition))
					game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = 0}):Play()
					script.Parent.ambience.Ended:Wait()
				else
					script.Parent.ambience.Volume = vol
					script.Parent.ambience:Play()
					print(script.Parent.ambience.TimeLength.." leng")
					wait(((script.Parent.ambience.TimeLength-script.Parent.ambience.TimePosition) * script.Parent.ambience.PlaybackSpeed)-0.5)
					print(script.Parent.ambience.TimeLength.." leng")
					script.Parent.ambience.Ended:Wait()
				end
				print(script.Parent.ambience.TimeLength.." leng")
				print(script.Parent.ambience.TimePosition.." pos")
				if id ~= loopId then return end
			end
		end
	end
end)

Expected:
Music plays and stops according to time length and playbackspeed, does not interrupt even if you swich between biomes back and forth
Reality:
If you switch between biomes back and forth, it will turn the volume to 0 during sound is playing.

It doesn’t stop the sound, it just turns the volume to 0.


- Br, iSyriux

1 Like

First off, don’t put clickbait in your title. People aren’t going to help if they don’t know how, the title has nothing to do with your problem.

6 Likes

Please do this, lol. I was expecting to help someone, but since the title was wrong, I can’t help you cause I have no idea. I think the so called “pros” would [like to] help you more if you didn’t clickbait :thinking:

2 Likes

pretty sure wait() doesn’t wait for EXACTLY the amount of time ( sometimes it does ) but it normally waits for a little longer or doesn’t or something

also, if you just searched up ‘music zone tutorial’ on youtube (as I once did) you could find a pretty great music zone script.

Hello,
I’m asking how to fix my music script not how to make one

2 Likes

i know. did what i said get through to you?

Hello,
That’s not even the point of my problem
I don’t care if it’s a few milliseconds off what I do care about is that it’s 12 seconds off

1 Like
script.Parent.ambience.TimePosition>=((script.Parent.ambience.TimeLength-script.Parent.ambience.TimePosition) * script.Parent.ambience.PlaybackSpeed)-0.5

This part will always stop halfway.
TimePos >= (TimeLength - TimePos)-.5 is TimePos>= TimeLength /2 - .25

Hello,
Why

repeat game:GetService("RunService").Heartbeat:Wait() until script.Parent.ambience.TimePosition>=script.Parent.ambience.TimeLength * script.Parent.ambience.PlaybackSpeed - 0.5
print(tostring(script.Parent.ambience.TimeLength).." and "..tostring(script.Parent.ambience.TimePosition))
game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = 0}):Play() 
if script.Parent.ambience.TimePosition < script.Parent.ambience.TimeLength then
	script.Parent.ambience.Ended:Wait()
end

Replace the code from line 43 with this. It might fix it

Hello,
Very well I will test it later

thats what i said. it doesn’t wait for exactly that amount of time. its usually a few seconds (sometimes 10+) off.