Need help making this audio life hack efficient

Greetings! I created a life hack that allows me to upload 2 audios at once.

How it works is I add 2 songs to 1 audio, and use the script below to split it up:

-- script.Parent is a Sound object. 

if script.Parent:GetAttribute("Version") == "Normal" then
	script.Parent:Play()
	while true do
		task.wait()
		print("Changing")
		if script.Parent.TimePosition > 43 then
			print("Detected")
			script.Parent:Stop()
			script.Parent.TimePosition = 0
			script.Parent:Play()
		end
	end
end

if script.Parent:GetAttribute("Version") == "Event" then
	script.Parent.TimePosition = 43
	script.Parent:Play()
	while true do
		task.wait()
		print("Changing")
		if script.Parent.TimePosition > 100 then
			print("Detected")
			script.Parent:Stop()
			script.Parent.TimePosition = 43
			script.Parent:Play()
		end
	end
end

However, you may notice it is not that efficient.

It works and all, but it lags my game.

I tried using .Changed on script.Parent, but that lags the game even worse…

I want to fix this issue, because I do not want v3.10 to be the laggiest version of Bloxy Kart. It will already be laggy enough because of a live event scheduled to take place when v4.0 releases.

Thanks in advance! I just do not want to waste my upload limits. This was my last one this month

Hi! I fiddled with it and added a couple notes, see if this works.

if script.Parent:GetAttribute("Version") == "Normal" then
	print("Changing") -- This was happening every frame, not on version change.
	script.Parent:Play()
	while script.Parent.IsPlaying do -- Only run while the sound actually plays.
		if script.Parent.TimePosition >= 43 then
			print("Detected")
			script.Parent.TimePosition = 0
		end
	end
end
 -- Stopping and starting again can cause serious stutter.
 -- Also, Stop() automatically sets TimePosition to 0. Pause() is separate.
if script.Parent:GetAttribute("Version") == "Event" then
	script.Parent.TimePosition = 43
	script.Parent:Play()
	while script.Parent.IsPlaying do
		if script.Parent.TimePosition >= 100 then
			print("Detected")
			script.Parent.TimePosition = 43
		end
	end
end
1 Like

It’s a shame that performing the check on the .Changed event is giving you issues. Have you tried singling out the TimePosition property exclusively, using :GetPropertyChangedSignal()? Perhaps some other property like PlaybackLoudness updates very frequently.

Beyond that, periodically checking the property for updates may be entirely unnecessary. Have you tried calculating the amount of time that the song has left, and then stalling that duration before repeating? I’d imagine that would work in theory, unless the PlaybackSpeed is, for whatever reason, entirely variable. Of course, before initiating the wait, you’d have to first guarantee that the audio is indeed playing, so that there is not any desync.

Originally, I tried :GetPropertyChangedSignal("TimePosition"), but it broke the script.

Done. How does it look:

if script.Parent:GetAttribute("Version") == "Normal" then
	print("Changing")
	while true do
		script.Parent:Play()
		task.wait(43)
		script.Parent.TimePosition = 0
	end
end

if script.Parent:GetAttribute("Version") == "Event" then
	while true do
		script.Parent.TimePosition = 43
		script.Parent:Play()
		script.Parent.Stopped:Wait()
	end
end

I had to add task.wait() into your loop.

1 Like