local n={}local function R(n,R,Y,z,d)if Y and not tonumber(Debounce)then Debounce=0 end if R then for R,Y in pairs(n:GetDescendants())do if Y:IsA("\066\097\115\101\080\097\114\116")then Y.Material="\078\101\111\110"elseif Y:IsA("\076\105\103\104\116")then Y.Enabled=true if z==true then Y.Color=Color3.fromRGB(H_BEAM_COLOR[1],H_BEAM_COLOR[2],H_BEAM_COLOR[3])Y.Range=H_BEAM_RANGE Y.Brightness=H_BEAM_BRIGHTNESS elseif z==false then Y.Color=Color3.fromRGB(L_BEAM_COLOR[1],L_BEAM_COLOR[2],L_BEAM_COLOR[3])Y.Range=L_BEAM_RANGE Y.Brightness=L_BEAM_BRIGHTNESS end if d and n==TL then Y.Range=B_LIGHT_RANGE Y.Brightness=B_LIGHT_BRIGHTNESS elseif not d and n==TL then Y.Range=T_LIGHT_RANGE Y.Brightness=T_LIGHT_BRIGHTNESS end end end else for n,R in pairs(n:GetDescendants())do if R:IsA("\066\097\115\101\080\097\114\116")then if d and CurrentMode<4 then R.Material="\078\101\111\110"else R.Material="\083\109\111\111\116\104\080\108\097\115\116\105\099"end elseif R:IsA("\076\105\103\104\116")then R.Enabled=false if d and CurrentMode<4 then R.Enabled=true R.Range=T_LIGHT_RANGE R.Brightness=T_LIGHT_BRIGHTNESS end end end end if Y and tonumber(Debounce)then Debounce+=1 end if CurrentMode==4 and Debounce==3 then Debounce=true elseif CurrentMode==1 and Debounce==2 then Debounce=true elseif CurrentMode==2 and Debounce==1 then Debounce=true elseif CurrentMode==3 and Debounce==1 then Debounce=true end end local function Y(n,R,Y,z,d)local l={}if Y and not tonumber(Debounce)then Debounce=0 end if R then for R,Y in pairs(n:GetDescendants())do if Y:IsA("\066\097\115\101\080\097\114\116")then Y.Material="\078\101\111\110"elseif Y:IsA("\076\105\103\104\116")then table.insert(l,Y)Y.Enabled=true if z==true then Y.Color=Color3.fromRGB(H_BEAM_COLOR[1],H_BEAM_COLOR[2],H_BEAM_COLOR[3])Y.Range=H_BEAM_RANGE Y.Brightness=H_BEAM_BRIGHTNESS elseif z==false then Y.Color=Color3.fromRGB(L_BEAM_COLOR[1],L_BEAM_COLOR[2],L_BEAM_COLOR[3])Y.Range=L_BEAM_RANGE Y.Brightness=L_BEAM_BRIGHTNESS end if d and n==TL then Y.Range=B_LIGHT_RANGE Y.Brightness=B_LIGHT_BRIGHTNESS elseif not d and n==TL then Y.Range=T_LIGHT_RANGE Y.Brightness=T_LIGHT_BRIGHTNESS end end end local R={}for Y,z in pairs(l)do table.insert(R,z.Range)table.insert(R,z.Brightness)if d and n==TL then z.Range=T_LIGHT_RANGE z.Brightness=T_LIGHT_BRIGHTNESS else z.Range=0 z.Brightness=0 end end for n,Y in pairs(l)do(TS:Create(Y,FadeTweenInfo,{Range=R[n*2-1]})):Play();(TS:Create(Y,FadeTweenInfo,{Brightness=R[n*2]})):Play()end wait(FADE_TIME)else for n,R in pairs(n:GetDescendants())do if R:IsA("\066\097\115\101\080\097\114\116")then if d and CurrentMode<4 then R.Material="\078\101\111\110"else R.Material="\083\109\111\111\116\104\080\108\097\115\116\105\099"end elseif R:IsA("\076\105\103\104\116")then table.insert(l,R)if d and CurrentMode<4 then R.Enabled=true end end end local R={}for n,Y in pairs(l)do table.insert(R,Y.Range)table.insert(R,Y.Brightness)end if d==nil then for n,R in pairs(l)do(TS:Create(R,FadeTweenInfo,{Range=0})):Play();(TS:Create(R,FadeTweenInfo,{Brightness=0})):Play()end wait(FADE_TIME)elseif d==true then for n,R in pairs(l)do if CurrentMode==4 then(TS:Create(R,BrakeFadeInfo,{Range=0})):Play();(TS:Create(R,BrakeFadeInfo,{Brightness=0})):Play()elseif CurrentMode<4 then(TS:Create(R,BrakeFadeInfo,{Range=T_LIGHT_RANGE})):Play();(TS:Create(R,BrakeFadeInfo,{Brightness=T_LIGHT_BRIGHTNESS})):Play()end end wait(B_LIGHT_FADE_TIME)for n,R in pairs(l)do R.Range=T_LIGHT_RANGE R.Brightness=T_LIGHT_BRIGHTNESS end end for n,Y in pairs(l)do if d and CurrentMode<4 then Y.Enabled=true else Y.Enabled=false Y.Range=R[n*2-1]Y.Brightness=R[n*2]end end end if Y and tonumber(Debounce)then Debounce+=1 end if CurrentMode==4 and Debounce==3 then Debounce=true elseif CurrentMode==1 and Debounce==2 then Debounce=true elseif CurrentMode==2 and Debounce==1 then Debounce=true elseif CurrentMode==3 and Debounce==1 then Debounce=true end end n.ChangeLight=function()if tonumber(Debounce)then return end if CurrentMode==4 then if Debounce==true then CurrentMode=1 Debounce=false n.ChangeLight()return elseif Debounce==false then if LIGHT_FADE then spawn(function()Y(FL,false,true)end)spawn(function()Y(HL,false,true)end)spawn(function()Y(TL,false,true)end)else spawn(function()R(FL,false,true)end)spawn(function()R(HL,false,true)end)spawn(function()R(TL,false,true)end)end end elseif CurrentMode==1 then if Debounce==true then CurrentMode+=1 Debounce=false n.ChangeLight()return elseif Debounce==false then if LIGHT_FADE then spawn(function()Y(HL,true,true,false)end)spawn(function()Y(TL,true,true)end)else spawn(function()R(HL,true,true,false)end)spawn(function()R(TL,true,true)end)end end elseif CurrentMode==2 then if Debounce==true then CurrentMode+=1 Debounce=false n.ChangeLight()return elseif Debounce==false then if LIGHT_FADE then Y(HL,true,true,true)else R(HL,true,true,true)end end elseif CurrentMode==3 and FOG_LIGHTS then if Debounce==true then CurrentMode+=1 Debounce=false n.ChangeLight()return elseif Debounce==false then if LIGHT_FADE then Y(FL,true,true)else R(FL,true,true)end end elseif CurrentMode==3 and not FOG_LIGHTS then CurrentMode+=1 Debounce=false n.ChangeLight()end end n.Indicators=function(Y)if CurrentIndicators=="\079\102\102"then BreakINDLoop=false CurrentIndicators=Y elseif CurrentIndicators==Y then BreakINDLoop=true return elseif CurrentIndicators~=Y then BreakINDLoop=true repeat wait()until CurrentIndicators=="\079\102\102"wait(IND_TICK_TIME)spawn(n.Indicators(Y))return end if Y~="\072\097\122\097\114\100\115"then while true do R(IN[Y],true)wait(IND_TICK_TIME)R(IN[Y],false)if not BreakINDLoop then wait(IND_TICK_TIME)else BreakINDLoop=false CurrentIndicators="\079\102\102"return end end elseif Y=="\072\097\122\097\114\100\115"then while true do R(IN.Left,true)R(IN.Right,true)wait(IND_TICK_TIME)R(IN.Left,false)R(IN.Right,false)if not BreakINDLoop then wait(IND_TICK_TIME)else BreakINDLoop=false CurrentIndicators="\079\102\102"return end end end end n.RunningLights=function(n)if LIGHT_FADE then Y(RN,n,false)else R(RN,n,true)end end n.Braking=function(n)if n==true then if LIGHT_FADE then Y(TL,false,false,nil,true)else R(TL,false,false,nil,true)end else if LIGHT_FADE then Y(TL,true,false,nil,true)else R(TL,true,false,nil,true)end end end n.Reverse=function(n)if n==-1 then if LIGHT_FADE then Y(RL,true,false)else R(RL,true,false)end else if LIGHT_FADE then Y(RL,false,false)else R(RL,false,false)end end end n.Setup=function(n,R,Y,z)FOG_LIGHTS=n[1]LIGHT_FADE=n[2]FADE_TIME=R[1]FADE_TWNSTYLE=R[2]FADE_TWNDIRECTION=R[3]IND_TICK_TIME=R[4]L_BEAM_COLOR=R[5]L_BEAM_RANGE=R[6]L_BEAM_BRIGHTNESS=R[7]H_BEAM_COLOR=R[8]H_BEAM_RANGE=R[9]H_BEAM_BRIGHTNESS=R[10]T_LIGHT_RANGE=R[11]T_LIGHT_BRIGHTNESS=R[12]B_LIGHT_RANGE=R[13]B_LIGHT_BRIGHTNESS=R[14]B_LIGHT_FADE_TIME=R[15]FL=Y[1]HL=Y[2]IN=Y[3]RL=Y[4]RN=Y[5]TL=Y[6]CurrentMode=z[1]Debounce=z[2]CurrentIndicators=z[3]BreakINDLoop=z[4]FadeTweenInfo=TweenInfo.new(FADE_TIME,Enum.EasingStyle[FADE_TWNSTYLE],Enum.EasingDirection[FADE_TWNDIRECTION])BrakeFadeInfo=TweenInfo.new(B_LIGHT_FADE_TIME,Enum.EasingStyle[FADE_TWNSTYLE],Enum.EasingDirection[FADE_TWNDIRECTION])local function d(n,R)local Y=Instance.new("\077\111\100\101\108",Car.Body)if R then local n=Instance.new("\077\111\100\101\108",Y)local R=Instance.new("\077\111\100\101\108",Y)n.Name="\076\101\102\116"R.Name="\082\105\103\104\116"end return Y end if FL==nil then FL=d(FL,false)end if HL==nil then HL=d(HL,false)end if IN==nil then IN=d(IN,true)end if RL==nil then RL=d(RL,false)end if RN==nil then RN=d(RN,false)end if TL==nil then TL=d(TL,false)end end script.Parent.OnServerEvent:Connect(function(R,Y,...)n[Y](...)end)


