How would I make a renderstepped color-changing part that included playback loudness?

Ok plan B: go learn how to use remote events and move the color changing script to the client

i actually do remember how to use remote events, let me mess around with it and ill come back. where do i put the local script for color change?

Do note that you’ll probably need two separate remotes; A remote function for requesting the currently playing song when the player joins the game, and a remote event for when the server changes the song

You can put it anywhere that supports localscripts

serverscript:

	local song = sounds[math.random(1, #sounds)]
	song:Play()
	_G.PlayingSong = song
	remote:FireClient()
end


playRandomSong()

localscript:

Remote.OnClientEvent:Connect(function(player) 
res.Heartbeat:Connect(function()
	local song = _G.PlayingSong
	if song and song.IsPlaying then 
		local alpha = song.PlaybackLoudness * .01 
		print(song.PlaybackLoudness)
		print(alpha)
		part.Color = clr_black:Lerp(setColor, alpha)
	end
end)
end)

alright so i added the remotes (tell me if they look wrong) but what should i change with the _G stuff to make it fit this?

Don’t use _G anymore
And I think you did everything wrong

It should be something like:

--server
function playRandomSong()
  local song = sounds[math.random(#sounds)]
  song:Play()
  remote:FireAllClients(song) --tell all of the clients which song is playing
end
--client
local playingSong
remote.OnClientEvent:Connect(function(newSong)
  playingSong = newSong
end)

res.RenderStepped:Connect(function()
  if playingSong and playingSong.IsPlaying then
    local alpha = math.clamp(playingSong.PlaybackLoudness * .01, 0, 1)
    part.Color = clr_black:Lerp(setColor, alpha)
  end
end)

‘song’ is a global unknown, what was it replaced with?

oops minor spelling mistake

its replaced with playingSong (like it wasn’t obvious enough in that example code)

1 Like

okay i fixed everything here are my scripts:

local part = workspace:WaitForChild('TRIAGE')
local setColor = Color3.fromRGB(75, 43, 255)
local clr_black = Color3.new() 
local La = game.ReplicatedStorage.Layla

local playingSong
La.OnClientEvent:Connect(function(newSong)
	playingSong = newSong
end)



res.RenderStepped:Connect(function()
	if playingSong and playingSong.IsPlaying then
		local alpha = math.clamp(playingSong.PlaybackLoudness * .01, 0, 1)
		print(playingSong.PlaybackLoudness)
		print(alpha)
		part.Color = clr_black:Lerp(setColor, alpha)
	end
end)
local baba = MUSIC:GetChildren()
local layla = game.ReplicatedStorage.Layla
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Layla")
local sounds = {
	
	MUSIC:WaitForChild("DiamondAndPearl"),
	MUSIC:WaitForChild("AdoreYouHarryStyles")
	
	
	
	
}


function playRandomSong()
	local song = sounds[math.random(1, #sounds)]
	song:Play()
	remote:FireAllClients(song)
end


playRandomSong()

so did it work?

( 20 charsss )

uhh it doesnt 88382838823432534534

I did say something about using two remotes
It’s so that when the player joins after the song had already started playing the player can still know what song is currently playing

In this case, you can make your remote event dual purpose:

--serverscript
local song --tada it's now outside
function playRandomSong()
	song = sounds[math.random(1, #sounds)]
	song:Play()
	remote:FireAllClients(song)
end

remote.OnServerEvent:Connect(function(player) --when a client asks what song is playing, just tell them
  return song 
end)

playRandomSong()
--localscript
local playingSong = La:FireServer() --tada instead of nil its going to ask the server for the song
La.OnClientEvent:Connect(function(newSong)
	playingSong = newSong
end)

okay i implemented the new stuff so that it has the dual purpose but it still does not function. idk if i mentioned this before but it used to be black when playing because of the clr_black thing but now it remains the color that it is in workspace

try printing out the things again

i kept the

print(playingSong.PlaybackLoudness)
   	print(alpha)

inside of the script and it hasn’t printed since ive been testing the new stuff

put print statements in all of the event connections, i.e. OnClientEvent, OnServerEvent, and FireServer response

okay i added prints to the function in music script, the on server event in the music script, and the onclientevent in colorchange, only the onserverevent printed

I’m dumb i tried using return statement on a remote event

Take two

--serverscript
remote.OnServerEvent:Connect(function(player) 
  remote:FireClient(player, song) --manually tell the player the song
end)
--localscript
local playingSong
La.OnClientEvent:Connect(function(newSong)
  playingSong = newSong
end)
if not playingSong then --if the player hasn't received the song upon joining, manually request the song
  La:FireServer()
end

yes oh my god after hours of back and fourth it FINALLY WORKS thank you so so so so so much for taking this time to work with me on this, you’re honestly great at what you do and i wish u luck in the future, thanks so much

1 Like

I know this is solved, but technically you could have 1 script that manages only 1 sound object and loops through a table of IDs randomly. I do this, and it works just fine for my game.

That way you don’t have to locate the song and depend on an entirely new sound object just to get the desired effect. One local script on the client can then handle the light changing effects while the audio plays.

Otherwise, happy this got solved.

1 Like