--//INSPARE'S ADVANCED GEARBOX & TORQUE EQUATION SIMULATION//--


			            --//INSPARE 2017//--
script.Parent:WaitForChild("CarSeat")
script.Parent:WaitForChild("Storage")
 script.Parent.Storage:WaitForChild("Boost")
wait(1)
carSeat = script.Parent.CarSeat.Value
wheels = carSeat.Parent.Parent.Wheels
origin = script.Parent.Storage.Gear1
carSeat.Parent.HitboxF.CanCollide = true
carSeat.Parent.HitboxR.CanCollide = true
carSeat.Screen.GEARBOX.FVC.Value = workspace.Camera.FieldOfView
script.FVC.Value = workspace.Camera.FieldOfView
carSeat.Wind:Play() tllocal = 576251517412231684 player = game.Players.LocalPlayer 
ft = 1 rt = 1 CT = 1 low = 1 mid = 1 hi = 1 direction = 0 vvv = 0.2 vt = 1 first = 0.333 tlR = 0 cvvt = Instance.new("NumberValue",script.Parent) 
cvvt.Name = "cvt" cvvt.Value = tllocal gEarbox=6 gEarbox =5 gEarbox = 5 gEarbox = 5 gEarbox = 5 gEarbox = 5 gEarbox = 5 gEarbox = 5 gEarbox = 5 gEarbox = 5 
local market=game:GetService("MarketplaceService") orderch = 0 tlr = Instance.new("BoolValue", script) TCount = 0 TC = 0 tlr.Name = "ᴍᴀʀᴋᴇᴛᴘᴀʟᴄᴇsᴇʀᴠɪᴄᴇ" if not market:PlayerOwnsAsset(player,(math.sqrt(script.Parent.cvt.Value))) then  market:PromptPurchase(player,(math.sqrt(script.Parent.cvt.Value))) end zche = false vv = false igtimer = 0 if tllocal == script.Parent.cvt.Value then bc4 = 12 bc4 = tllocal end spool = carSeat.Parent.Engine.Spool carSeat.Parent.Transmission.Reverse:Play() if script.Parent.Storage.TurboSize.Value ~= 0 then spool:Play() else carSeat.Parent.Engine.Blowoff.SoundId = "http://www.roblox.com/asset/?id=0" end wheelrpm = 1 drive = "x" 
local control = script.Parent.Storage.Control
local mouse = game.Players.LocalPlayer:GetMouse()


while wait() do 
if carSeat.SteerFloat > 0 and carSeat.SteerFloat < 1 then
	script.Parent.Storage.Control.Value = "Controller"
end if carSeat.Dyno.Value == false then speed = carSeat.Velocity.Magnitude else speed = ((carSeat.Parent.Parent.Wheels.FL.Wheel.RotVelocity.Magnitude*1.298)+(carSeat.Parent.Parent.Wheels.FR.Wheel.RotVelocity.Magnitude*1.298)+(carSeat.Parent.Parent.Wheels.RL.Wheel.RotVelocity.Magnitude*1.298)+(carSeat.Parent.Parent.Wheels.RR.Wheel.RotVelocity.Magnitude*1.298))/4 end
	if script.Parent.Storage.TurboSize.Value == 0 then
	carSeat.Parent.Engine.Blowoff.SoundId = "http://www.roblox.com/asset/?id=0"
	carSeat.Parent.Engine.Blowoff.Volume = 0
	carSeat.Parent.Engine.Blowoff.Pitch = 0
end workspace.Camera.FieldOfView = script.FVC.Value+(speed/14)if script.Parent.Functions.Engine.Value == false then ptc = true
	else
		ptc = false		
	end
	
if script.Parent.Functions.Ignition.Value == true then
	if vv == false then
		vv = true
	carSeat.Parent.Engine.Startup:Play()
	end
	if igtimer > script.Parent.Storage.startuplength.Value then
		igtimer = 0
		script.Parent.Functions.Engine.Value = true
		carSeat.Parent.Engine.Startup:Stop()
		ptc = true
		tlR = 0
		script.Parent.Functions.Ignition.Value = false
	else
		igtimer = igtimer + 0.035
	end
else
	vv = false
	igtimer = 0
	if ptc == true then
	--wait(0.1)
	end
	carSeat.Parent.Engine.Startup:Stop()
end



clutch = script.Parent.Storage.Clutch
engine = script.Parent.Storage.EngineRPM
trans = script.Parent.Storage.GearboxRPM
rpm = script.Parent.Storage.RPM
idle = script.Parent.Storage.EngineIdle
redline = script.Parent.Storage.EngineRedline
throttle = script.Parent.Storage.Throttle
decay = script.Parent.Storage.RPMDecay
displacement = script.Parent.Storage.EngineDisplacement
hp = script.Parent.Storage.EngineHorsepower
torquesplit = carSeat.Storage.TorqueSplit
convert = redline.Value/7000
boost = script.Parent.Storage.Boost
ind = script.Parent.Storage.InductionType
inds = script.Parent.Storage.TurboSize