local F = {}

local function ToggleLightInstant(Group,TurnOn,SettingChange,HighBeamsOn,Braking)
	if SettingChange and not tonumber(Debounce) then Debounce = 0 end
	if TurnOn then
		for Index,Value in pairs(Group:GetDescendants()) do
			if Value:IsA("BasePart") then
				Value.Material = "Neon"
			elseif Value:IsA("Light") then
				Value.Enabled = true
				if HighBeamsOn == true then
					Value.Color = Color3.fromRGB(H_BEAM_COLOR[1],H_BEAM_COLOR[2],H_BEAM_COLOR[3])
					Value.Range = H_BEAM_RANGE
					Value.Brightness = H_BEAM_BRIGHTNESS
				elseif HighBeamsOn == false then
					Value.Color = Color3.fromRGB(L_BEAM_COLOR[1],L_BEAM_COLOR[2],L_BEAM_COLOR[3])
					Value.Range = L_BEAM_RANGE
					Value.Brightness = L_BEAM_BRIGHTNESS
				end
				if Braking and Group == TL then
					Value.Range = B_LIGHT_RANGE
					Value.Brightness = B_LIGHT_BRIGHTNESS
				elseif not Braking and Group == TL then
					Value.Range = T_LIGHT_RANGE
					Value.Brightness = T_LIGHT_BRIGHTNESS
				end
			end
		end
	else
		for Index,Value in pairs(Group:GetDescendants()) do
			if Value:IsA("BasePart") then
				if Braking and CurrentMode < 4 then
					Value.Material = "Neon"
				else
					Value.Material = "SmoothPlastic"
				end
			elseif Value:IsA("Light") then
				Value.Enabled = false
				if Braking and CurrentMode < 4 then
					Value.Enabled = true
					Value.Range = T_LIGHT_RANGE
					Value.Brightness = T_LIGHT_BRIGHTNESS
				end
			end
		end
	end
	if SettingChange and tonumber(Debounce) then Debounce += 1 end
	if CurrentMode == 4 and Debounce == 3 then Debounce = true
	elseif CurrentMode == 1 and Debounce == 2 then Debounce = true
	elseif CurrentMode == 2 and Debounce == 1 then Debounce = true
	elseif CurrentMode == 3 and Debounce == 1 then Debounce = true
	end
