--these are for button checks. It's kinda ugly so if you have a better way pm me


ha=false
hd=false
hs=false
hw=false
imgassets ={
	hazardoff="http://www.roblox.com/asset/?id=116525460",
	hazardon = "http://www.roblox.com/asset/?id=116525473",
	leftyoff = "http://www.roblox.com/asset/?id=116525490",
	leftyon = "http://www.roblox.com/asset/?id=116525499",
	rightyoff = "http://www.roblox.com/asset/?id=116525510",
	rightyon = "http://www.roblox.com/asset/?id=116525535",
	outoff="http://www.roblox.com/asset/?id=116525552",
	outon = "http://www.roblox.com/asset/?id=116525565",
	offoff = "http://www.roblox.com/asset/?id=116525585",
	offon = "http://www.roblox.com/asset/?id=116525604",
	onoff = "http://www.roblox.com/asset/?id=116539722",
	onon = "http://www.roblox.com/asset/?id=116539724",
	x4off = "http://www.roblox.com/asset/?id=116525613",
	x4on = "http://www.roblox.com/asset/?id=116525623",
	x2off = "http://www.roblox.com/asset/?id=116525638",
	x2on = "http://www.roblox.com/asset/?id=116525657",
	fastoff = "http://www.roblox.com/asset/?id=116525676",
	faston = "http://www.roblox.com/asset/?id=116525692",
	slowoff = "http://www.roblox.com/asset/?id=116525700",
	slowon = "http://www.roblox.com/asset/?id=116525707",
	lightsoff = "http://www.roblox.com/asset/?id=115931775",
	lightson = "http://www.roblox.com/asset/?id=115931779",
	lockoff = "http://www.roblox.com/asset/?id=116532096",
	lockon = "http://www.roblox.com/asset/?id=116532114",
	leftturn = "http://www.roblox.com/asset/?id=115931542",
	rightturn = "http://www.roblox.com/asset/?id=115931529",
	fltd = "http://www.roblox.com/asset/?id=116531501",
}
for _,i in pairs (imgassets) do
	Game:GetService("ContentProvider"):Preload(i)
end

if gear == 1 then
	script.Parent.dn.TextColor3 = Color3.new(.3,.3,.3)
	script.Parent.dn.Style = "RobloxButton"
end
if gear == maxgear then
	script.Parent.up.TextColor3 = Color3.new(.3,.3,.3)
	script.Parent.up.Style = "RobloxButton"
end

function gearup()--activated by GUI or pressing E
	if lock then return end
	if gear < maxgear then
		gear = gear+1
		watdo()
		script.Parent.Gear.Text = (gear.."/"..maxgear)
	end
	if gear == maxgear then
		script.Parent.up.TextColor3 = Color3.new(.3,.3,.3)
		script.Parent.up.Style = "RobloxButton"
		script.Parent.dn.TextColor3 = Color3.new(1,1,1)
		script.Parent.dn.Style = "RobloxButtonDefault"
	elseif gear == 1 then
		script.Parent.dn.TextColor3 = Color3.new(.3,.3,.3)
		script.Parent.dn.Style = "RobloxButton"
		script.Parent.up.TextColor3 = Color3.new(1,1,1)
		script.Parent.up.Style = "RobloxButtonDefault"
	else
		script.Parent.dn.TextColor3 = Color3.new(1,1,1)
		script.Parent.dn.Style = "RobloxButtonDefault"
		script.Parent.up.TextColor3 = Color3.new(1,1,1)
		script.Parent.up.Style = "RobloxButtonDefault"
	end
end
function geardown()--activated by GUI or pressing Q
	if lock then return end
	if gear > 1 then
		gear = gear-1
		watdo()
		script.Parent.Gear.Text = (gear.."/"..maxgear)
	end
	if gear == 1 then
		script.Parent.dn.TextColor3 = Color3.new(.3,.3,.3)
		script.Parent.dn.Style = "RobloxButton"
		script.Parent.up.TextColor3 = Color3.new(1,1,1)
		script.Parent.up.Style = "RobloxButtonDefault"
	elseif gear == maxgear then
		script.Parent.up.TextColor3 = Color3.new(.3,.3,.3)
		script.Parent.up.Style = "RobloxButton"
		script.Parent.dn.TextColor3 = Color3.new(1,1,1)
		script.Parent.dn.Style = "RobloxButtonDefault"
	else
		script.Parent.dn.TextColor3 = Color3.new(1,1,1)
		script.Parent.dn.Style = "RobloxButtonDefault"
		script.Parent.up.TextColor3 = Color3.new(1,1,1)
		script.Parent.up.Style = "RobloxButtonDefault"
	end
