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.

--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
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

--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!

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.

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.

Final Product

--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

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