Help with Synchronization for rythm game

Hello
I’m making a rhythm game like the RoBeats game. I got a problem with creating songs. The whole system is completely done. But I’m trying to record songs now, i.e. I play the song on the keyboard beforehand and then save it in the studio. And I play it back so that I have to press in the order that I pressed it. So I’m trying to create a level that fits exactly with the music.

I’ve written this way through tick(), I’ll show you now:

		local keys = 0
		for i,v in pairs(Song.keys:GetChildren()) do
			if v:IsA("ValueBase") then
				keys += 1
			end
		end
		task.wait()
		
		
		workspace.Music:Play()
		local StartTime = tick()
		
		for i = 1,keys do
			local key = Song.keys:FindFirstChild(i):GetTags()
			local currTick = nil
			for i,v in pairs(key) do
				currTick = v
			end
			repeat task.wait()
			until tick() - StartTime == tonumber(currTick) or tick() - StartTime > tonumber(currTick)
			keyAdder.Add(Character,"Simple",Song.keys:FindFirstChild(i).Value,2) -- add a key
		end

But the problem is that they do not reach the end, but only spawns at the moment when they should already be pressed.
If something is not clear ask, as I do not know how to explain lucratively.

Basically, I need the spawn button to be triggered a little earlier

A few notes before I go further:

  1. tick is deprecated, use os.clock instead.
  2. I think it’s a bad idea to use the instance hierarchy to store your songs.
  3. You can just use >= which is the greater-than-or-equal-to operator which should make your until condition much less gnarly though I strongly advocate for removing this loop entirely.

I’d like to understand the currTick code, what are these instances you have and what have you tagged them with?

1 Like

How can I make the playback system better? If you can give me some tips that would be awesome

I have no idea how else to store songs :o

I’ve already fixed the code and it works perfectly. Thanks for the tips!
I just decreased the tweenTime from each tick and it works

Well you can just precompute the time and wait for that duration (which I think you already have done), you’ll get resumed on the dot by the scheduler. So you’d just go task.wait(delay) where delay is what’s in currTick if I understand correctly; currTick’s name seems very strange to me and contradictory to how it’s used.

To store the song you can use a table. A table is one of the collection types in Luau. If you’re not familiar with tables or how you’d want to go about using a table to store a song let me know.

1 Like