Can anyone help me understand the Jeep's script?

Hello there,

So I wanted to make a decent vehicle for my game, But I had no clue how it works. So I searched through deforum and saw most people telling to check out “Jeep” vehicle from toolbox, So I did but I didn’t understood a single thing. Moreover, there are 0 comments in the script showing what it does. So can any of you help me understand it?

Heres the script:

--Scripted by DermonDarble

local car = script.Parent
local stats = car.Configurations
local Raycast = require(script.RaycastModule)

local mass = 0

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

local bodyPosition = car.Chassis.BodyPosition
local bodyGyro = car.Chassis.BodyGyro

--local bodyPosition = Instance.new("BodyPosition", car.Chassis)
--bodyPosition.MaxForce = Vector3.new()
--local bodyGyro = Instance.new("BodyGyro", car.Chassis)
--bodyGyro.MaxTorque = Vector3.new()

local function UpdateThruster(thruster)
	-- Raycasting
	local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value) --game.Workspace:FindPartOnRay(ray, car)
	local thrusterHeight = (position - thruster.Position).magnitude
	
	-- Wheel
	local wheelWeld = thruster:FindFirstChild("WheelWeld")
	wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 0.8) + (wheelWeld.Part1.Size.Y / 2), 0)
	-- Wheel turning
	local offset = car.Chassis.CFrame:inverse() * thruster.CFrame
	local speed = car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)
	if offset.Z < 0 then
		local direction = 1
		if speed.Z > 0 then
			direction = -1
		end
		wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(0, (car.Chassis.RotVelocity.Y / 2) * direction, 0)
	end
	
	-- Particles
	if hit and thruster.Velocity.magnitude >= 5 then
		wheelWeld.Part1.ParticleEmitter.Enabled = true
	else
		wheelWeld.Part1.ParticleEmitter.Enabled = false
	end
end

car.DriveSeat.Changed:connect(function(property)
	if property == "Occupant" then
		if car.DriveSeat.Occupant then
			car.EngineBlock.Running.Pitch = 1
			car.EngineBlock.Running:Play()
			local player = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
			if player then
				car.DriveSeat:SetNetworkOwner(player)
				local localCarScript = script.LocalCarScript:Clone()
				localCarScript.Parent = player.PlayerGui
				localCarScript.Car.Value = car
				localCarScript.Disabled = false
			end
		else
			car.EngineBlock.Running:Stop()
		end
	end
end)

--spawn(function()
	while true do
		game:GetService("RunService").Stepped:wait()
		for i, part in pairs(car:GetChildren()) do
			if part.Name == "Thruster" then
				UpdateThruster(part)
			end
		end
		if car.DriveSeat.Occupant then
			local ratio = car.DriveSeat.Velocity.magnitude / stats.Speed.Value
			car.EngineBlock.Running.Pitch = 1 + ratio / 4
			bodyPosition.MaxForce = Vector3.new()
			bodyGyro.MaxTorque = Vector3.new()
		else
			local hit, position, normal = Raycast.new(car.Chassis.Position, car.Chassis.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
			if hit and hit.CanCollide then
				bodyPosition.MaxForce = Vector3.new(mass / 5, math.huge, mass / 5)
				bodyPosition.Position = (CFrame.new(position, position + normal) * CFrame.new(0, 0, -stats.Height.Value + 0.5)).Position
				bodyGyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)
				bodyGyro.CFrame = CFrame.new(position, position + normal) * CFrame.Angles(-math.pi/2, 0, 0)
			else
				bodyPosition.MaxForce = Vector3.new()
				bodyGyro.MaxTorque = Vector3.new()
			end
		end
	end
--end)

Thanks in advance for reading.

1 Like

This is my opinion, but maybe you should learn more lua in order to understand certain complex models, I’d suggest going to Roblox Developer Hub as it cracks down different pieces of code. Also, you should also try to play around with roblox studio for a little, just to see how it works.

1 Like

I have been coding in lua for 2 years now. I understand most of lua but I don’t understand CFrames and vector maths which this script mostly uses.

This also includes different body vectors, which you should either look on YouTube, Roblox Developer Hub, or this platform in general.

What have u tried to achieve a descent vehicle before?
On that Jeep script they use a BodyMovers approach.
A bodyPosition and a bodyGyro.

The other stuff in the script are just the Seat functions and some particles for stetic

Learn to use those bodymovers, and experiment a lot, idk check tutorials maybe.
If you already know how to code, you will notice that those CFrames are not all in the system

EDIT: True I forgot the raycasting to check the ground @EmeraldLimes :+1:

1 Like

Although I do agree with what @Enhanced_Tix has said, I just wanted to explain the basics of how the car works.

In a nutshell,

The car uses BodyPosition, which is kind of like a force that will attempt to move a part to a certain position. In other words, it’s like gravity, only it pushes/gravitates towards the Position property.

The car also uses Raycasting (put simply, shooting a ray/laser and seeing what it hits) to determine if the car is on the ground, and if so, then move it will move the Position value of the BodyPosition to the front of the car, thus moving the car forwards.

I hope this is simplified enough so that you can understand easily, or at least gain some kind of understanding. Cheers! :slight_smile:

1 Like

Thank you,

Now I understood how this car actually uses body movers.

It looks like most people are telling to learn bodymovers So I’ll go learn them before understanding this script.

3 Likes

I basically have the same question too. Since this post was made very recently, ill just ask here. Does anybody know what “WheelWeld.C0” Means? I have been trying to swap out the body of this car for a new one.

Edit: I was using the wrong kind of weld.