local D={TireWearOn=true;FWearSpeed=.4;FTargetFriction=.7,FMinFriction=.1;RWearSpeed=.4,RTargetFriction=.7,RMinFriction=.1;TCSOffRatio=.33333333333333,WheelLockRatio=.5,WheelspinRatio=.90909090909091;FFrictionWeight=1;RFrictionWeight=1;FLgcyFrWeight=10,RLgcyFrWeight=10;FElasticity=.5;RElasticity=.5;FLgcyElasticity=0,RLgcyElasticity=0;FElastWeight=1;RElastWeight=1,FLgcyElWeight=10,RLgcyElWeight=10,RegenSpeed=3.6}local q=script.Parent.Parent.Car.Value local u=require(q["\065-\067\104\097\115\115\105\115 \084\117\110\101"])local n=script.Parent.Parent:WaitForChild("\086\097\108\117\101\115")local B=u.FWheelDensity local y=u.RWheelDensity local X=D.FFrictionWeight local Z=D.RFrictionWeight local e=D.FElasticity local g=D.RElasticity local R=D.FElastWeight local m=D.RElastWeight if not workspace:PGSIsEnabled()then B=u.FWLgcyDensity y=u.RWLgcyDensity X=D.FLgcyFrWeight Z=D.FLgcyFrWeight e=D.FLgcyElasticity g=D.RLgcyElasticity R=D.FLgcyElWeight m=D.RLgcyElWeight end local O={}for q,n in pairs(q.Wheels:GetChildren())do local l={}l.wheel=n local f l.x=0 if n.Name=="\070\076"or n.Name=="\070\082"or n.Name=="\070"then f=(12-math.abs(u.FCamber))/15 l.x=B l.BaseHeat=D.FTargetFriction-D.FMinFriction l.WearSpeed=D.FWearSpeed l.fWeight=X l.elast=e l.eWeight=R else f=(12-math.abs(u.RCamber))/15 l.x=y l.BaseHeat=D.RTargetFriction-D.RMinFriction l.WearSpeed=D.RWearSpeed l.fWeight=Z l.elast=g l.eWeight=m end l.Heat=l.BaseHeat l.stress=0 table.insert(O,l)end q.DriveSeat.ChildRemoved:connect(function(D)if D.Name=="\083\101\097\116\087\101\108\100"then local D=q:FindFirstChild("\087\101\097\114\068\097\116\097")if q:FindFirstChild("\087\101\097\114\068\097\116\097")==nil then D=script.Parent.WearData:Clone()D.Parent=q end for q,u in pairs(O)do D[u.wheel.Name].Value=u.Heat end D.STime.Value=tick()end end)while wait()do for B,y in pairs(O)do local X=q.DriveSeat.Velocity.Magnitude local Z=y.wheel.RotVelocity.Magnitude local e=0 local g=.000126 local R=(X/1.298)*(2.6/y.wheel.Size.Y)local m=math.abs(Z-R)if D.TireWearOn then if X<4 then y.Heat=math.min(y.Heat+D.RegenSpeed/10000,y.BaseHeat)elseif m>1 then y.Heat=y.Heat-((m*g)*y.WearSpeed)/28 elseif y.Heat>=y.BaseHeat then y.Heat=y.BaseHeat end end if y.wheel.Name=="\070\076"or y.wheel.Name=="\070\082"or y.wheel.Name=="\070"then e=D.FMinFriction+y.Heat g=(g-.0001188*n.Brake.Value)*(1-math.abs(n.SteerC.Value))+1.26e-06*math.abs(n.SteerC.Value)else e=D.RMinFriction+y.Heat end if math.ceil(((Z/.774)/X)*100)<8 then y.wheel.CustomPhysicalProperties=PhysicalProperties.new(y.x,e*D.WheelLockRatio,y.elast,y.fWeight,y.eWeight)y.Heat=math.max(y.Heat,0)elseif u.TCSEnabled and(n.TCS.Value==false and math.ceil(((Z/.774)/X)*100)>80)then y.wheel.CustomPhysicalProperties=PhysicalProperties.new(y.x,e*D.TCSOffRatio,y.elast,y.fWeight,y.eWeight)y.Heat=math.max(y.Heat,0)elseif math.ceil(((Z/.774)/X)*100)>130 then y.wheel.CustomPhysicalProperties=PhysicalProperties.new(y.x,e*D.WheelspinRatio,y.elast,y.fWeight,y.eWeight)y.Heat=math.max(y.Heat,0)else y.wheel.CustomPhysicalProperties=PhysicalProperties.new(y.x,e,y.elast,y.fWeight,y.eWeight)y.Heat=math.min(y.Heat,y.BaseHeat)end local O=math.abs((((m+R)/.774)*.774-R)/15)if O>.05 and O>y.stress then y.stress=math.min(y.stress+.03,1)else y.stress=math.max(y.stress-.03,O)end local l=script.Parent.Tires[y.wheel.Name]l.First.Second.Image.ImageColor3=Color3.new(math.min(y.stress*2,1),1-y.stress,0)l.First.Position=UDim2.new(0,0,1-y.Heat/y.BaseHeat,0)l.First.Second.Position=UDim2.new(0,0,y.Heat/y.BaseHeat,0)end end

