Help with my Lighting Script


I am trying to make a time setting script to make my game realistic at night by setting the brightness, ambient and Shadows. The above is my code. It partly works, but it continues to set the brightness higher and higher with each tick (because its a while true do loop obviously) until it’s at max which makes the game very bright. I’ve forgotten (I know its simple, and i’m pretty sure its got to do with variables) how to make it so it does it once each time the time either goes to night or day.

Also, I feel like i’ve made some errors in the Ambient, since it doesn’t change it (its currently at 137,137,137, and I’ve tried replicating it so it changes by setting the time to night then back again but it doesn’t work.) Could anyone help?

5 Likes

Couple things to point out.
One, when setting a color value you cannot just write r, g, b. It has to be Color3.fromRGB(r, g, b).
Two, if the variable name turns blue like “time” does, change the name. It is not advised to use variable names that are keywords (blue bolded words in script like game and workspace).

3 Likes

I’ve made your changes. It still isn’t working. Code is here

EDIT: I’ll try testing it in main game, not in studio.

1 Like

You made it wrong, actually you have to write lie.Ambient = Color3.fromRGB(137,137,137)

2 Likes

Made the edit. It still doesn’t work.

EDIT: This is probably bad programming as it takes an age to load into the actual game. How can I script this better?

1 Like

First of all, does do you have a script that changes time constantly?

1 Like

Yes I do, but it doesn’t seem to affect it (might be wrong)

2 Likes

There are a couple issues I would like to point out.

First, as mentioned by @SPycre, you can’t provide Lighting.Ambient with a tuple. You have to provide it with a Color3 value. It should look like Lighting.Ambient = Color3.new(R/255, G/255, B/255) or Lighting.Ambient = Color3.fromRGB(R, G, B).

Secondly, Lighting.Shadows isn’t a valid property. I believe you are trying to modify Lighting.GlobalShadows, E.g. Lighting.GlobalShadows = false/true.

Thirdly, it is unnecessary to run the logic 600 times a second (loop which waits for 0.1 seconds). You can increase the time to 60 seconds (after all, your logic is checking minutes after midnight). Better yet, you can use Instance:GetPropertyChangedSignal on Lighting.ClockTime or Lighting.TimeOfDay, like so:

local Lighting = game:GetService("Lighting") -- acceptable convention is to use the full service name.

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	-- Logic
end)

Before providing a solution, I would like to know how you implement your day/night cycle. If you provide this information we can integrate the logic into the cycle.

3 Likes

OK, this is the code (its a free model, so its not mine).

-- dayLength defines how long, in minutes, a day in your game is. Feel free to alter it.
local dayLength = 120

local cycleTime = dayLength*60
local minutesInADay = 24*60

local lighting = game:GetService("Lighting")

local startTime = tick() - (lighting:getMinutesAfterMidnight() / minutesInADay)*cycleTime
local endTime = startTime + cycleTime

local timeRatio = minutesInADay / cycleTime

if dayLength == 0 then
	dayLength = 1
end

repeat
	local currentTime = tick()
	
	if currentTime > endTime then
		startTime = endTime
		endTime = startTime + cycleTime
	end
	
	lighting:setMinutesAfterMidnight((currentTime - startTime)*timeRatio)
	wait(1/15)
until false
2 Likes

I personally do something like this, i do this on the client and then where it says “Do stuff” you simply put the code you want to run at either the start of night time or the start of day time.

Edit: You dont have to put this in a loop either, you can use any Changed or PropertyChanged signal such as clocktime or timeofday to handle this. I personally use clocktime.

while wait(1) do
		
	if Lighting:GetMinutesAfterMidnight() < 6*60 or Lighting:GetMinutesAfterMidnight() > 18*60 then
		LastTimeOfDay = LastCheck
		LastCheck = "Night"
	else
		LastTimeOfDay = LastCheck
		LastCheck = "Day"
	end
	
	if LastTimeOfDay ~= LastCheck then
		if LastCheck == "Night" then
			-- do stuff
		elseif LastCheck == "Day" then
			-- do stuff
		end
	end
end
2 Likes

@Hadiisepic I’ve solved the thing. Here’s a script wich make a day 120 mins long, and change lighting at day and at night.

-The First Script

while true do
wait(3)
game.Lighting.ClockTime = game.Lighting.ClockTime + .01
end

-And the second Script

Lighting = game:GetService('Lighting')


while wait(1) do
	
if Lighting:GetMinutesAfterMidnight() < 6*60 or Lighting:GetMinutesAfterMidnight() > 18*60 then
	LastTimeOfDay = LastCheck
	LastCheck = "Night"
else
	LastTimeOfDay = LastCheck
	LastCheck = "Day"
end

if LastTimeOfDay ~= LastCheck then
	if LastCheck == "Night" then
		print('night')
	Lighting.Brightness = 0
	Lighting.Ambient = Color3.fromRGB(0,0,0)
	Lighting.GlobalShadows = false
		-- do stuff
	elseif LastCheck == "Day" then
		print('day')
	Lighting.Brightness = 2
	Lighting.Ambient = Color3.fromRGB(137,137,137)
	Lighting.GlobalShadows = true
		-- do stuff
	end
end
end

put it in Workspace

3 Likes

Those two loops won’t run in the same script as is.

4 Likes

Yes, those 2 loops have to be on 2 differents scripts, in workspace.

3 Likes

Should probably put them in serverscriptservice. Aswell, just use one script. There is no reason to use more than one script in this case. Use a loop for the clocktime then use the Lighting:GetPropertyChangedSignal(“ClockTime”) to listen for the change in time. Or do as i recommended and handle your TimeOfDay on the server and then handle the rendering of the lighting on the clients with the script i originally posted.

3 Likes

Thank you so much, it works. Thank you everyone else for your help too :smile: I’ve also enabled streamingenabled.

2 Likes