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

How do I play sounds at different times of the day?

Got a very rudimentary script I made to play different ambience sounds at different parts of the day but my script won't work.

local Lighting = game:GetService("Lighting")
local Time = Lighting.ClockTime
local Birds = game.Workspace:FindFirstChild("Birds-chirping-sound-effect")
local Crickets = game.Workspace:FindFirstChild("Crickets")

if Time == "6.3" then
	Birds:Play()
	Crickets:Stop()
else 
	if Time == "17.5" then
		Birds:Stop()
		Crickets:Start()
	end
end


Could someone tell my why?

1 answer

It shouldn't be in double quotation marks. Just have the time without the quotation marks, and also, wrap this in a :GetPropertyChangedSignal("ClockTime")

local Lighting = game:GetService("Lighting")
local Time = Lighting.ClockTime
local Birds = game.Workspace:FindFirstChild("Birds-chirping-sound-effect")
local Crickets = game.Workspace:FindFirstChild("Crickets")

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
   if Time == 6.3 then -- No quotation marks.
      Birds:Play()
      Crickets:Stop()
   end

   if Time == 17.5 then -- No quotation marks.
      Birds:Stop()
      Crickets:Start()
   end
end)

Also, you don't have to use else. else will run the 2nd if statement if it's not equal to 6.3.

The way I'd do this is by using more than or less than symbols because if you were to start the game at 13:00:00, the code will not run, and there'll be silence.

local Lighting = game:GetService("Lighting")
local Time = Lighting.ClockTime
local Birds = game.Workspace:FindFirstChild("Birds-chirping-sound-effect")
local Crickets = game.Workspace:FindFirstChild("Crickets")

if Time >= 6.3 and Time < 17.5 then -- No quotation marks.
    Birds:Play()
    Crickets:Stop()
end

if Time >= 17.5 and Time < 6.3 then -- No quotation marks.
        Birds:Stop()
        Crickets:Start()
end

= means setting a value.

== means comparing a value

You can find more relational operators here.

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