local _WHEELTUNE = {
  TireWearOn = true,
  FWearSpeed = 0.4,
  FTargetFriction = 0.7,
  FMinFriction = 0.1,
  RWearSpeed = 0.4,
  RTargetFriction = 0.7,
  RMinFriction = 0.1,
  TCSOffRatio = 0.3333333333333333,
  WheelLockRatio = 0.5,
  WheelspinRatio = 0.9090909090909091,
  FFrictionWeight = 1,
  RFrictionWeight = 1,
  FLgcyFrWeight = 10,
  RLgcyFrWeight = 10,
  FElasticity = 0.5,
  RElasticity = 0.5,
  FLgcyElasticity = 0,
  RLgcyElasticity = 0,
  FElastWeight = 1,
  RElastWeight = 1,
  FLgcyElWeight = 10,
  RLgcyElWeight = 10,
  RegenSpeed = 3.6
}
local car = script.Parent.Parent.Car.Value
local _Tune = require(car["A-Chassis Tune"])
local cValues = script.Parent.Parent:WaitForChild("Values")
local fDensity = _Tune.FWheelDensity
local rDensity = _Tune.RWheelDensity
local fFwght = _WHEELTUNE.FFrictionWeight
local rFwght = _WHEELTUNE.RFrictionWeight
local fElast = _WHEELTUNE.FElasticity
local rElast = _WHEELTUNE.RElasticity
local fEwght = _WHEELTUNE.FElastWeight
local rEwght = _WHEELTUNE.RElastWeight
if not workspace:PGSIsEnabled() then
  fDensity = _Tune.FWLgcyDensity
  rDensity = _Tune.RWLgcyDensity
  fFwght = _WHEELTUNE.FLgcyFrWeight
  rFwght = _WHEELTUNE.FLgcyFrWeight
  fElast = _WHEELTUNE.FLgcyElasticity
  rElast = _WHEELTUNE.RLgcyElasticity
  fEwght = _WHEELTUNE.FLgcyElWeight
  rEwght = _WHEELTUNE.RLgcyElWeight
