0% found this document useful (0 votes)
143 views11 pages

Roblox Car Chassis Script Guide

The document contains a Lua script for configuring a vehicle's chassis in a game environment, including functions for creating welds, adjusting wheel configurations, and managing vehicle weight. It initializes components, applies physical properties to parts, and sets up steering and suspension systems based on tuning parameters. The script also includes logic for weight distribution and mass calculations to ensure proper vehicle dynamics.

Uploaded by

Chayaphon Boon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views11 pages

Roblox Car Chassis Script Guide

The document contains a Lua script for configuring a vehicle's chassis in a game environment, including functions for creating welds, adjusting wheel configurations, and managing vehicle weight. It initializes components, applies physical properties to parts, and sets up steering and suspension systems based on tuning parameters. The script also includes logic for weight distribution and mass calculations to ensure proper vehicle dynamics.

Uploaded by

Chayaphon Boon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

--[[

___ _______ _
/ _ |____/ ___/ / ___ ____ ___ (_)__
/ __ /___/ /__/ _ \/ _ `(_-<(_-</ (_-<
/_/ |_| \___/_//_/\_,_/___/___/_/___/
SecondLogic @ Inspare
]]

--[[START]]

_BuildVersion = require([Link])

--[[Weld functions]]

local JS = game:GetService("JointsService")
local PGS_ON = workspace:PGSIsEnabled()

function MakeWeld(x,y,type,s)
if type==nil then type="Weld" end
local W=[Link](type,JS)
W.Part0=x W.Part1=y
W.C0=[Link]:inverse()*[Link]
W.C1=[Link]:inverse()*[Link]
if type=="Motor" and s~=nil then
[Link]=s
end
return W
end

function ModelWeld(a,b)
if a:IsA("BasePart") then
MakeWeld(b,a,"Weld")
elseif a:IsA("Model") then
for i,v in pairs(a:GetChildren()) do
ModelWeld(v,b)
end
end
end

function UnAnchor(a)
if a:IsA("BasePart") then [Link]=false end for i,v in
pairs(a:GetChildren()) do UnAnchor(v) end
end

--[[Initialize]]

[Link]:WaitForChild("A-Chassis Interface")
[Link]:WaitForChild("Plugins")
[Link]:WaitForChild("README")

local car=[Link]
local _Tune=require([Link])

wait(_Tune.LoadDelay)

--Weight Scaling
local weightScaling = _Tune.WeightScaling
if not workspace:PGSIsEnabled() then
weightScaling = _Tune.LegacyScaling
end

local Drive=[Link]:GetChildren()

--Remove Existing Mass


function DReduce(p)
for i,v in pairs(p:GetChildren())do
if v:IsA("BasePart") then
if [Link] == nil then
[Link] = [Link]([Link]) end
[Link] = [Link](
0,
[Link],
[Link],
[Link],
[Link]
)
end
DReduce(v)
end
end
DReduce(car)

--[[Wheel Configuration]]

--Store Reference Orientation Function


function getParts(model,t,a)
for i,v in pairs(model:GetChildren()) do
if v:IsA("BasePart") then [Link](t,
{v,[Link]:toObjectSpace([Link])})
elseif v:IsA("Model") then getParts(v,t,a)
end
end
end

--PGS/Legacy
local fDensity = _Tune.FWheelDensity
local rDensity = _Tune.RWheelDensity
if not PGS_ON then
fDensity = _Tune.FWLgcyDensity
rDensity = _Tune.RWLgcyDensity
end

local fDistX=_Tune.FWsBoneLen*[Link]([Link](_Tune.FWsBoneAngle))
local fDistY=_Tune.FWsBoneLen*[Link]([Link](_Tune.FWsBoneAngle))
local rDistX=_Tune.RWsBoneLen*[Link]([Link](_Tune.RWsBoneAngle))
local rDistY=_Tune.RWsBoneLen*[Link]([Link](_Tune.RWsBoneAngle))

local fSLX=_Tune.FSusLength*[Link]([Link](_Tune.FSusAngle))
local fSLY=_Tune.FSusLength*[Link]([Link](_Tune.FSusAngle))
local rSLX=_Tune.RSusLength*[Link]([Link](_Tune.RSusAngle))
local rSLY=_Tune.RSusLength*[Link]([Link](_Tune.RSusAngle))

for _,v in pairs(Drive) do


--Apply Wheel Density
if [Link]=="FL" or [Link]=="FR" or [Link]=="F" then
if v:IsA("BasePart") then
if [Link] == nil then
[Link] = [Link]([Link]) end
[Link] = [Link](
fDensity,
[Link],
[Link],
[Link],
[Link]
)
end
else
if v:IsA("BasePart") then
if [Link] == nil then
[Link] = [Link]([Link]) end
[Link] = [Link](
rDensity,
[Link],
[Link],
[Link],
[Link]
)
end
end

--Resurface Wheels
for _,a in pairs({"Top","Bottom","Left","Right","Front","Back"}) do
v[a.."Surface"]=[Link]
end

--Store Axle-Anchored/Suspension-Anchored Part Orientation


local WParts = {}

local tPos = [Link]


if [Link]=="FL" or [Link]=="RL" then
[Link] =
[Link]*[Link]([Link](90),0,[Link](90))
else
[Link] =
[Link]*[Link]([Link](90),0,[Link](-90))
end
[Link] = [Link]+tPos

if v:FindFirstChild("Parts")~=nil then
getParts([Link],WParts,v)
end
if v:FindFirstChild("Fixed")~=nil then
getParts([Link],WParts,v)
end

--Align Wheels
if [Link]=="FL" or [Link]=="FR" then
[Link] = [Link]*[Link]([Link](_Tune.FCamber),0,0)
if [Link]=="FL" then
[Link] = [Link]*[Link](0,0,[Link](_Tune.FToe))
else
[Link] = [Link]*[Link](0,0,[Link](-
_Tune.FToe))
end
elseif [Link]=="RL" or [Link]=="RR" then
[Link] = [Link]*[Link]([Link](_Tune.RCamber),0,0)
if [Link]=="RL" then
[Link] = [Link]*[Link](0,0,[Link](_Tune.RToe))
else
[Link] = [Link]*[Link](0,0,[Link](-
_Tune.RToe))
end
end

--Re-orient Axle-Anchored/Suspension-Anchored Parts


for _,a in pairs(WParts) do
a[1].CFrame=[Link]:toWorldSpace(a[2])
end

--[[Chassis Assembly]]
--Create Steering Axle
local arm=[Link]("Part",v)
[Link]="Arm"
[Link]=true
[Link]=false
[Link]=[Link]
[Link]=[Link](_Tune.AxleSize,_Tune.AxleSize,_Tune.AxleSize)

[Link]=([Link]*[Link](0,_Tune.StAxisOffset,0))*[Link](-
[Link]/2,-[Link]/2,0)
[Link] =
[Link](_Tune.AxleDensity,0,0,100,100)
[Link]=[Link]
[Link]=[Link]
[Link]=1

--Create Wheel Spindle


local base=arm:Clone()
[Link]=v
[Link]="Base"
[Link]=[Link]*[Link](0,_Tune.AxleSize,0)
[Link]=[Link]

--Create Steering Anchor


local axle=arm:Clone()
[Link]=v
[Link]="Axle"
[Link]=[Link]([Link]-(([Link]*[Link]([Link]/
2,0,0)).lookVector*(([Link].x/2)+([Link].x/
2))),[Link])*[Link](0,[Link],0)
[Link]=[Link]

if [Link]=="F" or [Link]=="R" then


local axle2=arm:Clone()
[Link]=v
[Link]="Axle"
[Link]=[Link]([Link]+
(([Link]*[Link]([Link]/2,0,0)).lookVector*(([Link].x/2)+([Link].x/
2))),[Link])*[Link](0,[Link],0)
[Link]=[Link]
MakeWeld(arm,axle2)
end

--Create Suspension
if PGS_ON and _Tune.SusEnabled then
local sa=arm:Clone()
[Link]=v
[Link]="#SA"
if [Link] == "FL" or [Link]=="FR" or [Link] =="F" then
local aOff = _Tune.FAnchorOffset
[Link]=[Link]*[Link](_Tune.AxleSize/2,-fDistX,-
fDistY)*[Link](aOff[3],aOff[1],-aOff[2])*[Link](-[Link]/2,-[Link]/2,0)
else
local aOff = _Tune.RAnchorOffset
[Link]=[Link]*[Link](_Tune.AxleSize/2,-rDistX,-
rDistY)*[Link](aOff[3],aOff[1],-aOff[2])*[Link](-[Link]/2,-[Link]/2,0)
end

local sb=sa:Clone()
[Link]=v
[Link]="#SB"
[Link]=[Link]*[Link](0,0,_Tune.AxleSize)

[Link]=[Link]

local g = [Link]("BodyGyro",sb)
[Link] = "Stabilizer"
[Link] = [Link](0,0,1)
g.P = 0

local sf1 = [Link]("Attachment",sa)


[Link] = "SAtt"

local sf2 = sf1:Clone()


[Link] = sb

if [Link] == "FL" or [Link] == "FR" or [Link] == "F" then


[Link] = [Link](fDistX-fSLX,-
fDistY+fSLY,_Tune.AxleSize/2)
[Link] =
[Link](fDistX,-fDistY,-_Tune.AxleSize/2)
elseif [Link] == "RL" or [Link]=="RR" or [Link] == "R" then
[Link] = [Link](rDistX-rSLX,-
rDistY+rSLY,_Tune.AxleSize/2)
[Link] =
[Link](rDistX,-rDistY,-_Tune.AxleSize/2)
end

sb:MakeJoints()

local sp = [Link]("SpringConstraint",v)
[Link] = "Spring"
sp.Attachment0 = sf1
sp.Attachment1 = sf2
[Link] = true

[Link]=_Tune.SusVisible
[Link]=_Tune.SusRadius
[Link]=_Tune.SusThickness
[Link]=[Link](_Tune.SusColor)
[Link]=_Tune.SusCoilCount

if [Link] == "FL" or [Link]=="FR" or [Link] =="F" then


g.D = _Tune.FAntiRoll
[Link] = _Tune.FSusDamping
[Link] = _Tune.FSusStiffness
[Link] = _Tune.FSusLength+_Tune.FPreCompress
[Link] = _Tune.FSusLength+_Tune.FExtensionLim
[Link] = _Tune.FSusLength-_Tune.FCompressLim
else
g.D = _Tune.RAntiRoll
[Link] = _Tune.RSusDamping
[Link] = _Tune.RSusStiffness
[Link] = _Tune.RSusLength+_Tune.RPreCompress
[Link] = _Tune.RSusLength+_Tune.RExtensionLim
[Link] = _Tune.RSusLength-_Tune.RCompressLim
end

MakeWeld([Link],sa)
MakeWeld(sb,base)
else
MakeWeld([Link],base)
end

--Lock Rear Steering Axle


if [Link] == "RL" or [Link] == "RR" or [Link]=="R" then
MakeWeld(base,axle)
end

--Weld Assembly
if [Link] == "RL" or [Link] == "RR" or [Link]=="R" then
MakeWeld([Link],arm)
end

MakeWeld(arm,axle)

arm:MakeJoints()
axle:MakeJoints()

--Weld Miscelaneous Parts


if v:FindFirstChild("SuspensionFixed")~=nil then
ModelWeld([Link],[Link])
end
if v:FindFirstChild("WheelFixed")~=nil then
ModelWeld([Link],axle)
end
if v:FindFirstChild("Fixed")~=nil then
ModelWeld([Link],arm)
end

--Weld Wheel Parts


if v:FindFirstChild("Parts")~=nil then
ModelWeld([Link],v)
end

--Add Steering Gyro


if v:FindFirstChild("Steer") then
v:FindFirstChild("Steer"):Destroy()
end

if [Link]=="FL" or [Link]=="FR" or [Link]=="F" then


local steer=[Link]("BodyGyro",arm)
[Link]="Steer"
steer.P=_Tune.SteerP
steer.D=_Tune.SteerD
[Link]=[Link](0,_Tune.SteerMaxTorque,0)
[Link]=[Link]*[Link](0,-[Link]/2,0)
end

--Add Stabilization Gyro


local gyro=[Link]("BodyGyro",v)
[Link]="Stabilizer"
[Link]=[Link](1,0,1)
gyro.P=0
if [Link]=="FL" or [Link]=="FR" or [Link]=="F" then
gyro.D=_Tune.FGyroDamp
else
gyro.D=_Tune.RGyroDamp
end

--Add Rotational BodyMover


local AV=[Link]("BodyAngularVelocity",v)
[Link]="#AV"
[Link]=[Link](0,0,0)
[Link]=[Link](_Tune.PBrakeForce,0,_Tune.PBrakeForce)
AV.P=1e9
end

--[[Vehicle Weight]]
--Determine Current Mass
local mass=0

function getMass(p)
for i,v in pairs(p:GetChildren())do
if v:IsA("BasePart") then
mass=mass+v:GetMass()
end
getMass(v)
end
end
getMass(car)

--Apply Vehicle Weight


if mass<_Tune.Weight*weightScaling then
--Calculate Weight Distribution
local centerF = [Link]()
local centerR = [Link]()
local countF = 0
local countR = 0

for i,v in pairs(Drive) do


if [Link]=="FL" or [Link]=="FR" or [Link]=="F" then
centerF = centerF+[Link].p
countF = countF+1
else
centerR = centerR+[Link].p
countR = countR+1
end
end
centerF = centerF/countF
centerR = centerR/countR
local center = centerR:Lerp(centerF, _Tune.WeightDist/100)

--Create Weight Brick


local weightB = [Link]("Part",[Link])
[Link] = "#Weight"
[Link] = true
[Link] = false
[Link] = [Link]("Really black")
[Link] = [Link]
[Link] = [Link]
if _Tune.WBVisible then
[Link] = .75
else
[Link] = 1
end
[Link] =
[Link](_Tune.WeightBSize[1],_Tune.WeightBSize[2],_Tune.WeightBSize[3])
[Link] =
[Link](((_Tune.Weight*weightScaling)-mass)/([Link].x*weightB.
Size.y*[Link].z),0,0,0,0)
[Link]=([Link]-
[Link]+center)*[Link](0,_Tune.CGHeight,0)
else
--Existing Weight Is Too Massive
warn( "\n\t [AC".._BuildVersion.."]: Mass too high for specified
weight."
.."\n\t Target Mass:\t"..
([Link](_Tune.Weight*weightScaling*100)/100)
.."\n\t Current Mass:\t"..([Link](mass*100)/100)
.."\n\t Reduce part size or axle density to achieve desired
weight.")
end

local flipG = [Link]("BodyGyro",[Link])


[Link] = "Flip"
flipG.D = 0
[Link] = [Link](0,0,0)
flipG.P = 0

--[[Finalize Chassis]]
--Misc Weld
wait()
for i,v in pairs(script:GetChildren()) do
if v:IsA("ModuleScript") then
require(v)
end
end

--Weld Body
wait()
ModelWeld([Link],[Link])

--Unanchor
wait()
UnAnchor(car)

--[[Manage Plugins]]

[Link]["A-Chassis Interface"].[Link]=car
for i,v in pairs([Link]:GetChildren()) do
for _,a in pairs(v:GetChildren()) do
if a:IsA("RemoteEvent") or a:IsA("RemoteFunction") then
[Link]=car
for _,b in pairs(a:GetChildren()) do
if b:IsA("Script") then [Link]=false end
end
end
end
[Link] = [Link]["A-Chassis Interface"]
end
[Link]:Destroy()

--[[Remove Character Weight]]


--Get Seats
local Seats = {}
function getSeats(p)
for i,v in pairs(p:GetChildren()) do
if v:IsA("VehicleSeat") or v:IsA("Seat") then
local seat = {}
[Link] = v
[Link] = {}
[Link](Seats,seat)
end
getSeats(v)
end
end
getSeats(car)

--Store Physical Properties/Remove Mass Function


function getPProperties(mod,t)
for i,v in pairs(mod:GetChildren()) do
if v:IsA("BasePart") then
if [Link] == nil then
[Link] = [Link]([Link]) end
[Link](t,{v,[Link]})
[Link] = [Link](
0,
[Link],
[Link],
[Link],
[Link]
)
end
getPProperties(v,t)
end
end
--Apply Seat Handler
for i,v in pairs(Seats) do
--Sit Handler
[Link]:connect(function(child)
if [Link]=="SeatWeld" and child:IsA("Weld") and
child.Part1~=nil and [Link] ~= workspace and not
[Link]:IsDescendantOf(car) then
[Link] = {}
getPProperties([Link],[Link])
end
end)

--Leave Handler
[Link]:connect(function(child)
if [Link]=="SeatWeld" and child:IsA("Weld") then
for i,v in pairs([Link]) do
if v[1]~=nil and v[2]~=nil and
v[1]:IsDescendantOf(workspace) then
v[1].CustomPhysicalProperties = v[2]
end
end
[Link] = {}
end
end)
end

--[[Driver Handling]]

--Driver Sit
[Link]:connect(function(child)
if [Link]=="SeatWeld" and child:IsA("Weld") and
[Link]:GetPlayerFromCharacter([Link])~=nil then
--Distribute Client Interface
local p=[Link]:GetPlayerFromCharacter([Link])
[Link]:SetNetworkOwner(p)
local g=[Link]["A-Chassis Interface"]:Clone()
[Link]=[Link]
end
end)

--Driver Leave
[Link]:connect(function(child)
if [Link]=="SeatWeld" and child:IsA("Weld") then
--Remove Flip Force
if [Link]:FindFirstChild("Flip")~=nil then
[Link] = [Link]()
end

--Remove Wheel Force


for i,v in pairs([Link]:GetChildren()) do
if v:FindFirstChild("#AV")~=nil then
if v["#AV"]:IsA("BodyAngularVelocity") then
if v["#AV"].[Link]>0 then
v["#AV"].AngularVelocity = [Link]()
v["#AV"].MaxTorque = [Link]()
end
else
if v["#AV"].AngularVelocity>0 then
v["#AV"].AngularVelocity = 0
v["#AV"].MotorMaxTorque = 0
end
end
end
end
end
end)

--[END]]

You might also like