Day/Night Script with Ambient?

Hello!

I’ve found some working day/night scripts that work the way I (almost) want them to. I’d like the day/night to sync with Roblox servers and they do that just fine. I’ve set the script to only check every 10 minutes hoping this will reduce load.

The feature I desire most however I cannot figure out. How would I add ambient changes to a script? Ambient makes a h-u-g-e difference in overall lighting quality. Currently I can change ambient settings but they stay the same through the entire cycle.

Any help is appreciated!

3 Likes

@sfinteractive If I understand your question correctly you want to change the ambiance of the game every 10 min.

To do this you can do a script in SSS with the following code.

while true do

game.Lighting.Ambient.Color3 = Color3.new(r,b,g)
wait(600);
end

2 Likes

Thanks! Let me do some testing and try it. I’d like to add it into the day/night script.

Is there a way to call it by time rather than wait? I’ve seen threads swearing wait is bad yet wait seems essential for some functions. Perhaps I could have a day/night that triggered only based on server time?

That would be a much longer script wait is a fine thing it has its bugs but if you use it right its fine but let me see if I can make you a script that runs off of sever time.

1 Like

Well the work is not your burden but that is quite generous! Even if you were able to post an example line of code showing me a timestamp style code it’d be very helpful.

While right now I cant think of a script to work as a wait time. To get the severs uptime you would use this.

local SeverTime = game.Workspace.DistributedGameTime

1 Like

Ambient itself doesn’t have a property called Color3. Color3.new requires you to specify a number from 0-1 which represent Red, Green and Blue. To make things easier, use the typical RGB values with Color3.fromRGB(r, g, b) which allows you to specify a range from 0-255. You should also realise that Ambient lighting is only really applied to indoor structures, while if you want to have a noticeable effect on the outside you should change the OutdoorAmbient

Correct code for changing the Ambient and OutdoorAmbient:

game.Lighting.Ambient = Color3.fromRGB(r, g, b)

game.Lighting.OutdoorAmbient = Color3.fromRGB(r, g, b)
2 Likes

@serverIess Color3.new lets you do any rbg value form 0-255 I use it all the time.

the only difference is that .new makes it so its a forced update on the sever by changing the color value

hence the

game.Lighting.Ambient.Color3 = Color3.new()

its the same with brickcolor you would do fkdjaflfkdjla.brickColor = brickColor.new()

Also your script would not work as your telling the game to change the color value of nothing.

game.Lighting.Ambient is not a color value.

My fault. I was using the term ambient in a general sense during the question. Your post gives me even more ability to customize though both indoor and outdoor.

This is not a valid index of Ambient. This returns an error, try it yourself. It is simply game.Lighting.Ambient.

Attempting to apply the RGB values 255, 255, 255 to a Color3.new() gave me the values of 65025, 65025, 65025 after I had checked the properties. Hence why I suggest you use Color3.fromRGB to prevent any funny business from going on.

Color3.new() should only be using a number from 0-1, which the source for that can be seen here.

1 Like

Oh I see what I did wrong my bad. Color3.new() does work perfectly fine however, I forgot that ambient is already stored as a color3 value and doesnt have a separate value for that.

Thank you for the correction. :slight_smile:

1 Like

If you don’t want to use wait() (which isn’t necessarily a bad thing), the other option you can take is connecting to a signal.

In this case, assuming you are using DistributedGameTime, you would get the changed signal for that property and create a callback that updates the ambient every time this property changes:

workspace:GetPropertyChangedSignal("DistributedGameTime"):Connect(function()
    local newTime = workspace.DistributedGameTime
    -- do stuff here
end)

There is a problem with this method, however. This callback will be called at an unnecessarily high rate because DistributedGameTime is meant to be accurate at a level smaller than seconds. So, while this might still be a viable idea, I recommend you set up a system to stop the ambience from actually doing anything most of the times it is called. For example (if you don’t have any conditional statements already):

local lastTime = 0

workspace:GetPropertyChangedSignal("DistributedGameTime"):Connect(function()
    local newTime = workspace.DistributedGameTime

    -- 1 second cooldown
    if newTime - lastTime < 1 then return end
    lastTime = newTime

    -- do stuff here
end)

It is still much simpler to use wait(). Its inaccuracy isn’t noticeable for us humans.

4 Likes

That does sound counter-productive. My main goal is to have a low resource, fully functioning, real time day/night with ambient and lighting changes. If wait is the best way that is fine. There was some vilifying of the wait function yet so many scripts use wait.

Consider making your ambient system work based on the time system. So if you set Lighting.ClockTime in your day/night code, hook up your signal to that instead of something generic like DistributedGameTime.

(As a bonus, this also means you can set the time however you like – e.g. using admin commands – and other related systems like ambience will work as the only thing they rely on is the time.)

If you don’t use Lighting.ClockTime or something that works similarly, you can create a BindableEvent and connect the ambience to time using that instead.

This approach should reduce the number of times that function is called (unless you are using TweenService or something on the time) and if it doesn’t then you can easily modify the day/time code so that it control that number without a significant performance overhead.


Oops, just realised that this still doesn’t solve the problem of how the time part of your system would work. So this post is kinda pointless. But don’t worry about it too much – what you’re doing shouldn’t be so expensive on performance that you will notice any issues. Do whatever you like :wink:

1 Like

Hah… but this sounds like it would work at least in my mind. What if I had the script check once when player enters server for the current time. This first and only check would set the lighting and ambients… anything after 8pm let’s say would be night. Anything after 6am day. 7:30pm-8pm sunset. 5:30am to 6am sunrise. Of course if the player was on for 4 hours or something you still need the cycle to progress. Really day/night should be built into Studio like terrain tools.

Yup, and perhaps just make a while loop that only runs once every 10 minutes or something with a wait(). It’s not a very intensive task so again you don’t need to worry that much about it unless you need to heavily optimise your game.

1 Like

Im late (1 year) but I want to share this guide with you.
It’s really useful and I liked the script technique. You may look at it.

1 Like