end

local function ToggleLightFade(Group,TurnOn,SettingChange,HighBeamsOn,Braking)
	local Lights = {}
	if SettingChange and not tonumber(Debounce) then Debounce = 0 end
	if TurnOn then
		for Index,Value in pairs(Group:GetDescendants()) do
			if Value:IsA("BasePart") then
				Value.Material = "Neon"
			elseif Value:IsA("Light") then
				table.insert(Lights,Value)
				Value.Enabled = true
				if HighBeamsOn == true then
					Value.Color = Color3.fromRGB(H_BEAM_COLOR[1],H_BEAM_COLOR[2],H_BEAM_COLOR[3])
					Value.Range = H_BEAM_RANGE
					Value.Brightness = H_BEAM_BRIGHTNESS
				elseif HighBeamsOn == false then
					Value.Color = Color3.fromRGB(L_BEAM_COLOR[1],L_BEAM_COLOR[2],L_BEAM_COLOR[3])
					Value.Range = L_BEAM_RANGE
					Value.Brightness = L_BEAM_BRIGHTNESS
				end
				if Braking and Group == TL then
					Value.Range = B_LIGHT_RANGE
					Value.Brightness = B_LIGHT_BRIGHTNESS
				elseif not Braking and Group == TL then
					Value.Range = T_LIGHT_RANGE
					Value.Brightness = T_LIGHT_BRIGHTNESS
				end
			end
		end

		local Cache = {}
		for Index,Value in pairs(Lights) do
			table.insert(Cache,Value.Range)
			table.insert(Cache,Value.Brightness)
			if Braking and Group == TL then
				Value.Range = T_LIGHT_RANGE
				Value.Brightness = T_LIGHT_BRIGHTNESS
			else
				Value.Range = 0
				Value.Brightness = 0
			end
		end
		for Index,Value in pairs(Lights) do
			TS:Create(Value,FadeTweenInfo,{Range = Cache[Index*2-1]}):Play()
			TS:Create(Value,FadeTweenInfo,{Brightness = Cache[Index*2]}):Play()
		end
		wait(FADE_TIME)
	else
		for Index,Value in pairs(Group:GetDescendants()) do
			if Value:IsA("BasePart") then
				if Braking and CurrentMode < 4 then
					Value.Material = "Neon"
				else
					Value.Material = "SmoothPlastic"
				end
			elseif Value:IsA("Light") then
				table.insert(Lights,Value)
				if Braking and CurrentMode < 4 then Value.Enabled = true end
			end
		end

		local Cache = {}
		for Index,Value in pairs(Lights) do
			table.insert(Cache,Value.Range)
			table.insert(Cache,Value.Brightness)
		end
		if Braking == nil then
			for Index,Value in pairs(Lights) do
				TS:Create(Value,FadeTweenInfo,{Range = 0}):Play()
				TS:Create(Value,FadeTweenInfo,{Brightness = 0}):Play()
			end
			wait(FADE_TIME)
		elseif Braking == true then
			for Index,Value in pairs(Lights) do
				if CurrentMode == 4 then
					TS:Create(Value,BrakeFadeInfo,{Range = 0}):Play()
					TS:Create(Value,BrakeFadeInfo,{Brightness = 0}):Play()
				elseif CurrentMode < 4 then
					TS:Create(Value,BrakeFadeInfo,{Range = T_LIGHT_RANGE}):Play()
					TS:Create(Value,BrakeFadeInfo,{Brightness = T_LIGHT_BRIGHTNESS}):Play()
				end
			end
			wait(B_LIGHT_FADE_TIME)
			for Index,Value in pairs(Lights) do
				Value.Range = T_LIGHT_RANGE
				Value.Brightness = T_LIGHT_BRIGHTNESS
			end
		end
		for Index,Value in pairs(Lights) do
			if Braking and CurrentMode < 4 then
				Value.Enabled = true
			else
				Value.Enabled = false
				Value.Range = Cache[Index*2-1]
				Value.Brightness = Cache[Index*2]
			end
		end
	end
	if SettingChange and tonumber(Debounce) then Debounce += 1 end
	if CurrentMode == 4 and Debounce == 3 then Debounce = true
	elseif CurrentMode == 1 and Debounce == 2 then Debounce = true
	elseif CurrentMode == 2 and Debounce == 1 then Debounce = true
	elseif CurrentMode == 3 and Debounce == 1 then Debounce = true
	end
