Song won't loop properly via script

I have a song that loops but has a bit at the start (and end) that doesn’t allow me to use the normal loop function, thus I need a script to do it properly. I find the points where I can get a perfect loop through audacity, and then try to use this script to loop it:


while true do
	repeat
		task.wait(0.00001) -- i also tried no numbers here, same result
	until
	workspace.holddown.TimePosition >= 196.428
	
	workspace.holddown.TimePosition = 14.74
end

However, doing this in Roblox seems to cause a small hiccup at the loop point. Instead of transitioning smoothly, it essentially pauses for a second before doing the loop. I tried messing with the loop points slightly to see if that helped but it didn’t. Is there a better way I could loop this, or is it just a quirk of the Roblox engine?

1 Like
while task.wait()
      if workspace.holddown.TimePosition >= 196.428 then
           workspace.holddown.TimePosition = 14.74
      end
end

this should work

(sadly i dont know any other way of going about this, but this could work)

1 Like

unfortunately it doesn’t seem to change it, but I appreciate the reply

Two things to try:

  1. Make sure you’re updating the audio on the client side for this. For anything that requires precise timing you’d want to have full control over it without ping getting in your way.

  2. If you’ve tried that and it doesn’t work. I’d recommend experimenting playing with two sounds with the same ID.

local startSound = 14.74
local endTime = 196.428

local soundObject = workspace.holddown
soundObject.TimePosition = startSound

local soundObject2 = soundObject:Clone()
soundObject2.Parent = soundObject.Parent


while true do
	-- prep copy 2, play copy 1
	soundObject2.TimePosition = startSound
	soundObject:Play()
	repeat
		task.wait()
	until
	soundObject.TimePosition >= endTime
	soundObject:Stop()
	-- stop copy 1
	-- prep copy 1 to start time position, play copy 2
	
	soundObject.TimePosition = startSound
	soundObject2:Play()
	repeat
		task.wait()
	until
	soundObject2.TimePosition >= endTime
	soundObject2:Stop()
	-- stop copy 2

	-- repeat
end

The code is a bit confusing but essentially you get rid of that 1 second delay by immediately playing the 2nd sound while the 1st is being resetted.

1 Like

Might use a function for this,making it clearer

this was a great idea, it solves my problem, the only difficulty is figuring out exactly how long the gap is - although it seems to be about 0.05 seconds. the end result looks like this:

-- holddown2 is set to 14.74 at the start manually
while true do
	repeat
		task.wait()
	until
	workspace.holddown.TimePosition >= 196.423
	
	workspace.holddown2:Play()
	task.wait(0.05)
	workspace.holddown:Stop()
	
	workspace.holddown.TimePosition = 14.74
	
	repeat
		task.wait()
	until
	workspace.holddown2.TimePosition >= 196.423
	
	workspace.holddown:Play()
	task.wait(0.05)
	workspace.holddown2:Stop()
	
	workspace.holddown2.TimePosition = 14.74
end

Thank you very much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.