Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Audio is not playing when I call it?

I made a script where different music will play according to what is going on with the game. It's a LocalScriptin the StarterPlayerScripts, and the music folder is in ReplicatedStorage. However, the music doesn't play and there is no output error. Here's the code:

RoundRunning = game.ServerStorage.IsRoundRunning.Value
Inter = game.ServerStorage.Intermission.Value

--[[Song Variables]]--
local songs = game.ReplicatedStorage["Action Game Playlist"]
local battlefield = songs.SSBBBattlefield
local gourmet = songs.SSBBGourmetRace
local dedede = songs.SSBBKingDedede
local storm = songs.SSBBSongOfStorms
local tabuu = songs.SSBBVsTabuu
local tf2 = songs.TF2IntruderAlert
local patch = songs.MrPatch
local billy = songs.ChillyBilly
local temple = songs.Temple
local playlist = {battlefield, gourmet, dedede, storm, tabuu, tf2, patch, billy, temple}

--[[Functions]]--

function playSong()
	print("playing")
	local i = math.random(1, #playlist)
	local currentSong = playlist[i]
	currentSong:Play'' 
	currentSong.Looped = true
end

function stopSong()
	print("stopped")
	battlefield:Stop''
	gourmet:Stop''
	dedede:Stop''
	storm:Stop''
	tabuu:Stop''
	tf2:Stop''
	patch:Stop''
	billy:Stop''
	temple:Stop''
end


--[[Call the functions]]--

game.ServerStorage.Intermission.Changed:connect(function()
	if Inter == true then
	print("intermission is running")
	stopSong()
	end
end)

game.ServerStorage.IsRoundRunning.Changed:connect(function()
	if RoundRunning == true then
	print("round true")
	playSong()
	end
end)

2 comments

0

ServerStorage can only be accessed by server scripts. Use ReplicatedStorage for local scripts to be able to access it.

0

I changed all the game.ServerStorage to game.ReplicatedStorage, however it still doesn't work.

1 answer

✓ Accepted answer

First, change your function calls to use () instead of ''. Your way is technically functional, but makes the code a little weird to read.

Second, when you set RoundRunning = game.ServerStorage.IsRoundRunning.Value and Inter = game.ServerStorage.Intermission.Value, those variables RoundRunning and Inter will not change with the Values of IsRoundRunning and Intermission. They are set once and forgotten.

Actually, the Changed event passes the value of the thing changing. So while you could say things like Inter = game.Storage.Intermission; if (Inter.Value) then, you could instead remove your first two lines and make your functions look like:

game.ServerStorage.Intermission.Changed:connect(function(intermission)
    if (intermission) then
	    print("intermission is running")
	    stopSong()
    end
end)

game.ServerStorage.IsRoundRunning.Changed:connect(function(roundRunning)
    if (roundRunning) then
	    print("round true")
	    playSong()
    end
end)

1 comment

0

Thanks a lot! The script runs perfectly now.

This discussion is preserved as it was. New answers, comments, and votes are closed.