end
local Wheels = {}
for i, v in pairs(car.Wheels:GetChildren()) do
  local w = {}
  w.wheel = v
  local ca
  w.x = 0
  if v.Name == "FL" or v.Name == "FR" or v.Name == "F" then
    ca = (12 - math.abs(_Tune.FCamber)) / 15
    w.x = fDensity
    w.BaseHeat = _WHEELTUNE.FTargetFriction - _WHEELTUNE.FMinFriction
    w.WearSpeed = _WHEELTUNE.FWearSpeed
    w.fWeight = fFwght
    w.elast = fElast
    w.eWeight = fEwght
  else
    ca = (12 - math.abs(_Tune.RCamber)) / 15
    w.x = rDensity
    w.BaseHeat = _WHEELTUNE.RTargetFriction - _WHEELTUNE.RMinFriction
    w.WearSpeed = _WHEELTUNE.RWearSpeed
    w.fWeight = rFwght
    w.elast = rElast
    w.eWeight = rEwght
  end
  w.Heat = w.BaseHeat
  w.stress = 0
  table.insert(Wheels, w)
end
car.DriveSeat.ChildRemoved:connect(function(child)
  if child.Name == "SeatWeld" then
    local wD = car:FindFirstChild("WearData")
    if car:FindFirstChild("WearData") == nil then
      wD = script.Parent.WearData:Clone()
      wD.Parent = car
    end
    for i, v in pairs(Wheels) do
      wD[v.wheel.Name].Value = v.Heat
    end
    wD.STime.Value = tick()
  end
end)
while wait() do
  for i, v in pairs(Wheels) do
    local speed = car.DriveSeat.Velocity.Magnitude
    local wheel = v.wheel.RotVelocity.Magnitude
    local z = 0
    local deg = 1.26E-4
    local cspeed = speed / 1.298 * (2.6 / v.wheel.Size.Y)
    local wdif = math.abs(wheel - cspeed)
    if _WHEELTUNE.TireWearOn then
      if speed < 4 then
        v.Heat = math.min(v.Heat + _WHEELTUNE.RegenSpeed / 10000, v.BaseHeat)
      elseif wdif > 1 then
        v.Heat = v.Heat - wdif * deg * v.WearSpeed / 28
      elseif v.Heat >= v.BaseHeat then
        v.Heat = v.BaseHeat
      end
    end
    if v.wheel.Name == "FL" or v.wheel.Name == "FR" or v.wheel.Name == "F" then
      z = _WHEELTUNE.FMinFriction + v.Heat
      deg = (deg - 1.188E-4 * cValues.Brake.Value) * (1 - math.abs(cValues.SteerC.Value)) + 1.26E-6 * math.abs(cValues.SteerC.Value)
    else
      z = _WHEELTUNE.RMinFriction + v.Heat
    end
    if math.ceil(wheel / 0.774 / speed * 100) < 8 then
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z * _WHEELTUNE.WheelLockRatio, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.max(v.Heat, 0)
    elseif _Tune.TCSEnabled and cValues.TCS.Value == false and math.ceil(wheel / 0.774 / speed * 100) > 80 then
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z * _WHEELTUNE.TCSOffRatio, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.max(v.Heat, 0)
    elseif math.ceil(wheel / 0.774 / speed * 100) > 130 then
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z * _WHEELTUNE.WheelspinRatio, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.max(v.Heat, 0)
    else
      v.wheel.CustomPhysicalProperties = PhysicalProperties.new(v.x, z, v.elast, v.fWeight, v.eWeight)
      v.Heat = math.min(v.Heat, v.BaseHeat)
    end
    local vstress = math.abs(((wdif + cspeed) / 0.774 * 0.774 - cspeed) / 15)
    if vstress > 0.05 and vstress > v.stress then
      v.stress = math.min(v.stress + 0.03, 1)
    else
      v.stress = math.max(v.stress - 0.03, vstress)
    end
    local UI = script.Parent.Tires[v.wheel.Name]
    UI.First.Second.Image.ImageColor3 = Color3.new(math.min(v.stress * 2, 1), 1 - v.stress, 0)
    UI.First.Position = UDim2.new(0, 0, 1 - v.Heat / v.BaseHeat, 0)
    UI.First.Second.Position = UDim2.new(0, 0, v.Heat / v.BaseHeat, 0)
  end
end
