A-Chassis Grass Detection

Hey y’all.
Currently using a f1 car model (https://create.roblox.com/marketplace/asset/8462249673/Mercedes-Petronas-F1-Car) and I’ve made a track. I’d love to punish the player for cutting into track limits, ideally by making the car much slower when touching grass.

The car already has a way of detecting which surface it is on, via this dust effect script:

WheelModel = script.Parent:WaitForChild("Wheels")
dustEffect = script:WaitForChild("DustEffect")
wheels = {}
touchingparts = {}

for i, v in pairs(WheelModel:GetChildren()) do
	if v:IsA("BasePart") then
		table.insert(wheels, v)
		dustEffect:Clone().Parent = v
		table.insert(touchingparts, nil)
	end
end
for i, v in pairs(wheels) do
	v.Touched:connect(function(part)
		touchingparts[i] = part
	end)
end
RunService = game:GetService("RunService")

RunService.Heartbeat:connect(function()
	for i, v in pairs(wheels) do
		local speed = v.Velocity.magnitude
		if speed > 20 then
			if touchingparts[i].Material == Enum.Material.Grass or touchingparts[i].Material == Enum.Material.Pebble or touchingparts[i].Material == Enum.Material.Sand or touchingparts[i].Material == Enum.Material.Slate or touchingparts[i].Material == Enum.Material.Ground or touchingparts[i].Material == Enum.Material.Mud or touchingparts[i].Material == Enum.Material.LeafyGrass or touchingparts[i].Material == Enum.Material.Basalt or touchingparts[i].Material == Enum.Material.Rock or touchingparts[i].Material == Enum.Material.CrackedLava then
				v.DustEffect.Rate = 5/100 * speed * 30
				
			else
				v.DustEffect.Rate = 0
			end
		else
			v.DustEffect.Rate = 0
		end
	end
end)

Issue is, this script does not change anything about the physics and is merely cosmetic. How could I modify, use or repurpose this script to slow the car when off track? What values would I need to change to slow the car, and how I change them, given that they are stored in seperate scripts?

I strongly advise you check out the model yourself, because I can’t really describe the inner structure of the car. Thanks for the help in advance.

Add debug boxes that slow down the vehicle when touching it

I’ve also added a similar type of effect on a riding lawnmower to have it emit grass, dust, snow, water, or sparks depending on a raycast pointing down from the mower deck.

Make this script set a NumberValue in the cars seat, or wherever you want to add it. When the car is on grass set it to .6 or whatever works. If they’re on pavement set it to 1.
In your driving script you need a function to check to see if the NumberValue changes. If it does then multiply their HingeConstraint AngularVelocity by the NumberValue to change the hinges rotational speed.

I’m not sure if a raycast or shapecast would work more efficiently than checking for touching parts.

Hi,
The adding of code to make different effects sounds sweet, do you happen to have an .rbxl file that has the code and effect for different detections of the raycast?

Thanks

I just get the RaycastResult of the Material hit by the ray, then use if statements for my ParticleEmitters.
I use one emitter for each material with the image already in it.
I use simple if statements

if raycastResult then -- checks to see if any material is hit
    if the material is grass
        enable true the grass emitter
        enable false all the other emitters
    elseif the material is rock
        enable true the sparks emitter
        enable false all other emitters
    elseif 
        --keep going for all the other materials you need to
    end

else  -- means no raycastresult
    enable false all emitters
end