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

My sound won't play even though IsPlaying is true?

So i have some musics in my game and a script that plays the music. Everything works perfectly. But works perfectly at first, but later for some reason the music doesn't play, even though i called :Play() and the Sounds IsPlaying is true. Why does that happen? Heres my script:

local LevelMan = game.Workspace:WaitForChild("Level Manager")
local VM = game.Workspace:WaitForChild("ValueManager")
local GS = LevelMan:WaitForChild("GameStarted")
local IIL = LevelMan:WaitForChild("IsInLobby")
local VIU = VM:WaitForChild("ValueInUse")
local Lobby = script:WaitForChild("Lobby")
local Battle = script:WaitForChild("Battle")
local Intro = script:WaitForChild("Intro")

function PlayStopSound(sound,play)
	if play then
		print("Sound Manager: Playing "..sound.Name)
		sound:Play()
	else
		print("Sound Manager: Stopping "..sound.Name)
		sound:Stop()
	end
end

while wait(0) do
	if IIL.Value and not Lobby.IsPlaying then
		PlayStopSound(Lobby,true)
	elseif not IIL.Value and not Lobby.IsPaused then
		PlayStopSound(Lobby,false)
	end
	if string.sub(VIU.Value,1,5) == "Intro" and not Intro.IsPlaying then
		PlayStopSound(Intro,true)
	end
	if GS.Value and not Battle.IsPlaying and not Intro.IsPlaying then
		PlayStopSound(Battle,true)
	elseif not GS.Value and not Battle.IsPaused then
		PlayStopSound(Battle,false)
	end
end

Note that this script is in PlayerScripts.

UPDATE:

---------------
--Load Sounds--
---------------

local ContentProvider = game:GetService("ContentProvider") -- Content Provider Service

local assets = {237266522, 237258393, 286581126} -- Sounds to Load

--Preload sounds
for _, asset in ipairs(assets) do
	ContentProvider:Preload("http://www.roblox.com/asset/?id=" .. asset)
end

-------------
--Variables--
-------------

local LevelMan = game.Workspace:WaitForChild("Level Manager") -- Level Manager Script
local VM = game.Workspace:WaitForChild("ValueManager") -- Value Manager Script
local GS = LevelMan:WaitForChild("GameStarted") -- Game Started Value 
local IIL = LevelMan:WaitForChild("IsInLobby") -- Is In Lobby Value
local VIU = VM:WaitForChild("ValueInUse") -- Value In Use Value
local Lobby = script:WaitForChild("Lobby") -- Lobby Music: Kahoot Lobby Music
local Battle = script:WaitForChild("Battle") -- Battle Music: Robocraft Mars Battle Music 2
local Intro = script:WaitForChild("Intro") -- Before the Battle Music plays: Robocraft Mars Battle Music 1

------------
--Function--
------------

function PlayStopSound(sound,play)
	--Plays Music
	if play then
		print("Sound Manager: Playing "..sound.Name)
		sound:Play()
	--Stops Music
	else
		print("Sound Manager: Stopping "..sound.Name)
		sound:Stop()
	end
end

----------------
--Script Logic--
----------------

while wait(0) do
	--Play Lobby Music
	if IIL.Value and not Lobby.IsPlaying then
		PlayStopSound(Lobby,true)
	--Stop Lobby Music
	elseif not IIL.Value and not Lobby.IsPaused then
		PlayStopSound(Lobby,false)
	end
	--Play Intro Music
	if string.sub(VIU.Value,1,5) == "Intro" and not Intro.IsPlaying then
		PlayStopSound(Intro,true)
	end
	--Play Battle Music
	if GS.Value and not Battle.IsPlaying and not Intro.IsPlaying then
		PlayStopSound(Battle,true)
	--Stop Battle Music
	elseif not GS.Value and not Battle.IsPaused then
		PlayStopSound(Battle,false)
	end
end

1 answer

Sound will not be heard in PlayerScripts. A few examples of where it can be are PlayerGui and Player.

1 comment

0

But as i said, it works. I can hear the sounds at the first time. When i join the game, i actually hear the lobby music, when my game is starting i actually hear the intro music and when the game starts i hear the battle music. But when the game ends, it plays the lobby music, but i don't hear it, and happens with the rest of the musics.

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