Day and Night Script?

I run a DarkRP server and would like to know if anyone has ported an old Gmod 12 version of the Day and Night Script to Gmod 13. I’m fairly new to LUA and have no clue how to port something like this or if it even needs porting at all. If this is a stupid question “My bad” lol, i would just like to have a Day and Night Script that works for Gmod 13.

aghhh I saw one recently, hold on let me look for it.

That would be great if you could.

Anyone else know?

I use one in GMS13… it’s not as easy as putting a script on your server.

For it to work correctly there are entities that have to be placed on the map and set up correctly.

Here’s mine… modify it as it please you… but I’m posting this with 0 support as I probably won’t even check this thread again. Also, please do not PM me asking how to use this.

Also, it should be known that I didn’t write this… this is just a modified version of a script that was released some time ago, but that we’ve had around our community for a long time



if DayLight == nil then
	DayLight = {}
end

-- Times in minutes
DayLight.TimeTable = {
	Length = 1440,
	Start = 450,
	End = 1110,

	Dawn = 450,
	Dawn_Start = 360,
	Dawn_End = 510,

	Noon = 720,

	Dusk = 1010,
	Dusk_Start = 950,
	Dusk_End = 1070,

	Low = string.byte("a"),
	High = string.byte("z")
}

-- Days of the week
DayLight.DaysOfTheWeek = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

DL = {}


DL.tcolor = {}
DL.tcolor.r = 0.2
DL.tcolor.g = 0.3
DL.tcolor.b = 1

DL.bcolor = {}
DL.bcolor.r = 0.6
DL.bcolor.g = 0.8
DL.bcolor.b = 1

--CreateConVar( "DayLight_MinLength", "1", {FCVAR_REPLICATED,FCVAR_ARCHIVE,FCVAR_NOTIFY} )
local REALTIME = CreateConVar( "DayLight_RealTime", "0", {FCVAR_REPLICATED,FCVAR_ARCHIVE,FCVAR_NOTIFY} )
--CreateConVar( "DayLight_DynamicShadows", "0", {FCVAR_REPLICATED,FCVAR_ARCHIVE,FCVAR_NOTIFY} )

if DayLight.LightTable == nil then
	-- Run Variables
	DayLight.LightTable = {}
	DayLight.Minute = 360
	DayLight.MinuteIndex = 360
	DayLight.LastTick = 0
	DayLight.NextTick = 0
	DayLight.LastThink = 0
	DayLight.NextThink = 0
	DayLight.Sun = nil
	DayLight.RemoteSuns = {}
	DayLight.Sun_FX = nil
	DayLight.Sun_Angle = 0
	DayLight.Overlays = {}
	DayLight.WaterOverlays = {}
	DayLight.ShadowControl = nil
	DayLight.NightLights = {}
	DayLight.Day = math.random( 1, 7 )
end

function DayLight:Init()
	DayLight:SetTime()
	

-- Find ALL Entities
	for _,v in ipairs(ents.GetAll()) do
		if v.GetClass and v.GetName then
			-- Sun
			if v:GetClass() == "light_environment" then
				DayLight.Sun = v
			end
			-- Remote Suns
			if string.find( v:GetName(), "sun_remote" ) then
				table.insert( DayLight.RemoteSuns, v )
			end
			-- Sun_FX
			if v:GetClass() == "env_sun" then
				DayLight.Sun_FX = v
			end
			--ENV SkyPaint
			if v:GetClass() == "env_skypaint" then
				DL.SPEnt = v
				DL.SPEnt:SetKeyValue("starscale", "1")
			end
			
			-- Overlays
			if string.find(v:GetName(),"skyoverlay") or string.find(v:GetName(),"miscoverlay") then
				table.insert( DayLight.Overlays, v )
			end
			if string.find(v:GetName(),"wateroverlay") then
				table.insert( DayLight.WaterOverlays, v )
			end
			-- Shadow Control
			if v:GetClass() == "shadow_control" then
				DayLight.ShadowControl = v
			end
			-- Night Lights
			if string.find(v:GetName(),"nightlight") then
				table.insert( DayLight.NightLights, v )
			end
		end
	end

-- Sun
	if DayLight.Sun == nil then
		DayLight:Kill()
		return
	else
		DayLight.Sun:Fire( "FadeToPattern", string.char( DayLight.TimeTable.Low ), 0 )
		DayLight.Sun:Activate()
	end

