Pausing and resuming songs when an event is triggered

Hey, developers! I’ve been working on a system to pause the current song that’s playing when the power goes off in my game, and resume after it gets turned back on, but I’m having a lot of problems.

I have multiple scripts to handle this. A local script in StarterPlayerScripts, and a Script in the FuseBox.

The local script manages the overall music playing in the game, and it also communicates with a RemoteEvent to pause and resume the music.

Here’s the Local Script:

local musicFolder = game.ReplicatedStorage.Music
local availableMusic = musicFolder:GetChildren()
local currentTrack = game.ReplicatedStorage.CurrentTrack

local powerOn = true
local currentSong = nil

local replicatedStorage = game:GetService("ReplicatedStorage")
local powerStateChangedEvent = replicatedStorage:WaitForChild("PowerStateChanged")
powerStateChangedEvent.OnClientEvent:Connect(function(isPowerOn)
	print("Client: Received PowerStateChanged event with value", isPowerOn)
	powerOn = isPowerOn
	if powerOn and currentSong and currentSong.IsPlaying then
		currentSong:Pause()
		print("Client: Paused current song", currentSong.Name)
	elseif not powerOn and currentSong then
		currentSong:Stop()
		print("Client: Stopped current song", currentSong.Name)
		currentSong = nil
	end
end)

while true do
	for i, randomTrack in ipairs(availableMusic) do
		currentTrack.Value = "..."

		wait(2)

		if powerOn and not currentSong then
			randomTrack:Play()
			print("Client: Started playing song", randomTrack.Name)
			currentSong = randomTrack
			currentTrack.Value = randomTrack.Name
			randomTrack.Ended:Wait()
			currentSong = nil
		else
			wait(1)
		end
	end
end

And here’s the Script in the FuseBox:

local replicatedStorage = game:GetService("ReplicatedStorage")
local powerStateChangedEvent = Instance.new("RemoteEvent", replicatedStorage)
powerStateChangedEvent.Name = "PowerStateChanged"

local powerOn = true
local powerStateChangedFired = false

local function togglePower(player)
	powerOn = not powerOn
	print("Server: Power state changed to", powerOn)

	game.Lighting.Ambient = powerOn and normalAmbience or Color3.new(0, 0, 0)

	updateFlashlights()

	if powerOn then
		powerStateChangedEvent:FireAllClients(powerOn)
	else
		if not powerStateChangedFired then
			powerStateChangedEvent:FireAllClients(powerOn)
			powerStateChangedFired = true
		end
	end
	print("Server: Fired PowerStateChanged event with value", powerOn)
	local musicFolder = game.ReplicatedStorage.Music
	for _, song in ipairs(musicFolder:GetChildren()) do
		if song:IsA("Sound") then
			if powerOn then
				song:Resume()
				print("Server: Resumed song", song.Name)
			else
				song:Pause()
				print("Server: Paused song", song.Name)
			end
		end
	end

	for _, part in ipairs(lightsFolder:GetChildren()) do
		if part.Name == "Part" then
			for _, light in ipairs(part:GetChildren()) do
				if light:IsA("SpotLight") or light:IsA("SurfaceLight") or light:IsA("PointLight") then
					light.Enabled = powerOn 
					part.Transparency = powerOn and 0 or 1 
				end 
			end 
		elseif part.Name == "TV" then 
			local tvLight = part:FindFirstChild("TVLight") 
			if tvLight then 
				tvLight.Enabled = not powerOn 
			end 
		elseif part.Name == "Part2" then
			local sound = part:FindFirstChildOfClass("Sound1")
			if sound then
				sound.Playing = powerOn
			end
			local prompt = part:FindFirstChildOfClass("ProximityPrompt")
			if prompt then
				prompt.Enabled = powerOn
			end
		end 
	end 
	toggleDoors()
	if powerOn then
		proximityPrompt.ActionText = "Turn Off Power"
		soundOn:Play()
	else
		proximityPrompt.ActionText = "Turn On Power"
		soundOff:Play()
	end
end

local function onProximityPromptTriggered(player)
	togglePower(player)
	proximityPrompt.Enabled = false
	wait(cooldown)
	proximityPrompt.Enabled = true
end

proximityPrompt.Triggered:Connect(onProximityPromptTriggered)

The music is located in ReplicatedStorage under a folder shown here:
image

And the song that should play when the power goes off is also in ReplicatedStorage, but not in a folder:
image

If you know anything about doing this (since this is my first time trying to code something like this) give me ideas and tips to help fix this!

I don’t think you can play audios in replicatedstorage…?

they dont have the playing check enabled in the explorer, its supposed to be a playlist that randomizes and cycles through those two songs.

f they don’t then it means that the sound didn’t load, do you have permissions for it?

yes, they’re my own audios, i’ve given them permission