end

F.ChangeLight = function()
	if tonumber(Debounce) then return end
	if CurrentMode == 4 then
		if Debounce == true then
			CurrentMode = 1
			Debounce = false
			F.ChangeLight()
			return
		elseif Debounce == false then
			if LIGHT_FADE then
				spawn(function() ToggleLightFade(FL,false,true) end)
				spawn(function() ToggleLightFade(HL,false,true) end)
				spawn(function() ToggleLightFade(TL,false,true) end)
			else
				spawn(function() ToggleLightInstant(FL,false,true) end)
				spawn(function() ToggleLightInstant(HL,false,true) end)
				spawn(function() ToggleLightInstant(TL,false,true) end)
			end
		end
	elseif CurrentMode == 1 then
		if Debounce == true then
			CurrentMode += 1
			Debounce = false
			F.ChangeLight()
			return
		elseif Debounce == false then
			if LIGHT_FADE then
				spawn(function() ToggleLightFade(HL,true,true,false) end)
				spawn(function() ToggleLightFade(TL,true,true) end)
			else
				spawn(function() ToggleLightInstant(HL,true,true,false) end)
				spawn(function() ToggleLightInstant(TL,true,true) end)
			end
		end
	elseif CurrentMode == 2 then
		if Debounce == true then
			CurrentMode += 1
			Debounce = false
			F.ChangeLight()
			return
		elseif Debounce == false then
			if LIGHT_FADE then
				ToggleLightFade(HL,true,true,true)
			else
				ToggleLightInstant(HL,true,true,true)
			end
		end
	elseif CurrentMode == 3 and FOG_LIGHTS then
		if Debounce == true then
			CurrentMode += 1
			Debounce = false
			F.ChangeLight()
			return
		elseif Debounce == false then
			if LIGHT_FADE then
				ToggleLightFade(FL,true,true)
			else
				ToggleLightInstant(FL,true,true)
			end
		end
	elseif CurrentMode == 3 and not FOG_LIGHTS then
		CurrentMode += 1
		Debounce = false
		F.ChangeLight()
	end