end

script.Parent.up.MouseButton1Click:connect(gearup)
script.Parent.dn.MouseButton1Click:connect(geardown)

script.Parent.flipbutton.MouseButton1Click:connect(function()
	if not flipping then
		flipping = true
		local a = Instance.new("BodyPosition",seat)
		a.maxForce = Vector3.new(100000,10000000,100000)
		a.position = seat.Position + Vector3.new(0,10,0)
		local b = Instance.new("BodyGyro",seat)
		wait(3)
		a:Destroy()
		b:Destroy()
		flipping = false
	end
end)

function turn()
	if turndebounce == false then
		turndebounce = true
		wait(0.05)
		repeat
			templeft = turningleft
			tempright = turningright
			script.Parent.onsound:Play()
			if turningleft == true then
				script.Parent.leftturn.Visible = true
				for _,i in pairs (leftturn) do
					i.BrickColor = BrickColor.new("Deep orange")
				end
				for _,a in pairs (leftlight) do
					a.BrickColor = BrickColor.new("Really red")
				end
				for _,b in pairs (leftflash) do
					if lightson then
						b.Enabled = true
					end
				end
				for _,b in pairs (leftbrake) do
					b.Brightness = 2
				end
			end
			if turningright == true then
				script.Parent.rightturn.Visible = true
				for _,i in pairs (rightturn) do
					i.BrickColor = BrickColor.new("Deep orange")
				end
				for _,a in pairs (rightlight) do
					a.BrickColor = BrickColor.new("Really red")
				end
				for _,b in pairs (rightflash) do
					if lightson then
						b.Enabled = true
					end
				end
				for _,b in pairs (rightbrake) do
					b.Brightness = 2
				end
			end
			wait(0.4)
			script.Parent.offsound:Play()
			script.Parent.leftturn.Visible = false
			script.Parent.rightturn.Visible = false
			if templeft == true then
				for _,i in pairs (leftturn) do
					i.BrickColor = BrickColor.new("Neon orange")
				end
				for _,b in pairs (leftflash) do
					b.Enabled = false
				end
				for _,a in pairs (leftlight) do
					a.BrickColor = BrickColor.new("Bright red")
				end
				for _,b in pairs (leftbrake) do
					b.Brightness = 1
				end
			else
				if throttle > 0 then
					for _,a in pairs (leftlight) do
						a.BrickColor = BrickColor.new("Bright red")
					end
					for _,b in pairs (leftbrake) do
						b.Brightness = 1
					end
				else
					for _,a in pairs (leftlight) do
						a.BrickColor = BrickColor.new("Really red")
					end
					for _,b in pairs (leftbrake) do
						b.Brightness = 2
					end
				end
			end
			if tempright == true then
				for _,i in pairs (rightturn) do
					i.BrickColor = BrickColor.new("Neon orange")
				end
				for _,b in pairs (rightflash) do
					b.Enabled = false
				end
				for _,a in pairs (rightlight) do
					a.BrickColor = BrickColor.new("Bright red")
				end
				for _,b in pairs (rightbrake) do
					b.Brightness = 1
				end
			else
				if throttle > 0 then
					for _,a in pairs (rightlight) do
						a.BrickColor = BrickColor.new("Bright red")
					end
					for _,b in pairs (rightbrake) do
						b.Brightness = 1
					end
				else
					for _,a in pairs (rightlight) do
						a.BrickColor = BrickColor.new("Really red")
					end
					for _,b in pairs (rightbrake) do
						b.Brightness = 2
					end
				end
			end
			wait(0.35)
		until turningleft == false and turningright == false
		turndebounce = false
	end
