-------------------------
 
mouse.KeyDown:connect(function (key)
    key = string.lower(key)
	if key == "k" then --Camera controls
        if cam == ("car") then
			Camera.CameraSubject = player.Character.Humanoid
			Camera.CameraType = ("Custom")
			cam = ("freeplr")
		elseif cam == ("freeplr") then
			Camera.CameraSubject = player.Character.Humanoid
			Camera.CameraType = ("Attach")
			cam = ("lockplr")
        elseif cam == ("lockplr") then
			Camera.CameraSubject = carSeat
			Camera.CameraType = ("Custom")
			cam = ("car")
        end
	elseif key == "[" then -- volume down
		if carSeat.Parent.Body.MP.Sound.Volume >= 0 then
			handler:FireServer('volumedown', true)
		end
	elseif key == "]" then -- volume up
		if carSeat.Parent.Body.MP.Sound.Volume <= 10 then
			handler:FireServer('volumeup', true)
		end
	elseif key == "u" then -- music player
		if windows == false then 
			winfob.Visible = true
			windows = true
		else windows = false
			winfob.Visible = false
		end
	elseif key == "g" then -- sunroof
		handler:FireServer('sr')
	elseif key == "b" then -- fp modes
		if carSeat.Mode.Value == "Basic" then 
			carSeat.Mode.Value = "Normal"
		elseif carSeat.Mode.Value == "Normal" then
			carSeat.Mode.Value = "Sport"
		else
			carSeat.Mode.Value = "Basic"
		end
	elseif key:byte() == 48 then -- CC
		st = true
	end
end)

game:GetService("RunService").RenderStepped:connect(function()
	if cam == "lockplr" then
		local XLook=math.max(math.min(((mouse.X-(mouse.ViewSizeX/2))/300)^3,1),-1)
		local YLook=math.max(math.min(((mouse.Y-(mouse.ViewSizeY/2))/300)^3,1),-1)
		local LookOffset
		if player.Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
			LookOffset=player.Character.Head.CFrame:toWorldSpace(CFrame.new(0.01,.45,-.4)*CFrame.Angles(-YLook,-XLook,0))
		else
			LookOffset=player.Character.Head.CFrame:toWorldSpace(CFrame.new(0.01,.05,-.45)*CFrame.Angles(-YLook,-XLook,0))
		end
		Camera.CameraType="Scriptable"
		Camera.CameraSubject=player.Character.Humanoid
		Camera.CoordinateFrame=LookOffset
		player.CameraMaxZoomDistance=0
	else
		player.CameraMaxZoomDistance=400
	end
end)

mouse.KeyUp:connect(function (key)
    key = string.lower(key)
	if key:byte() == 48 then --Camera controls
		st = false
	end
end)

local mph = ((10/12) * (60/88))

function showspeed(a)
	if carSeat.CC.Value then
		HUB2.S.Visible = true
		HUB2.CC.Visible = false
		HUB2.S.Text = math.floor(carSeat.CCS.Value)
		HUB2.S.TextColor3 = a and Color3.new(0,1,0) or Color3.new(1,0,0)
		wait(2)
		HUB2.S.Visible = false
		HUB2.CC.Visible = true
	end
end

HUB2.Up.MouseButton1Click:connect(function()
	if st then
		carSeat.CCS.Value = carSeat.CCS.Value+5
	else
		carSeat.CCS.Value = carSeat.CCS.Value+1
	end
	showspeed(true)
end)

HUB2.UpW.MouseButton1Click:connect(function()
	handler:FireServer('wspd',1)
end)

HUB2.DnW.MouseButton1Click:connect(function()
	handler:FireServer('wspd',-1)
end)

HUB2.W.MouseButton1Click:connect(function()
	handler:FireServer('wws',carSeat.WS.Value,st)
	handler:FireServer('wwr',carSeat.WS.Value,st)
end)

HUB2.Dn.MouseButton1Click:connect(function()
	if st then
		carSeat.CCS.Value = carSeat.CCS.Value-5
	else
		carSeat.CCS.Value = carSeat.CCS.Value-1
	end
	showspeed(false)
end)

HUB2.SS.MouseButton1Click:connect(function()
	carSeat.SST.Value = not carSeat.SST.Value
end)

HUB2.CC.MouseButton1Click:connect(function()
	carSeat.CC.Value = not carSeat.CC.Value
	carSeat.CCS.Value = math.floor(carSeat.Velocity.Magnitude*mph)
	if st then
		carSeat.RCC.Value = not carSeat.RCC.Value
	end
	wait(.1)
	showspeed(true)
end)

HUB2.Limiter.MouseButton1Click:connect(function() --Ignition
	 if carSeat.IsOn.Value == false then
		carSeat.IsOn.Value = true
		carSeat.Parent.Body.Dash.D.G.Enabled = true
		carSeat.Parent.Body.Dash.S.G.Enabled = true
		carSeat.Startup:Play()
		wait(1)
		carSeat.Chime:Play()
	else
		carSeat.IsOn.Value = false
		carSeat.Parent.Body.Dash.D.G.Enabled = false
		carSeat.Parent.Body.Dash.S.G.Enabled = false
	end
end)