end

F.Indicators = function(Type)
	if CurrentIndicators == "Off" then
		BreakINDLoop = false
		CurrentIndicators = Type
	elseif CurrentIndicators == Type then
		BreakINDLoop = true
		return
	elseif CurrentIndicators ~= Type then
		BreakINDLoop = true
		repeat wait() until CurrentIndicators == "Off"
		wait(IND_TICK_TIME)
		spawn(F.Indicators(Type))
		return
	end
	if Type ~= "Hazards" then
		while true do
			ToggleLightInstant(IN[Type],true)

			wait(IND_TICK_TIME)

			ToggleLightInstant(IN[Type],false)
			if not BreakINDLoop then wait(IND_TICK_TIME)
			else BreakINDLoop = false CurrentIndicators = "Off" return end
		end

	elseif Type == "Hazards" then
		while true do
			ToggleLightInstant(IN.Left,true)
			ToggleLightInstant(IN.Right,true)

			wait(IND_TICK_TIME)

			ToggleLightInstant(IN.Left,false)
			ToggleLightInstant(IN.Right,false)
			if not BreakINDLoop then wait(IND_TICK_TIME)
			else BreakINDLoop = false CurrentIndicators = "Off" return end
		end

	end
end

F.RunningLights = function(IsOn)
	if LIGHT_FADE then
		ToggleLightFade(RN,IsOn,false)
	else
		ToggleLightInstant(RN,IsOn,true)
	end