end

seat.ChildRemoved:connect(function(it)
	if it:IsA("Weld") then
		if it.Part1.Parent == Player.Character then
			lock = true
			ha=false
			hd=false
			hs=false
			hw=false
			throttle = 0
			steer = 0
			watdo()
			script.Parent.close.Active = true
			script.Parent.close.Visible = true
			script.Parent.xlabel.Visible = true
		end
	end
end)
seat.ChildAdded:connect(function(it)
	if it:IsA("Weld") then
		if it.Part1.Parent == Player.Character then
			lock = false
			script.Parent.close.Active = false
			script.Parent.close.Visible = false
			script.Parent.xlabel.Visible = false
		end
	end
end)
function exiting()
	lock = true--when we close the gui stop everything
	steer = 0
	throttle = 0
	watdo()
	turningleft = false
	turningright = false
	script.Parent.flasher.Value = false
	script.Parent.siren.Value = false
	lightson = false
	Instance.new("IntValue",seat)
	for _,i in pairs (leftturn) do
		i.BrickColor = BrickColor.new("Neon orange")
	end
	for _,a in pairs (leftlight) do
		a.BrickColor = BrickColor.new("Really red")
	end
	for _,b in pairs (leftflash) do
		b.Enabled = false
	end
	for _,b in pairs (leftbrake) do
		b.Brightness = 2
	end
	for _,i in pairs (rightturn) do
		i.BrickColor = BrickColor.new("Neon orange")
	end
	for _,a in pairs (rightlight) do
		a.BrickColor = BrickColor.new("Really red")
	end
	for _,b in pairs (rightflash) do
		b.Enabled = false
	end
	for _,b in pairs (rightbrake) do
		b.Brightness = 2
	end
	script.Parent.Parent:Destroy()--destroy the 'Car' ScreenGui
end

function updatelights()
	for _,i in pairs (leftbrake) do
		i.Enabled = lightson
	end
	for _,i in pairs (rightbrake) do
		i.Enabled = lightson
	end
	for _,i in pairs (brakelight) do
		i.Enabled = lightson
	end
	for _,i in pairs (headlight) do
		i.Enabled = lightson
	end
	if lightson then
		script.Parent.lightimage.Image = imgassets.lightson
	else
		script.Parent.lightimage.Image = imgassets.lightsoff
	end
end

script.Parent.lights.MouseButton1Click:connect(function()
	if lock then return end
	lightson = not lightson
	updatelights()
end)

function destroycar()
	seat.Parent:Destroy()--destroy the car
end

script.Parent.close.MouseButton1Up:connect(exiting)
Player.Character.Humanoid.Died:connect(destroycar)

game.Players.PlayerRemoving:connect(function(Playeras)
	if Playeras.Name == Player.Name then
		destroycar()
	end
end)

for _, i in pairs (seat.Parent:GetChildren()) do--populate the tables for ease of modularity. You could have 100 left wheels if you wanted.
	if i.Name == "LeftWheel" then
		table.insert(left,i)
	elseif i.Name == "RightWheel" then
		table.insert(right,i)
	elseif i.Name == "Rearlight" then
		table.insert(rearlight,i)
	elseif i.Name == "Brakelight" then
		table.insert(brakelight,i.SpotLight)
	elseif i.Name == "rightturn" then
		table.insert(rightturn,i)
	elseif i.Name == "leftturn" then
		table.insert(leftturn,i)
	elseif i.Name == "leftflash" then
		table.insert(leftflash,i.SpotLight)
	elseif i.Name == "rightflash" then
		table.insert(rightflash,i.SpotLight)
	elseif i.Name == "leftlight" then
		table.insert(leftlight,i)
	elseif i.Name == "rightlight" then
		table.insert(rightlight,i)
	elseif i.Name == "Headlight" then
		table.insert(headlight,i.SpotLight)
	elseif i.Name == "leftbrake" then
		table.insert(leftbrake,i.SpotLight)
	elseif i.Name == "rightbrake" then
		table.insert(rightbrake,i.SpotLight)
	elseif i.Name == "revlight" then
		table.insert(revlight,i.SpotLight)
	end