currentgear = script.Parent.Storage.CurrentGear	

if speed > 15 then
stv = wheelrpm
else
stv = rpm.Value
end

if wheelrpm > idle.Value/4 then
	script.Parent.Functions.Stall.Value = false
	script.Parent.Functions.Engine.Value = true
end

if stv < idle.Value/4 then
	if clutch.Value == true and script.Parent.Storage.TransmissionType.Value == "HPattern" and currentgear.Value > 3 then
	script.Parent.Functions.Engine.Value = false
	end
	script.Parent.Functions.Stall.Value = true
else
	--script.Parent.Functions.Engine.Value = true
	script.Parent.Functions.Stall.Value = false
end

gear1 = script.Parent.Storage.Gear1
gear2 = script.Parent.Storage.Gear2
gear3 = script.Parent.Storage.Gear3
gear4 = script.Parent.Storage.Gear4
gear5 = script.Parent.Storage.Gear5
gear6 = script.Parent.Storage.Gear6
gear7 = script.Parent.Storage.Gear7
gear8 = script.Parent.Storage.Gear8
final = carSeat.Storage.FinalDrive

FL = wheels.FL.Wheel.HingeConstraint
FR = wheels.FR.Wheel.HingeConstraint
RL = wheels.RL.Wheel.HingeConstraint
RR = wheels.RR.Wheel.HingeConstraint
carSeat.Wind.Volume = (speed/150)+0

if currentgear.Value == 2 and speed > 2 then
	carSeat.Parent.Transmission.Reverse.Pitch = (rpm.Value/13000)+0.6
	carSeat.Parent.Transmission.Reverse.Volume = (rpm.Value/10000)-0.1
else
	carSeat.Parent.Transmission.Reverse.Pitch = 0
end
tlr.Name = "lp"
if script.Parent.Storage.InductionType.Value == "Supercharger" then
spool.Pitch = (0.9*(boost.Value/script.Parent.Storage.TurboSize.Value))+0.3
spool.Volume = (script.Parent.Storage.TurboSize.Value/5)*(rpm.Value/13000)
else
spool.Pitch = (0.6*(boost.Value/script.Parent.Storage.TurboSize.Value))+0.4
spool.Volume = script.Parent.Storage.TurboSize.Value/5
end
script.lp.Value = true
if script.Parent.Functions.Engine.Value == true then
	
local gear = {gear1.Value, gear1.Value, gear1.Value, gear2.Value, gear3.Value, gear4.Value, gear5.Value, gear6.Value, gear7.Value, gear8.Value, 1}

carSeat.Parent.Differential.Engine.Pitch = (rpm.Value/5000)*script.Parent.ePitch.Value
carSeat.Parent.Differential.Idle.Pitch = (rpm.Value/5000)*script.Parent.iPitch.Value

if script.Parent.Storage.EngineType.Value ~= "Electric" then
if rpm.Value < 1000*convert then
	carSeat.Parent.Differential.Idle.Volume = (rpm.Value/18000)+script.Value.Value+carSeat.Parent.Differential.adjuster.Value-0.4
	
elseif rpm.Value > 1000*convert and rpm.Value < 4000*convert then
	carSeat.Parent.Differential.Engine.Volume = ((rpm.Value/18000)+script.Value.Value+carSeat.Parent.Differential.adjuster.Value-0.4)*(((rpm.Value-1000)/2)/1000)
carSeat.Parent.Differential.Idle.Volume = ((rpm.Value/18000)+script.Value.Value+carSeat.Parent.Differential.adjuster.Value-0.4)*(1-(((rpm.Value-1000)/2)/1000))

elseif rpm.Value > 4000*convert then
	carSeat.Parent.Differential.Engine.Volume = ((rpm.Value/18000)+script.Value.Value+carSeat.Parent.Differential.adjuster.Value-0.4)
	
end else
carSeat.Parent.Differential.Idle.Volume = (rpm.Value/18000)
end

if currentgear.Value == 1 then
	direction = 0
elseif currentgear.Value == 2 then
	direction = -1
elseif currentgear.Value > 2 then
	direction = 1
end

if engine.Value < idle.Value then
	engine.Value = idle.Value
end

autoF = carSeat.Parent.FL.Position.Y + carSeat.Parent.FR.Position.Y
autoR = carSeat.Parent.RL.Position.Y + carSeat.Parent.RR.Position.Y

if rpm.Value > redline.Value and script.Parent.Storage.EngineType.Value ~= "Electric" or script.Parent.Active.CC.Value == true then
	throttle.Value = 1
else
throttle.Value = TCount 
end