Script not working

Title says it all. I’m trying to change the FogColor using a LocalScript (because normal scripts are too laggy). An error comes up, saying Unable to assign property FogColor. Color3 expected, got nil - Client - LocalScript:60
Script:

local function generateRandomColor()
	local color
	local red = Color3.fromRGB(255,0,0)
	local lime = Color3.fromRGB(0,255,0)
	local green = Color3.fromRGB(0, 93, 0)
	local orange = Color3.fromRGB(255, 130, 0)
	local yellow = Color3.fromRGB(255,255,0)
	local cyan = Color3.fromRGB(0, 255, 255)
	local blue = Color3.fromRGB(0,0,255)
	local indigo = Color3.fromRGB(70, 0, 255)
	local violet = Color3.fromRGB(120,0,255)
	local pink = Color3.fromRGB(255,0,255)
	local brown = Color3.fromRGB(81, 40, 0)
	local black = Color3.fromRGB(0,0,0)
	
	local rand = Random.new(tick()):NextNumber(1,12)
	print(rand)
	if rand == 1 then
		color = red
		return color
	elseif rand == 2 then
		color = lime
		return color
	elseif rand == 3 then
		color = green
		return color
	elseif rand == 4 then
		color = orange
		return color
	elseif rand == 5 then
		color = yellow
		return color
	elseif rand == 6 then
		color = cyan
		return color
	elseif rand == 7 then
		color = blue
		return color
	elseif rand == 8 then
		color = indigo
		return color
	elseif rand == 9 then
		color = violet
		return color
	elseif rand == 10 then
		color = pink
		return color
	elseif rand == 11 then
		color = brown
		return color
	elseif rand == 12 then
		color = black
		return color
	end
end

while true do
	print("ran")
	task.wait()
	game.Lighting.FogColor = generateRandomColor() --where it says error occurs
end

Thanks in advance.
NOTE: I might not respond to your answer immediately because Iive in a different time zone.

try replacing rand with

local rand = math.random(12)
1 Like

You have to use :NextInteger instead of :NextNumber.

(you could also just use math.random like Kizylle said)

1 Like

Thanks, it worked! @ketrab2004’s solution ALSO worked, but yours was the most efficient!