end

for _,l in pairs (left) do
	l.BottomParamA = 0
	l.BottomParamB = 0
end
for _,r in pairs (right) do
	r.BottomParamA = 0
	r.BottomParamB = 0
end

function watdo()
	seat.Parent.LeftMotor.DesiredAngle = math.rad(throttle < 0 and 40* steer or 40*steer/gear^0.5)
	seat.Parent.RightMotor.DesiredAngle = math.rad(throttle < 0 and 40* steer or 40*steer/gear^0.5)
	for _,l in pairs (left) do--I do it this way so that it's not searching the model every time an input happens
		if throttle ~= -1 then
			l.BottomParamA = (.1/gear)
			l.BottomParamB = (.5*gear+steer*gear/30)*throttle
		else
			l.BottomParamA = -.01
			l.BottomParamB = -.5-steer/20
		end
	end
	for _,r in pairs (right) do
		if throttle ~= -1 then
			r.BottomParamA = -(.1/gear)
			r.BottomParamB = -(.5*gear-steer*gear/30)*throttle
		else
			r.BottomParamA = .01
			r.BottomParamB = .5-steer/20
		end
	end
	if throttle < 1 then
		for _,g in pairs (rearlight) do
			g.BrickColor = BrickColor.new("Really red")
		end
		for _,b in pairs (brakelight) do
			b.Brightness = 2
		end
		if turningleft == false then
			for _,a in pairs (leftlight) do
				a.BrickColor = BrickColor.new("Really red")
			end
			for _,b in pairs (leftbrake) do
				b.Brightness = 2
			end
		end
		if turningright == false then
			for _,a in pairs (rightlight) do
				a.BrickColor = BrickColor.new("Really red")
			end
			for _,b in pairs (rightbrake) do
				b.Brightness = 2
			end
		end
	else
		for _,g in pairs (rearlight) do
			g.BrickColor = BrickColor.new("Bright red")
		end
		for _,b in pairs (brakelight) do
			b.Brightness = 1
		end
		if turningleft == false then
			for _,a in pairs (leftlight) do
				a.BrickColor = BrickColor.new("Bright red")
			end
			for _,b in pairs (leftbrake) do
				b.Brightness = 1
			end
		end
		if turningright == false then
			for _,a in pairs (rightlight) do
				a.BrickColor = BrickColor.new("Bright red")
			end
			for _,b in pairs (rightbrake) do
				b.Brightness = 1
			end
		end
	end
	if throttle < 0 then
		for _,b in pairs (revlight) do
			if lightson then
				b.Enabled = true
			end
		end
	else
		for _,b in pairs (revlight) do
			b.Enabled = false
		end
	end	
end

Player:GetMouse().KeyDown:connect(function(key)--warning ugly button code
	if lock then return end
	key = string.upper(key)
	if not ha and key == "A" or key == string.char(20) and not ha then
		ha = true
		steer = steer-1
	end
	if not hd and key == "D" or key == string.char(19) and not hd then
		hd = true
		steer = steer+1
	end
	if not hw and key == "W" or key == string.char(17) and not hw then
		hw = true
		throttle = throttle+1
	end
	if not hs and key == "S" or key == string.char(18) and not hs then
		hs = true
		throttle = throttle-1
	end
	if key == "Z" then
		geardown()
	end
	if key == "X" then
		gearup()
	end
	if key == "Q" then
		turningleft = not turningleft
		turn()
	end
	if key == "E" then
		turningright = not turningright
		turn()
	end
	watdo()
end)
Player:GetMouse().KeyUp:connect(function(key)
	if lock then return end
	key = string.upper(key)
	if ha and key == "A" or key == string.char(20)and ha then
		steer = steer+1
		ha = false
	end
	if hd and key == "D" or key == string.char(19) and hd then
		steer = steer-1
		hd = false
	end
	if hw and key == "W" or key == string.char(17) and hw then
		throttle = throttle-1
		hw = false
	end
	if hs and key == "S" or key == string.char(18) and hs then
		throttle = throttle+1
		hs = false
	end
	if key == "" then
		--more keys if I need them
	end
	watdo()
end)