-- Remote Suns
	if #DayLight.RemoteSuns > 0 then
		for _,v in ipairs(DayLight.RemoteSuns) do
			v:Fire( "FadeToPattern", string.char( DayLight.TimeTable.Low ), 0 )
			v:Activate()
		end
	end

-- Sun FX
	if DayLight.Sun_FX == nil then
		DayLight:Kill()
		return
	else
		DayLight.Sun_FX:SetKeyValue( "material" , "sprites/light_glow02_add_noz.vmt" )
		DayLight.Sun_FX:SetKeyValue( "overlaymaterial" , "sprites/light_glow02_add_noz.vmt" )
	end
	
-- Skypaint Kill
	if DL.SPEnt == nil then
		DayLight:Kill()
		return
	end

-- Overlays
	if #DayLight.Overlays > 0 then
		for _,v in ipairs(DayLight.Overlays) do
			v:Fire( "Enable", "", 0 )
			v:Fire( "Color", "0 0 0", 0.01 )
		end
	end
	if #DayLight.WaterOverlays > 0 then
		for _,v in ipairs(DayLight.WaterOverlays) do
			v:Fire( "Enable", "", 0 )
			v:Fire( "Color", "0 0 0", 0.01 )
		end
	end

	DayLight:BuildLightTable()

	if REALTIME:GetBool() then
		DayLight.Minute = tonumber((os.date("%H")))*60+tonumber((os.date("%M")))
		if GetConVarNumber( "DayLight_RealTime" ) == 1 then
			DayLight.Day = tonumber( os.date( "%w" ) )
		end
	end

	DayLight.NextThink = 0
end
hook.Add( "InitPostEntity", "DayLight_Init", DayLight.Init )

function DayLight:ScaleSBColor( val )

	if DL == nil then return end
	if DL.SPEnt == nil then return end

	DL.SPEnt:SetKeyValue("topcolor", tostring(DL.tcolor.r * val) .. " " .. tostring(DL.tcolor.g * val) .. " " .. tostring(DL.tcolor.b * val))
	DL.SPEnt:SetKeyValue("bottomcolor", tostring(DL.bcolor.r * val) .. " " .. tostring(DL.bcolor.g * val) .. " " .. tostring(DL.bcolor.b * val))
	DL.SPEnt:SetKeyValue("suncolor", "0 0 0")
	
	if val > 0.5 then
		DL.SPEnt:SetKeyValue("duskintensity", "0")
	elseif val <= 0.5 and val > 0.05 then
		DL.SPEnt:SetKeyValue("duskintensity", tostring( ( 1 * val ) + 0.3 ) )
	else
		DL.SPEnt:SetKeyValue("duskintensity", "0")
	end
	
	if DayLight.Minute < 349 or DayLight.Minute > 1085 then
		DL.SPEnt:SetKeyValue("starfade", "1")
	else
		DL.SPEnt:SetKeyValue("starfade", "0")
	end
end

function DayLight:Kill()
	hook.Remove( "InitPostEntity", "DayLight_Init" )
	hook.Remove( "Think", "DayLight_Think" )

	Msg( "#### DayLight: SCRIPT FAILURE, MISSING MAP ENTITIES!
" )
end

function DayLight:BuildLightTable()
	DayLight.LightTable = {}

	for i=1, DayLight.TimeTable.Length do
		local letter = string.char( DayLight.TimeTable.Low )
		local color = Color( 0, 0, 0, 0 )
		
		-- calculate which letter to use in the light pattern.
		if i >= DayLight.TimeTable.Dawn_Start and i < DayLight.TimeTable.Dawn_End then
			local progress = ( DayLight.TimeTable.Dawn_End - i ) / ( DayLight.TimeTable.Dawn_End - DayLight.TimeTable.Dawn_Start )
			local letter_progress = 1 - math.EaseInOut( progress , 0 , 1 )
						
			letter = ( ( DayLight.TimeTable.High - DayLight.TimeTable.Low ) * letter_progress ) + DayLight.TimeTable.Low
			letter = math.ceil( letter )
			letter = string.char( letter )
		elseif i >= DayLight.TimeTable.Dusk_Start and i < DayLight.TimeTable.Dusk_End then
			local progress = ( i - DayLight.TimeTable.Dusk_Start ) / ( DayLight.TimeTable.Dusk_End - DayLight.TimeTable.Dusk_Start )
			local letter_progress = 1 - math.EaseInOut( progress , 0 , 1 )
						
			letter = ( ( DayLight.TimeTable.High - DayLight.TimeTable.Low ) * letter_progress ) + DayLight.TimeTable.Low
			letter = math.ceil( letter )
			letter = string.char( letter )
		elseif (i > 0 and i < DayLight.TimeTable.Dawn_Start) or (i >= DayLight.TimeTable.Dusk_End and i <= 1440) then
			letter = string.char( DayLight.TimeTable.Low )
		else
			letter = string.char( DayLight.TimeTable.High )
		end
		
		-- calculate colors.
		if i >= DayLight.TimeTable.Dawn_Start and i <= DayLight.TimeTable.Dawn_End then
			-- golden dawn.
			local frac = ( i - DayLight.TimeTable.Dawn_Start ) / ( DayLight.TimeTable.Dawn_End - DayLight.TimeTable.Dawn_Start )
			if i < DayLight.TimeTable.Dawn then
				color.r = 100 * frac
				color.g = 64 * frac
			else
				color.r = 100 - ( 100 * frac )
				color.g = 64 - ( 64 * frac )
			end
		elseif i >= DayLight.TimeTable.Dusk_Start and i <= DayLight.TimeTable.Dusk_End then
			-- red dusk.
			local frac = ( i - DayLight.TimeTable.Dusk_Start ) / ( DayLight.TimeTable.Dusk_End - DayLight.TimeTable.Dusk_Start )
			if i < DayLight.TimeTable.Dusk then
				color.r = 40 * frac
			else
				color.r = 40 - ( 40 * frac )
			end
		elseif i >= DayLight.TimeTable.Dusk_End or i <= DayLight.TimeTable.Dawn_Start then
			-- blue hinted night sky.
			if i > DayLight.TimeTable.Dusk_End then
				local frac = ( i - DayLight.TimeTable.Dusk_End ) / ( DayLight.TimeTable.Length - DayLight.TimeTable.Dusk_End )
				color.r = 1 * frac
				color.g = 1 * frac
				color.b = 2 * frac
			else
				local frac = i / DayLight.TimeTable.Dawn_Start
				color.r = 1 - ( 1 * frac )
				color.g = 1 - ( 1 * frac )
				color.b = 2 - ( 2 * frac )
			end
		end

		DayLight.LightTable* = {}

		DayLight.LightTable*.Alpha = 0
		if i >= DayLight.TimeTable.Dawn_Start and i <= DayLight.TimeTable.Dawn_End then
			local progress = ( DayLight.TimeTable.Dawn_End - i ) / ( DayLight.TimeTable.Dawn_End - DayLight.TimeTable.Dawn_Start )
			DayLight.LightTable*.Alpha = math.floor( 250 * progress )
		elseif i >= DayLight.TimeTable.Dusk_Start and i <= DayLight.TimeTable.Dusk_End then
			local progress = ( i - DayLight.TimeTable.Dusk_Start ) / ( DayLight.TimeTable.Dusk_End - DayLight.TimeTable.Dusk_Start )
			DayLight.LightTable*.Alpha = math.floor( 250 * progress )
		elseif (i >= 0 and i <= DayLight.TimeTable.Dawn_Start) or (i >= DayLight.TimeTable.Dusk_End and i <= 1440) then
			DayLight.LightTable*.Alpha = 250
		end

		DayLight.LightTable*.Pattern = letter;
		--DayLight.LightTable*.Alpha = math.floor( 255 * math.Clamp( math.abs( ( i - DayLight.TimeTable.Noon ) / DayLight.TimeTable.Noon ) , 0 , 0.9 ) )
		--DayLight.LightTable*.Alpha = math.floor( 255 * (math.cos(i/230)/2+0.5) )
		DayLight.LightTable*.Color = math.floor( color.r ).." "..math.floor( color.g ).." "..math.floor( color.b )

	end
end

function DayLight:Think()
	local curtime = CurTime()

	if curtime < DayLight.NextTick then return end
	DayLight.NextTick = curtime + 0.50

	local SunFX_Angle = DayLight.Minute / DayLight.TimeTable.Length * 360 + 90
	if SunFX_Angle > 360 then SunFX_Angle = SunFX_Angle - 360 end
	if DayLight.Sun_FX then
		DayLight.Sun_FX:Fire( "AddOutput", "pitch "..SunFX_Angle, 0 )
		DayLight.Sun_FX:Activate()
	end

	/*
	local ShadowControl_Angle = DayLight.Minute / DayLight.TimeTable.Length * 360 - 90
	if DayLight.ShadowControl then
		local sunang = DayLight.Sun_FX:GetAngles() or DayLight.ShadowControl:GetAngles()
		DayLight.ShadowControl:Fire( "SetAngles", ShadowControl_Angle.." "..(sunang.yaw+180).." "..sunang.roll, 0 )
		DayLight.ShadowControl:Activate()
		for _, v in ipairs(ents.GetAll()) do
			if v.PhysWake then v:PhysWake() end
		end
	end
	*/

	for _,v in ipairs(DayLight.Overlays) do
		if v != nil then
			v:Fire( "Alpha", DayLight.LightTable[DayLight.MinuteIndex].Alpha, 0 )
			v:Fire( "Color", DayLight:GetFade( DayLight.Minute-math.floor(DayLight.Minute) ), 0 )
			v:SetRenderMode( RENDERMODE_TRANSALPHA )
		end
	end

	for _,v in ipairs(DayLight.WaterOverlays) do
		if v != nil then
			v:Fire( "Alpha", math.floor(DayLight.LightTable[DayLight.MinuteIndex].Alpha*0.98), 0 )
			v:Fire( "Color", DayLight:GetFade( DayLight.Minute-math.floor(DayLight.Minute) ), 0 )
			v:SetRenderMode( RENDERMODE_TRANSALPHA )
		end
	end
	
	if REALTIME:GetBool() then
		local date = os.date( "*t" )
		ErrorNoHalt( (date.hour*60).." "..date.min.." "..(date.sec/60).."
" )
		DayLight.Minute = (date.hour*60) + date.min + (date.sec/60)
		DayLight.Day = date.wday
	else
		DayLight.Minute = DayLight.Minute + (curtime - DayLight.LastTick)
		if DayLight.Minute > DayLight.TimeTable.Length then
			DayLight.Minute = DayLight.Minute - DayLight.TimeTable.Length
			DayLight.Day = DayLight.Day + 1
			if DayLight.Day > 7 then DayLight.Day = 1 end
		end
	end

	DayLight.MinuteIndex = math.Clamp( math.Round(DayLight.Minute), 1, DayLight.TimeTable.Length )

	DayLight.LastTick = curtime

	if curtime < DayLight.NextThink then return end
	DayLight.NextThink = curtime + 1

	--ErrorNoHalt( DayLight.MinuteIndex.."
" )
	local pattern = DayLight.LightTable[DayLight.MinuteIndex].Pattern
	if DayLight.Sun then
		DayLight.Sun:Fire( "FadeToPattern", pattern, 0 )
		DayLight.Sun:Activate()
	end

	if #DayLight.RemoteSuns > 0 then
		for _,v in ipairs(DayLight.RemoteSuns) do
			v:Fire( "FadeToPattern", pattern, 0 )
			v:Activate()
		end
	end

	DayLight:SetTime()

	if DayLight.MinuteIndex >= DayLight.TimeTable.Dusk_End then
		if !DayLight.LightsAreOn then DayLight:LightsOn() end
	elseif DayLight.MinuteIndex >= DayLight.TimeTable.Dawn then
		if DayLight.LightsAreOn then DayLight:LightsOff() end
	end
	
	if DayLight.Minute < 350 then --Night Time After Midnight
		DayLight:ScaleSBColor(0)
	elseif DayLight.Minute >= 350 and DayLight.Minute < 510 then--Dawn Start to Dawn End	(0.1 to 0.3)
		DayLight:ScaleSBColor( ( ( ( DayLight.Minute - 350 ) / 159 ) * 0.2 ) + 0.1  )
	elseif DayLight.Minute >= 510 and DayLight.Minute < 660 then --Morning					(0.3 to 1)
		DayLight:ScaleSBColor( ( ( ( DayLight.Minute - 510 ) / 149 ) * 0.7 ) + 0.3  )
	elseif DayLight.Minute >= 660 and DayLight.Minute < 780 then --MIDDAY
		DayLight:ScaleSBColor(1)
	elseif DayLight.Minute >= 780 and DayLight.Minute < 950 then --AfterNoon				(1 to 0.3)
		DayLight:ScaleSBColor(( ( ( ( ( DayLight.Minute - 949 ) / 169 ) * 0.7 ) * -1 ) + 0.3  ) )
	elseif DayLight.Minute >= 950 and DayLight.Minute < 1090 then --Dusk Start to Dusk End
		DayLight:ScaleSBColor( ( ( ( ( DayLight.Minute - 1089 ) / 139 ) * 0.2 ) * -1 ) + 0.1  )
	elseif DayLight.Minute >= 1090 and DayLight.Minute <= 1440 then --Dusk End to Midnight
		DayLight:ScaleSBColor(0)
	else
		DayLight:ScaleSBColor(0)
	end

	hook.Call( "DayLightMinute", GAMEMODE, DayLight.MinuteIndex )
end
hook.Add( "Think", "DayLight_Think", DayLight.Think )


function DayLight:GetFade( i )
	local first = string.Explode( " ", DayLight.LightTable[DayLight.MinuteIndex].Color )
	local second_min = DayLight.MinuteIndex
	if second_min > DayLight.TimeTable.Length then second_min = 1 end
	local second = string.Explode( " ", DayLight.LightTable[second_min].Color )

	first[1] = math.Round(first[1]*((i-1)*-1))
	first[2] = math.Round(first[2]*((i-1)*-1))
	first[3] = math.Round(first[3]*((i-1)*-1))
	second[1] = math.Round(second[1] * i)
	second[2] = math.Round(second[2] * i)
	second[3] = math.Round(second[3] * i)

	local col = { first[1]+second[1], first[2]+second[2], first[3]+second[3] }
	col = string.Implode( " ", col )
	return col
end

function DayLight:SetTime()
	local min = DayLight.MinuteIndex
	local hour = math.floor(min/60)
	local min = math.floor(min%60)

	SetGlobalInt( "DayLightMinute", min )

	if min < 10 then min = "0"..tostring(min) end
	local pm = false
	if hour >= 12 then
		hour = hour - 12
		pm = true
	end
	if hour == 0 then hour = 12 end
	if pm then
		SetGlobalString( "DayLightTime", DayLight.DaysOfTheWeek[DayLight.Day].." "..hour..":"..min.." PM" )
	else
		SetGlobalString( "DayLightTime", DayLight.DaysOfTheWeek[DayLight.Day].." "..hour..":"..min.." AM" )
	end
	

end

function DayLight:LightsOn()
	local pattern = ""
	local delay = 0
	for _,v in ipairs(DayLight.NightLights) do
		-- Flicker Function
		if math.random() >= 0.50 then
			pattern = "az"
		else
			pattern = "za"
		end

		delay = math.random() * 3

		v:Fire( "SetPattern", pattern, delay )
		v:Fire( "TurnOn", "", delay )

		timer.Simple( delay, function() v:EmitSound("buttons/button1.wav",math.random()*10+70,math.random()*10+95) end )

		delay = delay + math.random()*0.40
		v:Fire( "SetPattern", "z", delay )
		v:Fire( "FireUser1", "", delay )
	end

	DayLight.LightsAreOn = true
end

function DayLight:LightsOff()
	for _,v in ipairs(DayLight.NightLights) do
		v:Fire( "TurnOff", "", 0 )
		v:Fire( "FireUser2", "", 0 )
	end

	DayLight.LightsAreOn = false
end

function DayLight_SetTime( ply, cmd, args )
	if ply:IsAdmin() then
		if table.Count(args) >= 1 and tonumber(args[1]) then
			DayLight.Minute = math.Clamp(math.Round(tonumber(args[1])),1,DayLight.TimeTable.Length)
		end
	end
end
concommand.Add( "DayLight_SetTime", DayLight_SetTime )


I also have one you can use (at the moment, mostly only for advanced users!)

http://gmodcapsadmin.googlecode.com/svn/trunk/addons/tod/

(i’ll put it on workshop while at the same time making it more userfriendly sometime later)

http://dl.dropbox.com/u/244444/tod/1.jpg

http://dl.dropbox.com/u/244444/tod/2.jpg

http://dl.dropbox.com/u/244444/tod/3.jpg

http://dl.dropbox.com/u/244444/tod/4.jpg

http://dl.dropbox.com/u/244444/tod/5.jpg

The snow is hard coded into it at the moment and it activates at this time of year. It tries to replace grass with snow, so it might yield unexpected results on some maps.

If you want to customize the time presets you should see the bottom of tod.lua

It works by lerping between time presets. By default I have 4 presets, “dusk, day, dawn, night”

to set the time of day run

settod 0-24

“sv_tod 1” syncs it to real time