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.
01 | --Sound Test |
02 |
03 | local Time = game.Workspace.Lighting.TimeOfDay |
04 | local SoundOne = game.Workspace.Music |
05 | local SoundTwo = game.Workspace.Another |
06 |
07 | while true do |
08 | if Time = = 06 : 00 : 00 then |
09 | SoundOne:Stop() |
10 | SoundTwo:Play() |
11 |
12 | end |
13 | if Time = = 22 : 00 : 00 then |
14 | SoundTwo:Stop() |
15 | SoundOne:Play() |
16 | end |
17 | end |
Simple. 1. You need a wait for your while loop 2. TimeOfDay is a string or text value
01 | --Sound Test |
02 |
03 | local Time = game.Workspace.Lighting.TimeOfDay |
04 | local SoundOne = game.Workspace.Music |
05 | local SoundTwo = game.Workspace.Another |
06 |
07 | while wait() do |
08 | if Time = = "06:00:00" then |
09 | SoundOne:Stop() |
10 | SoundTwo:Play() |
11 |
12 | end |
13 | if Time = = "22:00:00" then |
14 | SoundTwo:Stop() |
15 | SoundOne:Play() |
16 | end |
17 | end |
Hope it helps!
Problem:
If you read in the ROBLOX wiki, you must use a wait()
function in loops.
1 | while true do -- this isn't using a wait, therefore the client will crash. |
2 | print ( "Loop" ) |
3 | end |
4 |
5 | while wait() do -- this on the other hand, will work because it has a wait |
6 | print ( "loop will work" ) |
7 | 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.
01 | --Sound Test |
02 |
03 | local Time = game.Workspace.Lighting.TimeOfDay |
04 | local SoundOne = game.Workspace.Music |
05 | local SoundTwo = game.Workspace.Another |
06 |
07 | while wait() do |
08 | if Time = = "06:00:00" then -- also TimeOfDay uses strings. |
09 | SoundOne:Stop() |
10 | SoundTwo:Play() |
11 |
12 | end |
13 | if Time = = "22:00:00" then |
14 | SoundTwo:Stop() |
15 | SoundOne:Play() |
16 | end |
17 | end |