Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What's wrong with the script?

Asked by 9 years ago

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 
03local Time = game.Workspace.Lighting.TimeOfDay
04local SoundOne = game.Workspace.Music
05local SoundTwo = game.Workspace.Another
06 
07while 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
17end
0
output? HungryJaffer 1246 — 9y
0
You should always add a "wait(0.001)" to loops like this fdfxd 50 — 9y
0
wait(0.001) won't work. wait(0.03) is the fastest it can be. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

Simple. 1. You need a wait for your while loop 2. TimeOfDay is a string or text value


Final Product

01--Sound Test
02 
03local Time = game.Workspace.Lighting.TimeOfDay
04local SoundOne = game.Workspace.Music
05local SoundTwo = game.Workspace.Another
06 
07while 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
17end


Hope it helps!

Ad
Log in to vote
0
Answered by
woodengop 1134 Moderation Voter
9 years ago

Problem:

If you read in the ROBLOX wiki, you must use a wait() function in loops.

1while true do -- this isn't using a wait, therefore the client will crash.
2    print("Loop")
3end
4 
5while wait() do -- this on the other hand, will work because it has a wait
6    print("loop will work")
7end

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.

Final Product

01--Sound Test
02 
03local Time = game.Workspace.Lighting.TimeOfDay
04local SoundOne = game.Workspace.Music
05local SoundTwo = game.Workspace.Another
06 
07while 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
17end
0
You copied my script XP EzraNehemiah_TF2 3552 — 9y
0
I didn't, I just copied the asker's question, then added the wait and strings. woodengop 1134 — 9y
0
You copied my script EzraNehemiah_TF2 3552 — 9y
0
Seem familiar? EzraNehemiah_TF2 3552 — 9y

Answer this question