How to Rig a Car

Hello so i have a problem in the third part, my code dosent work i tried to rewrite it a lot of times but still wrong, can someone help me?

 local car = script.Parent
local seat = car.Body.VehicleSeat
local body = car.Body.Body

local physicsService = game:GetService("PhysicsService")
local defaultCollisionGroup = "Default"
local characterCollisionGroup = "Character"

local cooldown = 0

local occupiedPlayer


local function Cooldown(durantion)
	local cooldownTag = tick()
	cooldown = cooldownTag
	delay(durantion, function()
		if (cooldown == cooldownTag) then
			cooldown = 0
		end
	end)
end


local function SetCharacterCollide(character, shouldCollide)
	local group = (shouldCollide and defaultCollisionGroup or characterCollisionGroup)
	for _,part in ipairs(character:GetDescendants()) do
		if (part:IsA("BasePart")) then
			part.Massless = not shouldCollide
			physicsService:SetPartCollisionGroup(part, group)
		end
	end
end


local function BodyTouched(part)

	if (seat.Occupant or cooldown ~= 0) then return end

	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if (not player) then return end

	local humanoid = character:FindFirstChildOfClass("humanoid")
	if (not humanoid) then return end

	seat:Sit(humanoid)
	occupiedPlayer = player
	SetCharacterCollide(character, false)
	car.PrimaryPart:SetNetworkOwner(player)
	Cooldown(1)

end


local function OccupantChanged()
	if (seat.Occupant) then return end
	if (occupiedPlayer.Character) then
		SetCharacterCollide(occupiedPlayer.Character, true)
	end
	car.PrimaryPart:SetNetworkOwner(player)
	occupiedPlayer = nil
end


body.Touched:Connect(BodyTouched)
seat:GetPropertyChangedSignal("Occupant"):Connect(OccupantChanged)

Just a question,

Will it lag if a bunch of vehicles are driving around?

Great Tutorial! Really well explained and detailed, it would be awesome if there was a continuation, where you show us how to add more stuff to the car gauges, turning signals, etc.

I don’t think so, i might be wrong. The script transfers network ownership to the local player but you can still lag when you have a LOT of them, depends on your pc specs.

Your missing a lot of code from the tutorial idk if you followed the whole tutorial but this was the final code:

local car = script.Parent
local seat = car.Body.VehicleSeat
local body = car.Body.Body

local physicsService = game:GetService("PhysicsService")
local DefaultCollisionGroup = "Default"
local CharacterCollisionGroup = "Character"

local cooldown = 0

local OccupiedPlayer
local OccupiedClientScript

local carClientScript = script.CarClient

local function Cooldown(duration)
	local cooldownTag = tick()
	cooldown = cooldownTag
	delay(duration,function()
		if (cooldown == cooldownTag) then
			cooldown = 0
		end
	end)
end


local function SetCharacterCollide(character,ShouldCollide)
	local group = (ShouldCollide and DefaultCollisionGroup or CharacterCollisionGroup)
	for _,part in ipairs(character:GetDescendants()) do
		if (part:IsA("BasePart")) then
			part.Massless = not ShouldCollide
			physicsService:SetPartCollisionGroup(part, group)
		end
	end
end

local function BodyTouched(part)
	
	if (seat.Occupant or cooldown ~= 0) then return end
	
	local character = part.Parent 
	local player = game.Players:GetPlayerFromCharacter(character)
	if (not player) then return end
	
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if (not humanoid) then return end
	
	seat:Sit(humanoid)
	OccupiedPlayer = player
	SetCharacterCollide(character,false)
	car.PrimaryPart:CanSetNetworkOwnership(player)
	OccupiedClientScript = carClientScript:Clone()
	OccupiedClientScript.Car.Value = car
	OccupiedClientScript.Parent = player.Backpack
	Cooldown(2)
end

local function OccupantChanged()
	if (seat.Occupant) then return end
	if (OccupiedPlayer.Character) then
		SetCharacterCollide(OccupiedPlayer.Character,true)
	end
	if (OccupiedClientScript.Parent) then
		OccupiedClientScript.Stop.Value = true
		local client = OccupiedClientScript
		delay(3, function()
			client:Destroy()
		end)
	end
	car.PrimaryPart:SetNetworkOwnershipAuto()
	OccupiedPlayer = nil
	OccupiedClientScript = nil
	Cooldown(3)
end

body.Touched:Connect(BodyTouched)
seat:GetPropertyChangedSignal("Occupant"):Connect(OccupantChanged)

