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

How to gradually change outdoor ambient color when the player touches a part?

I have attempted to change the outdoor ambient color when a player touches a part and make the color change gradually. The color stays the same as the first color and does not gradually change... any idea why? Nothing in output...

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Block = game.Lighting
		Block.OutdoorAmbient = Color3.new (185, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (182, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (180, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (178, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (176, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (174, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (172, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (170, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (168, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (166, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (164, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (162, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (160, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (158, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (156, 0, 0)
		wait(0.1)
		Block.OutdoorAmbient = Color3.new (154, 0, 0)			
	end
end)

1 answer

✓ Accepted answer

ANSWER: You're using Color3.new() when you should be using Color3.fromRGB(). In addition to all this I don't like to repeat code, so I'll help you save time by teaching you Tween as well. Enjoy!

Note: You also require a Debounce to prevent extra .Touched threads from running.

CODE FIX:

local TS = game:GetService("TweenService")

local goal = {}
goal.OutdoorAmbient = Color3.fromRGB(154, 0, 0) 
 
local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
 
local tween = TS:Create(game.Lighting, tweenInfo, goal)

local debounce = true
script.Parent.Touched:Connect(function(hit)
	if debounce and hit.Parent:FindFirstChild("Humanoid") then
		debounce = false
		local L = game.Lighting
		L.OutdoorAmbient = Color3.fromRGB(255, 0, 0)
		tween:Play()
	end
end)

Note: You have also chosen colors that look really similair so I changed them to help you see that my code is working. Feel free to edit them however you like.

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