Yeah, if the PlaybackLoudness gets over 150, then forget using it entirely.
sometimes the songs just goes faster for the drop
Everytime i press Space the script will print “Time passed (t) \n Change Lights”
You have the right idea here. You can generate a table with the times when the lights should change.
It seems your table would be something like {0.1, 0.1, 0,1, 0,1} if BPM is 10, so to wait for the next beat, you do wait(0.1) wait(0.1) wait(0.1) wait(0.1) etc.
Do NOT do that! This can make the beats a solid half minute off by the end of the song. wait() is inaccurate and you need to compensate for that.
The simplest way is to use the return value of wait(), which is the amount of time that it really waited for. Next time you wait, adjust the time according to the error.
local error = 0
function waitAdjusted(time)
time = time or 1/30 -- default value
local howmuch = time + error -- how long to actually wait
local waited = 0 -- how long this actually waited
if howmuch <= 0 then -- skip beats if we're somehow running late (waiting for a negative amount of time)
-- waited = 0, did not wait at all
error = error + time -- - 0
else
waited = wait(howmuch)
error = error + howmuch - waited
end
return waited
end
-- demonstration. should print 0 (x50) and then small numbers with some 0 thrown in
error = -5
for i = 1,100 do print(waitAdjusted(0.1)) end
If you use waitAdjusted, then when wait() waits for a moment longer than it should, the next wait will wait a moment less, to balance it out.
It’s possible to do slightly better than that by using absolute times for specifying when a beat is. i.e., {0.1, 0.2, 0,3, 0,4} if BPM is 10. The last beat should be near the length of the song. Basically, you specify when in the song to flash, not the time between each flash. Then you wait until the next beat. This will require knowing the current time (with os.clock() or the like) and the exact time the song started (possibly accounting for loading time if the song hadn’t loaded yet).
Also, you can make these tables (both wait per beat and exact beat time) with a script if you know when and what the BPM changes to. So you don’t have to push the beat buttons yourself.
To test out waitAdjusted in the middle of the song, you can set the time of the song to, say, 100 seconds, and the error to -100, so it will skip waiting for the first 100 seconds (but hang the game for a few seconds as it flashes the lights).