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 coordinate day/night script with changing ambient?

The goal is to make a day and night cycle that also changes the ambient of the game. For example, when it is night, the ambient is darker - when it is day, the ambient is lighter.

I am using this script for a Day/Night cycle:

local speed = .2

time = game:service("Lighting"):GetMinutesAfterMidnight()

local full = 60 * 60 * 12

while true do
	time = (time + wait()/60*speed)
	game:service("Lighting"):SetMinutesAfterMidnight(time)
	if time >= full then time = time - full end
end

As for the ambient changes, I tried using this but it is always slightly dark even in broad daylight!

Intensity = game.Lighting

while true do
game.Lighting.TimeOfDay = "00:00:00"
Intensity.Ambient = Color3.new (0, 0, 0)
Intensity.OutdoorAmbient = Color3.new (0,0,0)
wait(90)
game.Lighting.TimeOfDay = "03:00:00"
Intensity.Ambient = Color3.new (0, 0, 0)
Intensity.OutdoorAmbient = Color3.new (0,0,0)
wait(60)
game.Lighting.TimeOfDay = "05:00:00"
Intensity.Ambient = Color3.new (0.15, 0.15, 0.15)
Intensity.OutdoorAmbient = Color3.new (0.15, 0.15, 0.15)
wait(30)
game.Lighting.TimeOfDay = "06:00:00"
Intensity.Ambient = Color3.new (0.3, 0.3, 0.3)
Intensity.OutdoorAmbient = Color3.new (0.3, 0.3, 0.3)
wait(60)
game.Lighting.TimeOfDay = "08:00:00"
Intensity.Ambient = Color3.new (0.45, 0.45, 0.45)
Intensity.OutdoorAmbient = Color3.new (0.45, 0.45, 0.45)
wait(60)
game.Lighting.TimeOfDay = "10:00:00"
Intensity.Ambient = Color3.new (0.6, 0.6, 0.6)
Intensity.OutdoorAmbient = Color3.new (0.6, 0.6, 0.6)
wait(60)
game.Lighting.TimeOfDay = "12:00:00"
Intensity.Ambient = Color3.new (0.7, 0.7, 0.7)
Intensity.OutdoorAmbient = Color3.new (0.7, 0.7, 0.7)
wait(90)
game.Lighting.TimeOfDay = "15:00:00"
Intensity.Ambient = Color3.new (0.65, 0.65, 0.65)
Intensity.OutdoorAmbient = Color3.new (0.65, 0.65, 0.65)
wait(60)
game.Lighting.TimeOfDay = "17:00:00"
Intensity.Ambient = Color3.new (0.6, 0.6, 0.6)
Intensity.OutdoorAmbient = Color3.new (0.6, 0.6, 0.6)
wait(30)
game.Lighting.TimeOfDay = "18:00:00"
Intensity.Ambient = Color3.new (0.55, 0.55, 0.55)
Intensity.OutdoorAmbient = Color3.new (0.55, 0.55, 0.55)
wait(30)
game.Lighting.TimeOfDay = "19:00:00"
Intensity.Ambient = Color3.new (0.35, 0.35, 0.35)
Intensity.OutdoorAmbient = Color3.new (0.35, 0.35, 0.35)
wait(30)
game.Lighting.TimeOfDay = "20:00:00"
Intensity.Ambient = Color3.new (0.25, 0.25, 0.25)
Intensity.OutdoorAmbient = Color3.new (0.25, 0.25, 0.25)
wait(90)
game.Lighting.TimeOfDay = "23:00:00"
Intensity.Ambient = Color3.new (0, 0, 0)
Intensity.OutdoorAmbient = Color3.new (0,0,0)
wait(30)

end 

-- 0.1 = 25 in the ambients

Thank you for any help!

1 answer

Here's a script I wrote up that should do the trick. Paste this into your day/night cycle script and remove the other ambient script. That's all!

Instead of making the OutdoorAmbient change during a certain hour, it's best to put in a cycle where it's constantly changing. This script determines what ambient it should have for the TimeOfDay.

local speed = 1000 --How fast do you want the cycle to be?
local Intensity = 15 --The higher the intensity, the brighter and darker it will be between days.
local full = 60*60*12
local time = game.Lighting:GetMinutesAfterMidnight()

while true do
local TimeOfDay = game.Lighting.TimeOfDay
    time = (time + wait()/60*speed)
    game:service("Lighting"):SetMinutesAfterMidnight(time)
if time >= full then time = time - full end
	local LightValue = (string.sub(TimeOfDay,1,2))+(string.sub(TimeOfDay,4,5)/100) --Converts the TimeOfDay into (hours).(minutes) number value.
	local lightequation = {}
if LightValue > 0 and LightValue <= 12 then --The time is between 0:00:01 and 12:00:00
lightequation = 20+(LightValue*Intensity) --Determines the required OutdoorAmbient for what time it is. The time in (hours).(minutes) form multiplied by 15 and added by the darkest point will be the ambient.
	game.Lighting.OutdoorAmbient = Color3.new(lightequation/255, lightequation/255, lightequation/255) --Apply the brightness. Lightequation is divided by 255 since it is a Color3 value.
else --The time is between 12:00:01 and 24:59:59
lightequation = (12(Intensity)+20)-((LightValue-12)*Intensity) --The time in (hours).(minutes) form subtracted by 12 (changes 12:24 into 0:12), multiplied by the intensity then subtracted by the brightest point will be the ambient.
	game.Lighting.OutdoorAmbient = Color3.new(lightequation/255, lightequation/255, lightequation/255) --Apply the brightness.
end end
--SkeletalReality

P.S. Ignore the bad rep, this is from previous questions that I failed at asking :|

4 comments

0

This website is not for giving people scripts, it is for helping people understand how to do something. You haven't explained anything at all.

0

My apologies. I added details on how the script operates and how it's an easier option. I'll be sure to explain things in future answers.

0

Sorry for the late response but thank you for the script! Although, I removed the two scripts I had and added this...but the sun isn't moving

0

CORRECTION: There was an error on line 17 but i managed to fix it - but the script only seems to make the day brighter, it doesnt get any darker during the night

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