winfob.mg.Play.MouseButton1Click:connect(function() --Play the next song
	handler:FireServer('updateSong', winfob.mg.Input.Text)
end)

winfob.mg.Stop.MouseButton1Click:connect(function() --Play the next song
	handler:FireServer('pauseSong')
end)

carSeat.Indicator.Changed:connect(function()
	if carSeat.Indicator.Value == true then
		script.Parent.Indicator:Play()
	else
		script.Parent.Indicator2:Play()
	end
end)
if game.Workspace.FilteringEnabled == true then
	handler:FireServer('feON')
elseif game.Workspace.FilteringEnabled == false then
	handler:FireServer('feOFF')
end

carSeat.LI.Changed:connect(function()
	if carSeat.LI.Value == true then
		carSeat.Parent.Body.Dash.D.G.Info.Left.Visible = true
		carSeat.Parent.Body.Dash.D.G.OdoB.Left.Visible = true
		script.Parent.HUB.Left.Visible = true
	else
		carSeat.Parent.Body.Dash.D.G.Info.Left.Visible = false
		carSeat.Parent.Body.Dash.D.G.OdoB.Left.Visible = false
		script.Parent.HUB.Left.Visible = false
	end
end)

carSeat.RI.Changed:connect(function()
	if carSeat.RI.Value == true then
		carSeat.Parent.Body.Dash.D.G.Info.Right.Visible = true
		carSeat.Parent.Body.Dash.D.G.TacB.Right.Visible = true
		script.Parent.HUB.Right.Visible = true
	else
		carSeat.Parent.Body.Dash.D.G.Info.Right.Visible = false
		carSeat.Parent.Body.Dash.D.G.TacB.Right.Visible = false
		script.Parent.HUB.Right.Visible = false
	end
end)

for i,v in pairs(script.Parent:getChildren()) do
	if v:IsA('ImageButton') then
		v.MouseButton1Click:connect(function()
			if carSeat.Windows:FindFirstChild(v.Name) then
				local v = carSeat.Windows:FindFirstChild(v.Name)
				if v.Value == true then
					handler:FireServer('updateWindows', v.Name, false)
				else
					handler:FireServer('updateWindows', v.Name, true)
				end
			end
		end)
	end
end

function fmt(mins)
	local hr
	local hrs = math.floor(mins / 60)
	local mn = math.floor((mins)%60)
	local stm
	if mn < 10 then
		stm = "0"..(tostring(math.floor((mins)%60)))
	else stm = tostring(math.floor((mins)%60))
	end
	local pm = false
	if hrs >= 12 then
		pm = true
	else pm = false
	end
	if hrs%12 == 0 then
		hr = 12
	else hr = hrs%12
	end
	local stamp = (hr)..":"..(stm)..(pm and " PM" or " AM")
	return stamp
end

while wait() do
	carSeat.Parent.Body.Dash.D.G.Info.Time.Text = fmt(game.Lighting:GetMinutesAfterMidnight())
	carSeat.Parent.Body.Dash.S.G.HB.Time.Text = fmt(game.Lighting:GetMinutesAfterMidnight())
	
	carSeat.Parent.Body.Dash.D.G.Spd.Speed.Text = (math.floor(carSeat.Velocity.magnitude*((10/12) * (60/88))))
	if carSeat.SST.Value == false then
		HUB2.SS.ImageColor3 = Color3.new(0.85,0,0)
	else
		if carSeat.Tic.Value then
			HUB2.SS.ImageColor3 = Color3.new(0,1,0)
		else
			HUB2.SS.ImageColor3 = Color3.new(0,0,0)
		end
	end
	
	carSeat.Parent.Body.Dash.S.G.HB.S.Visible = (carSeat.Mode.Value == "Sport")
	carSeat.Parent.Body.Dash.D.G.SpdB.Visible = (carSeat.Mode.Value == "Basic")
	carSeat.Parent.Body.Dash.D.G.OdoB.Visible = (carSeat.Mode.Value == "Basic")
	carSeat.Parent.Body.Dash.D.G.TacB.Visible = (carSeat.Mode.Value == "Basic")
	
	carSeat.Parent.Body.Dash.D.G.Spd.Visible = (carSeat.Mode.Value ~= "Basic")
	carSeat.Parent.Body.Dash.D.G.Info.Visible = (carSeat.Mode.Value ~= "Basic")
	carSeat.Parent.Body.Dash.D.G.Tac.Visible = (carSeat.Mode.Value ~= "Basic")
	carSeat.Parent.Body.Dash.D.G.h.Visible = (carSeat.Mode.Value ~= "Basic")
	
	if game.Lighting:GetMinutesAfterMidnight() < 60*6 or game.Lighting:GetMinutesAfterMidnight() > 60*17.5 then
		carSeat.Parent.Body.Dash.S.G.HB.ImageColor3 = Color3.fromRGB(0, 42, 63)
	else
		carSeat.Parent.Body.Dash.S.G.HB.ImageColor3 = Color3.fromRGB(0, 126, 189)
	end
	
	if carSeat.Stations.mood.Value > 0 then
		winfob.m2.Current.Text = "Preset #"..carSeat.Stations.mood.Value
	else
		winfob.m2.Current.Text = ""
	end