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

What's wrong with the script?

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

3 comments

0

output?

0

You should always add a "wait(0.001)" to loops like this

0

wait(0.001) won't work. wait(0.03) is the fastest it can be.

2 answers

✓ Accepted answer

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!

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

4 comments

0

You copied my script XP

0

I didn't, I just copied the asker's question, then added the wait and strings.

0

You copied my script

0

Seem familiar?

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