Check PlaybackLoudness on change

Hi everyone! i’m making a music based game and i want to change the lights on the stage everytime the beats goes, actually i’m trying to do this with Alone by Marshmello and i was using PlayingSong.PlaybackLoudness.Changed but doesn’t work.

Creating a new instance(intvalue) and changing it every x seconds makes just the lights change every x seconds following the wait(x)

I just need to check that everytime the loudness is >= to 150 the lights changes

Does anyone know how to check that on every change? so it will be on sync, i guess and hope.

local sound = [SOUND LOCATION]

sound.Changed:Connect(function()
    if sound.PlaybackLoudness >= 150 then
        --code
    end
end)

remove the IntValue. Just put this LocalScript somewhere and make sure you define where the sound is, script.Parent.sound or something like that.

My game:

I understand that there is no audio :confused:

Tried this rn but doesn’t work, i put a print(“Changed!”) just before the if statement and “Changed!” is printed 4 times

Maybe is because i’m using another audio that loads the ID of the song from a modulescript?

I mean, audio 1 = alone - marshmellow
audio 2 = nothing

audio2.ID = audio1.ID
then play audio2

yeah. put that code into the script that is selecting the song by random. then use the selected song instead of the variable sound

The first code will chose a song from the module that is where are stored all.
I’m using math.random to randomly chose, putting them in a local script will fire different songs for every client

You would want to use RenderStepped or Heartbeat to do this.

I’ve worked with sound a bit as I was doing the lighting and sound for someone’s club game. Most people end up using RenderStepped when using sound due to when it checks.

You can do a lot more with the sounds than you would expect, if you know how to collect the data and calculate some stuff that is. (For if you want some really good lighting effects, I’ve never seen anyone else actually use this in a game yet)

Changed does not fire for PlaybackLoudness. If it did, it would fire tens of thousands of times per second.

If you want to have something happen on each beat of the music, you will have to figure out how many beats per minute the music has, and take care to synchronize the playing music and the timer, which is a lot more work than a loop with a wait.
PlaybackLoudness doesn’t seem useful for detecting beats. But if you use the code at the end of this page:

and there’s a easily detectable correlation between the beats in the song and the PlaybackLoudness, then you could use it.

1 Like

I’m still unable to figure out for good how to do it, sometimes the songs just goes faster for the drop so changing the lights according to BPM will make the lights change a the same speed even when the volume is really low or is going really fast.

Using PlybackLudness >= 150 with any loop makes the light flick and spam really fast every time the Loudness is over 150, making it useless.

I’m still searching for some type of ideas that could help.

I’ve also tried making a KeyPressed check, so i will make the time and press when change it.
Better explaination

A while wait(.1) do
t = t+.1

So every 0.1 seconds t will increase of 0.1, 1 = 1 second.

Everytime i press Space the script will print “Time passed (t) \n Change Lights”
So if i copy the entire output the light will change after the time i choosed when pressed space.

IDK why but t will not increase in time and the wait(t) will result lower than the real time passed, making the lights change too fast and end before the end of the song.

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).

I dont think this is a property. You should use :GetPropertyChangedSignal():

Sound:GetPropertyChangedSignal("PlaybackLoudness"):Connect(function()
    if Sound.PlaybackLoudness >= 150 then 
    -- doSomething()
    end
end)

Seems like i fixed It, i made a neon change his color Basing on Loudness from black to White and the lights will change basing on the RGB doing a check.
If you want to see and give a Feedback here is the link for the topic: