Review for code

Im kind of starting to get to the halfway point of scripting knowledge of roblox. While I may not know too much so far, I can mostly make what I want work but probably have it have some flaws. Because of this, Im going to start posting more often here to make sure im scripting in a good and nice way instead of a flimsy and unsafe way.

Some code I made a while ago was a Day/night system. While it does work, I feel like there could be improvements to make it cleaner to read and just better overall.

_G.DayNightCycleDisabled = false
local Lighting = game.Lighting
local CometRM = game.ReplicatedStorage.RemoteEvents.CometCrystalActivataion
local EventOnGoing = false
local OriginalBrightness

CometRM.OnServerEvent:Connect(function()
	if EventOnGoing == false then
		EventOnGoing = true
		print('this should work')
		
	else
		print("There is already an event going on right now.")
	end
	
end)
while true do
	if _G.DayNightCycleDisabled == false or EventOnGoing == true then
     wait()
		
	else
		
	if Lighting.ClockTime == 12 then --- ill make this better later
		Lighting.ClockTime = 12.01
		local Occurrence = math.random(1,100)
			if Occurrence == 1 then
				Lighting.Ambient = Color3.new(0.388235, 0.411765, 0.576471)
				OriginalBrightness = Lighting.Brightness
				Lighting.OutdoorAmbient = Color3.new(0.388235, 0.411765, 0.576471)
				Lighting.Brightness = 2
					repeat wait(0.001)
						Lighting.ClockTime = Lighting.ClockTime + 0.1
						Lighting.ExposureCompensation = Lighting.ExposureCompensation - 0.03
					until Lighting.ClockTime >= 18 
---stuff will be put here to make this occurrence more instresting
				
			print('Comet.') --- this brings a dark night.
				repeat wait(0.5) 
					
				Lighting.ClockTime = Lighting.ClockTime + 0.01

			until Lighting.ClockTime >= 23 or EventOnGoing == true
			end
			if Occurrence == 2 then
				print("SomethingElse")
				Lighting.Sky.SunAngularSize = 50
			end
		
	else
    repeat wait(0.5) 
		Lighting.ClockTime = Lighting.ClockTime + 0.01
			until Lighting.ClockTime == 12 or EventOnGoing == true
		end
		
	end
end


1 Like

Code shorten.

_G.DayNightCycleDisabled = false
local Lighting = game.Lighting
local CometRM = game.ReplicatedStorage.RemoteEvents.CometCrystalActivataion
local EventOnGoing = false
local OriginalBrightness

CometRM.OnServerEvent:Connect(function()
	if EventOnGoing then
		print("There is already an event going on right now.")
	else
		EventOnGoing = true
		print('this should work')
	end
	
end)
while true do
	if (not _G.DayNightCycleDisabled) or EventOnGoing then
     wait()
		
	else
		
	if Lighting.ClockTime == 12 then --- ill make this better later
		Lighting.ClockTime = 12.01
		local Occurrence = math.random(1,100)
			if Occurrence == 1 then
				Lighting.Ambient = Color3.new(0.388235, 0.411765, 0.576471)
				OriginalBrightness = Lighting.Brightness
				Lighting.OutdoorAmbient = Color3.new(0.388235, 0.411765, 0.576471)
				Lighting.Brightness = 2
					repeat wait(0.001)
						Lighting.ClockTime = Lighting.ClockTime + 0.1
						Lighting.ExposureCompensation = Lighting.ExposureCompensation - 0.03
					until Lighting.ClockTime >= 18 
---stuff will be put here to make this occurrence more instresting
				
			print('Comet.') --- this brings a dark night.
				repeat wait(0.5) 
					
				Lighting.ClockTime = Lighting.ClockTime + 0.01

			until Lighting.ClockTime >= 23 or EventOnGoing
			end
			if Occurrence == 2 then
				print("SomethingElse")
				Lighting.Sky.SunAngularSize = 50
			end
		
	else
    repeat wait(0.5) 
		Lighting.ClockTime = Lighting.ClockTime + 0.01
			until Lighting.ClockTime == 12 or EventOnGoing
		end
		
	end
end
1 Like