Check if car is driving on sand

i want that the cars max speed changes if the car is driving on sand.
i have put a part underneath the car, its the part that checks the material the car is on.
Now its working but after a while it just doesn’t want to work anymore and the cars speed changes back to normal even though the car is still driving on sand.

Heres my script:

local part = script.Parent -- The material checker part
local targetMaterial = Enum.Material.Sand -- the material it should detect
local ChangeAttribute = script.Parent.Parent.Parent

local function onTouch(hit)
	local partMaterial = part.Material
	if partMaterial == targetMaterial then
		ChangeAttribute:SetAttribute("MaxSpeed", 50) -- if material is sand change cars speed
		print("omg car is driving on sand lololol time to make car slower woooo")
		elseif not (partMaterial == targetMaterial) then
			ChangeAttribute:SetAttribute("MaxSpeed", 101.5) -- if its not sand then make the cars speed normal
		
	end
end


	part.Touched:Connect(onTouch)

I would be very happy if someone helps me!

1 Like

Make a part directly under the chassis and fire a ray down from it.
If ray has instance and instance has sand material, slow it down

But whats wrong with material checking? i mean its working but after a while not anymore

Probably because you only raycasted from the part once. If not, then I suggest sending the updated script so I can help?

You’re checking the material of the Part, the part’s material doesn’t change depending on what’s under it.

You can just fire a ray downwards from the car and check the Material property of the result, which works for terrain as well.

Edit: I didn’t notice the touched event before, though the raycast method would probably be more reliable.

1 Like