I want that when it's daytime, one music plays. When it's nighttime - other music plays. I tried to script it but it didn't work. Any help please?
P.S. I have Day/Night script so nightime is possible.
--Sound Test local Time = game.Workspace.Lighting.TimeOfDay local SoundOne = game.Workspace.Music local SoundTwo = game.Workspace.Another while true do if Time == 06:00:00 then SoundOne:Stop() SoundTwo:Play() end if Time == 22:00:00 then SoundTwo:Stop() SoundOne:Play() end end
Simple. 1. You need a wait for your while loop 2. TimeOfDay is a string or text value
--Sound Test local Time = game.Workspace.Lighting.TimeOfDay local SoundOne = game.Workspace.Music local SoundTwo = game.Workspace.Another while wait() do if Time == "06:00:00" then SoundOne:Stop() SoundTwo:Play() end if Time == "22:00:00" then SoundTwo:Stop() SoundOne:Play() end end
Hope it helps!
Problem:
If you read in the ROBLOX wiki, you must use a wait()
function in loops.
while true do -- this isn't using a wait, therefore the client will crash. print("Loop") end while wait() do -- this on the other hand, will work because it has a wait print("loop will work") end
Consequences if wait()
is not used:
If not the use of wait() in loops, then the current loop will run continuously because it has no yielder, thus crashing the client.
--Sound Test local Time = game.Workspace.Lighting.TimeOfDay local SoundOne = game.Workspace.Music local SoundTwo = game.Workspace.Another while wait() do if Time == "06:00:00" then -- also TimeOfDay uses strings. SoundOne:Stop() SoundTwo:Play() end if Time == "22:00:00" then SoundTwo:Stop() SoundOne:Play() end end