--Automatic Gauge Scaling
if autoscaling then
	local Drive={}
	if _Tune.Config == "FWD" or _Tune.Config == "AWD" then
		if car.Wheels:FindFirstChild("FL")~= nil then
			table.insert(Drive,car.Wheels.FL)
		end
		if car.Wheels:FindFirstChild("FR")~= nil then
			table.insert(Drive,car.Wheels.FR)
		end
		if car.Wheels:FindFirstChild("F")~= nil then
			table.insert(Drive,car.Wheels.F)
		end
	end
	if _Tune.Config == "RWD" or _Tune.Config == "AWD" then
		if car.Wheels:FindFirstChild("RL")~= nil then
			table.insert(Drive,car.Wheels.RL)
		end
		if car.Wheels:FindFirstChild("RR")~= nil then
			table.insert(Drive,car.Wheels.RR)
		end
		if car.Wheels:FindFirstChild("R")~= nil then
			table.insert(Drive,car.Wheels.R)
		end
	end

	local wDia = 0
	for i,v in pairs(Drive) do
		if v.Size.x>wDia then wDia = v.Size.x end
	end
	Drive = nil
	for i,v in pairs(UNITS) do
		v.maxSpeed = math.ceil(v.scaling*wDia*math.pi*_lRPM/60/_Tune.Ratios[#_Tune.Ratios]/_Tune.FinalDrive)
		v.spInc = math.max(math.ceil(v.maxSpeed/200)*20,20)
	end
end

for i=0,revEnd do
	local ln = script.Parent.A:Clone()
	ln.Name = i
	ln.Parent = script.Parent.Tac.G
	ln.Rotation = -90 + i * 270 / (revEnd)
	ln.Num.Text = i
	ln.Num.Rotation = -ln.Rotation
	if i*1000>=math.ceil(_lRPM/1000)*1000 then
		ln.Num.TextColor3 = Color3.new(1,0,0)
	end
	ln.Num.Visible=true
	ln.Visible=true
end

local lns = Instance.new("Frame",script.Parent.Spd)
lns.Name = "lns"
lns.BackgroundTransparency = 1
lns.BorderSizePixel = 0
lns.Size = UDim2.new(0,0,0,0)

for i,v in pairs(UNITS) do
	local lnn = Instance.new("Frame",script.Parent.Spd.G)
	lnn.BackgroundTransparency = 1
	lnn.BorderSizePixel = 0
	lnn.Size = UDim2.new(1,0,1,0)
	lnn.Name = v.units
	if i~= 1 then lnn.Visible=false end
	
	for i=0,v.maxSpeed,v.spInc do
		local ln = script.Parent.A:clone()
		ln.Parent = lnn
		ln.Name = i
		ln.Rotation = -90 + 270*(i/v.maxSpeed)
		ln.Num.Text = i
		ln.Num.TextSize = 14
		ln.Num.Rotation = -ln.Rotation
		ln.Num.Visible=true
		ln.Visible=true
	end
end

if 	script.Parent.Parent.IsOn.Value then
	script.Parent:TweenPosition(UDim2.new(0, 0, 0, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quad,1,true)
end

script.Parent.Parent.IsOn.Changed:connect(function()
	if script.Parent.Parent.IsOn.Value then
		script.Parent:TweenPosition(UDim2.new(0, 0, 0, 0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quad,1,true)
	end
end)

script.Parent.Parent.Values.RPM.Changed:connect(function()
	local r = script.Parent.Parent.Values.RPM
	script.Parent.Tac.G.Needle.Rotation = -90 + 270 * math.min(1,script.Parent.Parent.Values.RPM.Value / (revEnd*1000))
	
	inTac.Rotation = -30 + 240 * math.min(1,script.Parent.Parent.Values.RPM.Value / (revEnd*1000))
end)

script.Parent.Parent.Values.TCS.Changed:connect(function()
	if _Tune.TCSEnabled then
		if script.Parent.Parent.Values.TCS.Value then
			script.Parent.Clst.TCS.TextColor3 = Color3.new(1,170/255,0)
			script.Parent.Clst.TCS.TextStrokeColor3 = Color3.new(1,170/255,0)
			if script.Parent.Parent.Values.TCSActive.Value then
				wait()
				script.Parent.Clst.TCS.Visible = not script.Parent.Clst.TCS.Visible
			else
				wait()
				script.Parent.Clst.TCS.Visible = false
			end
		else
			script.Parent.Clst.TCS.Visible = true
			script.Parent.Clst.TCS.TextColor3 = Color3.new(1,0,0)
			script.Parent.Clst.TCS.TextStrokeColor3 = Color3.new(1,0,0)
		end
	else
		script.Parent.Clst.TCS.Visible = false
	end
end)

script.Parent.Parent.Values.TCSActive.Changed:connect(function()
	if _Tune.TCSEnabled then
		if script.Parent.Parent.Values.TCSActive.Value and script.Parent.Parent.Values.TCS.Value then
			wait()
			script.Parent.Clst.TCS.Visible = not script.Parent.Clst.TCS.Visible
		elseif not script.Parent.Parent.Values.TCS.Value then
			wait()
			script.Parent.Clst.TCS.Visible = true
		else
			wait()
			script.Parent.Clst.TCS.Visible = false
		end
	else
		script.Parent.Clst.TCS.Visible = false
	end
end)

script.Parent.Clst.TCS.Changed:connect(function()
	if _Tune.TCSEnabled then
		if script.Parent.Parent.Values.TCSActive.Value and script.Parent.Parent.Values.TCS.Value then
			wait()
			script.Parent.Clst.TCS.Visible = not script.Parent.Clst.TCS.Visible
		elseif not script.Parent.Parent.Values.TCS.Value then
			wait()
			script.Parent.Clst.TCS.Visible = true
		end
	else
		if script.Parent.Clst.TCS.Visible then
			script.Parent.Clst.TCS.Visible = false
		end
	end
end)

script.Parent.Parent.Values.ABS.Changed:connect(function()
	if _Tune.ABSEnabled then
		if script.Parent.Parent.Values.ABS.Value then
			script.Parent.Clst.ABS.TextColor3 = Color3.new(1,170/255,0)
			script.Parent.Clst.ABS.TextStrokeColor3 = Color3.new(1,170/255,0)
			if script.Parent.Parent.Values.ABSActive.Value then
				wait()
				script.Parent.Clst.ABS.Visible = not script.Parent.Clst.ABS.Visible
			else
				wait()
				script.Parent.Clst.ABS.Visible = false
			end
		else
			script.Parent.Clst.ABS.Visible = true
			script.Parent.Clst.ABS.TextColor3 = Color3.new(1,0,0)
			script.Parent.Clst.ABS.TextStrokeColor3 = Color3.new(1,0,0)
		end
	else
		script.Parent.Clst.ABS.Visible = false
	end
end)

script.Parent.Parent.Values.ABSActive.Changed:connect(function()
	if _Tune.ABSEnabled then
		if script.Parent.Parent.Values.ABSActive.Value and script.Parent.Parent.Values.ABS.Value then
			wait()
			script.Parent.Clst.ABS.Visible = not script.Parent.Clst.ABS.Visible
		elseif not script.Parent.Parent.Values.ABS.Value then
			wait()
			script.Parent.Clst.ABS.Visible = true
		else
			wait()
			script.Parent.Clst.ABS.Visible = false
		end
	else
		script.Parent.Clst.ABS.Visible = false
	end
end)

script.Parent.Clst.ABS.Changed:connect(function()
	if _Tune.ABSEnabled then
		if script.Parent.Parent.Values.ABSActive.Value and script.Parent.Parent.Values.ABS.Value then
			wait()
			script.Parent.Clst.ABS.Visible = not script.Parent.Clst.ABS.Visible
		elseif not script.Parent.Parent.Values.ABS.Value then
			wait()
			script.Parent.Clst.ABS.Visible = true
		end
	else
		if script.Parent.Clst.ABS.Visible then
			script.Parent.Clst.ABS.Visible = false
		end
	end
end)

script.Parent.Parent.Values.PBrake.Changed:connect(function()
	script.Parent.Clst.P.Visible = script.Parent.Parent.Values.PBrake.Value
end)

script.Parent.Parent.Values.TransmissionMode.Changed:connect(function()
	if script.Parent.Parent.Values.TransmissionMode.Value == "Auto" then
		script.Parent.Clst.TMode.Text = "A/T"
		script.Parent.Clst.TMode.TextColor3 = Color3.new(1,170/255,0)
	elseif script.Parent.Parent.Values.TransmissionMode.Value == "Semi" then
		script.Parent.Clst.TMode.Text = "S/T"
		script.Parent.Clst.TMode.TextColor3 = Color3.new(0, 170/255, 127/255) 
	else
		script.Parent.Clst.TMode.Text = "M/T"
		script.Parent.Clst.TMode.TextColor3 = Color3.new(1,85/255,.5)
	end
end)

script.Parent.Parent.Values.Velocity.Changed:connect(function(property)
	script.Parent.Spd.G.Needle.Rotation = -90 + 270 * math.min(1,UNITS[currentUnits].scaling*script.Parent.Parent.Values.Velocity.Value.Magnitude/UNITS[currentUnits].maxSpeed)
	script.Parent.Spd.G.Gear.Text = math.floor(UNITS[currentUnits].scaling*script.Parent.Parent.Values.Velocity.Value.Magnitude)
	inspd.Rotation = -30 + (240 / 160) * (math.abs(script.Parent.Parent.Values.Velocity.Value.Magnitude*((10/12) * (60/88))))
end)