I’m running into an issue with this car chassis setup. The car works great on laptop/desktop, and even works great on my Samsung Galaxy Note 9. However, the steering seems to be really odd on any Apple mobile devices. When you attempt to drive straight, the car will be turning. The controls are pretty sensitive on my Samsung phone but it’s not unbearable like it is on an iPhone.

Part of me thinks this is more of a Roblox issue, though I am not noticing this issue on other games. Is it possible Roblox is returning some weird numbers for my VehicleSeat’s SteerFloat for iPhones?

Here’s a link to my thread which gives a better outline of my problem, including a video:

If anyone knows what I can do to fix this, it would be greatly appreciated.

1 Like

The tutorial is super good! Keep up with your work! :grinning:

I don’t quite understand a few things and am need help with trying to make a drift button for a car chassis I’m making so far this is the code I’m using but it doesn’t seem to quite work that well. Also I’m needing help calculating suspension based off the mass of the car. I decide the mass of the car using a weight brick which I change the density using a formula. The suspension is the main thing I cant figure out also how would you recommend calculating how much density a car brick of 1x1x1 volume should have to equal 1 pound I need the suspension to match the density, and I’ve tried everything I can think of

--Suspension
local SuspensionC = Instance.new("SpringConstraint", Folder)
SuspensionC.Name = "Suspension"
SuspensionC.Damping = SpringDamping
SuspensionC.MaxForce = math.huge
SuspensionC.Stiffness = SpringStiffness
SuspensionC.LimitsEnabled = true
SuspensionC.MaxLength = 3
SuspensionC.MinLength = 0
SuspensionC.FreeLength = 2

local SuspensionPC = Instance.new("PrismaticConstraint", Folder)
SuspensionPC.Name = "Suspension"
SuspensionPC.LimitsEnabled = true
SuspensionPC.LowerLimit = 0
SuspensionPC.UpperLimit = 0

-- Drift Script
local function DriftOn()
if VehicleSeat.AssemblyLinearVelocity.Magnitude/Stats.SpeedMultiplier.Value> Stats.MinimumDriftSpeed.Value and math.abs(VehicleSeat.SteerFloat) >= 0.2 then
UsingDrift = true
Drift.AngularVelocity = Vector3.new(0,DriftVelocity* (-VehicleSeat.SteerFloat/math.abs(VehicleSeat.SteerFloat)),0)
Drift.MaxTorque = Vector3.new(0,Force,0)
for i,Wheelz in pairs(Model.Wheels:GetChildren()) do
Wheelz.CustomPhysicalProperties = PhysicalProperties.new(0.3, 0.36, 0, 100, 100)
end
wait(0.1)
Drift.AngularVelocity = Vector3.new(0,0,0)
Drift.MaxTorque = Vector3.new(0,0,0)
end
end

I’ve always found it easier to read this way I apologize for my weird habits.

I’m stuck on Part 2. My frontal wheels keep glitching upwards. If I do anything, that influences the suspension (falling, rolling down the wedge) the frontal wheels just break.

If I move it downwards, so it’s inside the Baseplate, it glitches back up, but with the wheels (sometimes) fixed.

(Note: My car, has multiple meshes, but all set up correct I guess)

Before (already simulating)
image

After letting it fall
image

After letting it roll downhill
image

You should add limits to the cylindrical constraints.

I tried using it, works now. Is this how you intended it to look like?
(the yellow numbers are just the Limits)

Yeah. But why do the springs look slanted?

EDIT: Make the upper limit / lower limit the bottom of the floor of the car, like where the 0 is.

Like this?

image

Yes. You can also put one of the limits under the wheel (Which looks like you’ve already done).

Seems to work! I set the Limits a bit more downwards, so I can simulate the wheels hanging (i.e. gap in road). Thanks!!

1 Like

I am working on eliminating Video 2 on rigging with a new car rigging plugin. Here’s a demo of it so far. Hopefully it helps streamline this process greatly:

Roblox: Car Rig Plugin Demo - YouTube

5 Likes

Why does my car randomly fling around like anything when I run the game? All the constraints are exactly the same as his one, the only difference is that am using my own car model (it just has the four wheels, not separate physical wheels). Is it probably the reason? I have also set the masses correctly.
EDIT: That was a problem with the springs, although I used the exact same configuration as sleit’s it still was flinging around. Probably due to the car mass or something. Whatever, tweaked the springs and its fixed.

Hello, I am on the last step and this is happening.

Disgusting bug - YouTube

It looks like you might have welded the physical wheels non-centered to the visual wheels. Either that, or the attachments aren’t centered. Either will cause the wheels to wobble since the center of rotation is off.