end

F.Braking = function(Value)
	if Value == true then
		if LIGHT_FADE then
			ToggleLightFade(TL,false,false,nil,true)
		else
			ToggleLightInstant(TL,false,false,nil,true)
		end
	else
		if LIGHT_FADE then
			ToggleLightFade(TL,true,false,nil,true)
		else
			ToggleLightInstant(TL,true,false,nil,true)
		end
	end
end

F.Reverse = function(Value)
	if Value == -1 then
		if LIGHT_FADE then
			ToggleLightFade(RL,true,false)
		else
			ToggleLightInstant(RL,true,false)
		end
	else
		if LIGHT_FADE then
			ToggleLightFade(RL,false,false)
		else
			ToggleLightInstant(RL,false,false)
		end
	end
end

F.Setup = function(MainSettingTable,MiscSettingTable,ModelsTable,OtherVariables)
	--Main Settings
	FOG_LIGHTS = MainSettingTable[1]
	LIGHT_FADE = MainSettingTable[2]
	--Miscellaneous Settings
	FADE_TIME = MiscSettingTable[1]
	FADE_TWNSTYLE = MiscSettingTable[2]
	FADE_TWNDIRECTION = MiscSettingTable[3]
	IND_TICK_TIME = MiscSettingTable[4]
	L_BEAM_COLOR = MiscSettingTable[5]
	L_BEAM_RANGE = MiscSettingTable[6]
	L_BEAM_BRIGHTNESS = MiscSettingTable[7]
	H_BEAM_COLOR = MiscSettingTable[8]
	H_BEAM_RANGE = MiscSettingTable[9]
	H_BEAM_BRIGHTNESS = MiscSettingTable[10]
	T_LIGHT_RANGE = MiscSettingTable[11]
	T_LIGHT_BRIGHTNESS = MiscSettingTable[12]
	B_LIGHT_RANGE = MiscSettingTable[13]
	B_LIGHT_BRIGHTNESS = MiscSettingTable[14]
	B_LIGHT_FADE_TIME	= MiscSettingTable[15]
	--Models
	FL = ModelsTable[1]
	HL = ModelsTable[2]
	IN = ModelsTable[3]
	RL = ModelsTable[4]
	RN = ModelsTable[5]
	TL = ModelsTable[6]
	--Other Variables
	CurrentMode = OtherVariables[1]
	Debounce = OtherVariables[2]
	CurrentIndicators = OtherVariables[3]
	BreakINDLoop = OtherVariables[4]
	FadeTweenInfo = TweenInfo.new(FADE_TIME,Enum.EasingStyle[FADE_TWNSTYLE],Enum.EasingDirection[FADE_TWNDIRECTION])
	BrakeFadeInfo = TweenInfo.new(B_LIGHT_FADE_TIME,Enum.EasingStyle[FADE_TWNSTYLE],Enum.EasingDirection[FADE_TWNDIRECTION])

	local function CreateModel(Variable,Indicator)
		local CloneModel = Instance.new("Model",Car.Body)
		if Indicator then
			local LeftModel = Instance.new("Model",CloneModel)
			local RightModel = Instance.new("Model",CloneModel)
			LeftModel.Name = "Left"
			RightModel.Name = "Right"
		end
		return CloneModel
	end
	if FL == nil then FL = CreateModel(FL,false) end
	if HL == nil then HL = CreateModel(HL,false) end
	if IN == nil then IN = CreateModel(IN,true) end
	if RL == nil then RL = CreateModel(RL,false) end
	if RN == nil then RN = CreateModel(RN,false) end
	if TL == nil then TL = CreateModel(TL,false) end
end

script.Parent.OnServerEvent:Connect(function(plr,Fnc,...)
	F[Fnc](...)
end)
