Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making ambient shift to RGB instead of instant?

Hey there! Im currently having trouble figuring out how to make my ambient shift look more fluid instead of instantly becoming purple. If anyone has any tips or code I'd very much appreciate it! Sorry for messy code, I'm not very good with this! Thanks!

MinutesPerSecond = 2
RealisticDarkness = true --make tha game dark

if RealisticDarkness == true then
	for i,v in pairs(game.Lighting:GetChildren()) do
		if v:IsA("ColorCorrectionEffect") and v.Enabled == true then
			Color = v
			break
		end
	end
	if Color == nil then
		Color = Instance.new("ColorCorrectionEffect",game.Lighting) --makes cc effect
	end
end

while wait(0.05) do
	MinsAfterMid = game.Lighting:GetMinutesAfterMidnight()
	game.Lighting:SetMinutesAfterMidnight(MinsAfterMid+MinutesPerSecond*0.05)
	if RealisticDarkness == true then
		if MinsAfterMid >= 1080 or MinsAfterMid <= 360 then
			MinsPastSix = MinsAfterMid-1080
			if MinsPastSix < 0 then
				MinsPastSix = 360+MinsAfterMid
			end
			if MinsPastSix <= 180 then
				Color.Brightness = MinsPastSix/180*-0.2
				Color.TintColor = Color3.fromRGB (192, 44, 255) --making ambient purple
			elseif MinsPastSix >= 540 then
				Color.Brightness = -(0.8-MinsPastSix/180*0.2)
				Color.TintColor = Color3.fromRGB (144, 144, 144) --daytime
			else
				Color.Brightness = -0.2
				Color.TintColor = Color3.fromRGB (192, 44, 255)
			end
		end
	end
end

2 comments

0

TweenService.

0

^ Also Hue

1 answer

✓ Accepted answer

hey, I don't have experience with lighting so this code may be incorrect, but I'm fairly certain the general concept will have to be loop that changes the rgb values overtime. I found a nice looking method on the Color3 documentation called Color3:lerp().

local function shiftTint(rgbStart, rgbEnd, time)
	for alpha = 0, 1, 1 / (time * 10) do
		Color.TintColor = rgbStart:lerp(rgbEnd, alpha)
		wait(0.1)
	end
end

time will be the transition time in seconds. I haven't tested it so let me know if there's a problem

edit: PS, you should be using local when declaring variables even on the outside, it's neater :)

edit 2: from what I see, your brightness is dependent on time while your color is not, so it seems wasteful to create a new color every time. what I would do is make a variable that shows the time of day. every loop, you will check the time passed, and once it reaches your threshold, change that value to something like "night", "day", etc. this has the bonus of making it easier to understand.

if your time was in fact changed, this is where you run that function I gave you earlier, using the current tint and the new tint.

after all that, calculate your brightness like you did already, depending on the time of day. I would make an object to organize the data for each time of day. here's what I would do. (remember, haven't used lighting before so I can't optimize everything :) )

function shiftTint(rgbStart, rgbEnd, time)
	for alpha = 0, 1, 1 / (time * 10) do
		Color.TintColor = rgbStart:lerp(rgbEnd, alpha)
		wait(0.1)
	end
end

local Settings = {
	Night = {
		Tint = Color3.fromRGB (192, 44, 255),
		Brightness = function(t) return t / 180 * -0.2 end
	},
	Day = {
		Tint = Color.TintColor = Color3.fromRGB (144, 144, 144),
		Brightness = function(t) return -(0.8 - t / 180 * 0.2) end
	},
	Default = {
		Tint = Color3.fromRGB (192, 44, 255),
		Brightness = function(t) return -0.2 end
	}
}

local setting = Settings.Default
local LOOPTIME = 0.05
local MINS_PER_DAY = 60 * 24
local time = game.Lighting:GetMinutesAfterMidnight()

while true do
	-- now you only need to get the time once; the old time is added to and reused
	time = (time + minutesPerSecond * LOOPTIME) % MINS_PER_DAY
	game.Lighting:SetMinutesAfterMidnight(time)

	if RealisticDarkness == true and (time >= 1080 or time <= 360) then
		minsPastSix = time - 1080
		if minsPastSix < 0 then
			minsPastSix = 360 + time
		end

		local newSetting -- decide the current setting
		if minsPastSix <= 180 then
			newSetting = Settings.Night
		elseif MinsPastSix >= 540 then
			newSetting = Settings.Day
		else
			newSetting = Settings.Default
		end

		if setting ~= newSetting then -- transition the color if changed
			shiftTint(setting.Tint, newSetting.Tint, 3)
			setting = newSetting
		end

		Color.Brightness = setting.Brightness(time) -- adjsut brightness
	end
	wait(LOOPTIME)
end

also, I think that the people in the comments had a better solution that I did. tweenservice is built for smooth transitions, and I expect it works on color too (I didn't know that when I posted). my solution is a bit hackier, so I suggest you look into tweenservice as well :)

1 comment

0

I'm sorry, very bad at scripting! Where exactly should I put this code? And what should I edit? Thank you!

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