Script isn't playing sounds when :Play() is run

PLEASE SCROLL DOWN FOR CURRENT INFORMATION OF THE PROBLEM AND SCRIPT
K thx

So I’ve made a script that handles background music in my game. How its meant to work is that if a variable object is detected as false, it’ll go on to a while true loop (within an if statement) that checks for the time of the game, and plays a certain sound object depending wether the time is checked to be a certain time or not. This script isn’t changing the volume of the music neither playing or stopping any, and I don’t know why. The full script is as follows:

EventMusic = script.Parent.WinterMusic --Change for the music for event
L = game.Lighting.ClockTime
script.Parent.NonEventMusic:Play()

if Event.Value == true then
	EventMusic:Play()
	print("Event")
else
	EventMusic.Volume = 0
	print("No Event")
	while true do
		wait(.1)
		if L > 17.6 or L < 6.4 then
			if script.Parent.NonEventMusic.Playing == true and script.Parent.SpaceMusic.Playing == false then
				script.Parent.NonEventMusic.Volume = .2
				script.Parent.NonEventMusic.Volume = .1
				script.Parent.NonEventMusic:Stop()
				script.Parent.SpaceMusic.Volume = .3
				script.Parent.SpaceMusic:Play()
			end
		else
			if script.Parent.SpaceMusic.Playing == true and script.Parent.NonEventMusic.Playing == false then
				script.Parent.SpaceMusic.Volume = .2
				script.Parent.SpaceMusic.Volume = .1
				script.Parent.SpaceMusic:Stop()
				script.Parent.NonEventMusic.Volume = .3
				script.Parent.NonEventMusic:Play()
			end
		end
	end
end

The part of the code that is broken is the part of the script at the bottom where it checks for the game time (using local variable L as the game time) , and its meant to change the music when it turns day / night but it doesn’t do that at all, and I don’t know why…
I’d really appreciate help if anyone can help fix this code

1 Like

Just to note that this script is in a folder inside the starterGUI, not sure if thats a problem, although I don’t see how it could be

The script should be working fine, though you rlly should add comments.

I assume Event.Value is a bool Value, setting it to true would play EventMusic

Anything after this else statement will only run if this conditional statement is true. That is if EventMusic's Volume is = 0

Apart from that, everything should work fine. It would really help if you could clarify what you’re trying to achieve and how. I assume you’re toggling with different music properties such as "Playing" and playing them depending on that.

Well the script isn’t working. Thats why I made the post…
The event music plays fine, but the actual part of the script that checks time to play the background music for daytime or nighttime does not work.

So I’ve changed the script a bit, and I can tell that the while true do loop just ISN’T starting as none of these print statements are firing, anyone know why the while true do loop isn’t working? Perhaps I should make it in a seperate script and have this script make its disabled property false if event == false?

EventMusic = script.Parent.WinterMusic --Change for the music for event
L = game.Lighting.ClockTime

if Event.Value == true then
	EventMusic:Play()
	print("Event")
else
	EventMusic.Volume = 0
	print("No Event")
	while true do
		print("Test")
		if L > 17.6 or L < 6.4 then
			print("night")
			if script.Parent.NonEventMusic.Playing == false then
				script.Parent.SpaceMusic:Play()
				wait(script.Parent.SpaceMusic.TimeLength)
			end
		else
			print("Day")
			if script.Parent.SpaceMusic.Playing == false then
				script.Parent.NonEventMusic:Play()
				wait(script.Parent.NonEventMusic.TimeLength)
			end
		end
	end
end

Edit: The while true do loop doesn’t LOOP, it just fires once then stops. Is this because its in an if statement?
Edit2: So I put in a different script, and its sort of working, but the sounds aren’t being played. When I call :play() on a sound it doesn’t play it. Plz help

Maybe I missed it in the previous posts but did you say whether or not you had a script that changed the time of day?

1 Like

I didn’t mention so, but yes I do have a day / night script.

Ok, I notice there isn’t a wait call in the loop.

" Always include a delay such as wait() inside an infinite loop, as omitting it can freeze the game."

Edit: sorry I see them now. I wonder if you still need one outside the if statements?

There are waits in the script, the waits are after the sounds are meant to play, and wait until the time length of the audio MEANT to be played has passed. I have the script working as one of the messages you might of missed mentions. My problem is that the sounds aren’t being played when :play() is called to them and I don’t know why the sounds aren’t being played, despite print statements in the same section of code printing
I have waits already in the code, so having one outside the if statements wouldn’t be necessary as the if statements will ALWAYS run the code inside, the if statement is to prevent the audio being played while its already playing, and even then it will never run like so as the wait inside the if statement waits until the audio should’ve finished playing. But my audio isn’t being played which is my problem, I’m going to reply to this message with my current code incase I forgot to update a previous one.

1 Like
L = game.Lighting.ClockTime

while true do
	print("Test")
	if L > 17.6 or L < 6.4 then
		print("night")
		if script.Parent.NonEventMusic.Playing == false then
			script.Parent.SpaceMusic:Play()
			wait(script.Parent.SpaceMusic.TimeLength)
		end
	else
		print("Day")
		if script.Parent.SpaceMusic.Playing == false then
			script.Parent.NonEventMusic:Play()
			wait(script.Parent.NonEventMusic.TimeLength)
		end
	end
end

Just to mention that this code is in a seperate script from originally, but it does print the print statements inside it, although I don’t think its reading the time right as it always returns night instead of day when the time passes the range determined nighttime

Do print statements inside the if statements fire?

I did mention that the print statements inside do fire, however I haven’t seen the “day” print statement fire even when its day so I think the script is incorrectly detecting the time
Just realised that I should probably assign L inside the while true loop as my original time detecting code has it inside and it works fine. I’ll test if it works now
Edit: My audio’s still aren’t benig played when I run :Play() , Do you have any idea why?

1 Like

How do you have your sounds prepared?

The sounds are in a folder, at the location the script locates to find and play the sound. I don’t know why they aren’t playing so :man_shrugging:
You see any reason why they aren’t playing? The script is detecting day and night properly now but the script isn’t playing the sound objects

If the “night” print statement fires, the wait might still be causing the loop to pause even when L < 6.4 is true.

That isn’t my problem. I am calling a sound to play using :Play() And they aren’t being played

The loop would fire again after the song ended though. Unless the song takes the entire night time to end that shouldn’t be an issue.

Well thats the thing, the sound just ISN’T being played. It just… does not play, and it doesn’t take all night to end, not even close. I don’t know why the sound objects aren’t playing when I call :Play() to then when they should. The script and sounds are in the starterGUI but that shouldn’t be a problem I believe

I wonder if this is a preloading issue?

So perhaps I should preload the audio? Pretty sure this wouldn’t be an issue as its NEVER been an issue before, and I use much longer and bigger audio files than the one in this. The audio just isn’t playing at all, even though it should