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

While loop only works in studio?

Not sure what the issue is, the code is suppose to print when a sound is playing, however it does not work in game but does in studio.

Script (not Local):

local Sound = script:WaitForChild("Sound")

while Sound.IsPlaying == true do
	wait(1)
	print("Playing")
end

1 comment

0

There are no errors before anyone asks.

2 answers

✓ Accepted answer

This code does not print whenever a sound is playing.

It prints until the sound stops playing. If it's not playing at the beginning, the code will never run.

If you want to print whenever it's playing, you need to always loop and conditionally print:

while true do
	if Sound.IsPlaying then
		print("Playing!")
	end
	wait(1)
end

Note that if a LocalScript plays the sound (and it's not replicated because it's local or FilteringEnabled is on) then a Script won't be able to get IsPlaying.


Things like == true are redundant and clutter up your code!

Well there might be a bug on roblox. Did you hear the Music Playing InGame? Sometimes Roblox Cant get Sound IDs to load into Roblox itself for whatever reason.

1 comment

0

Yes the music plays ingame.. that has nothing to do with this. I am asking why the IsPlaying isn't working.

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