Touched event ignoring criterias of the part is hitting

Hello, it’s a basic question, I’m trying to do that when the wheels touch any part that has the material grass, start making a trail but there is a problem, i made the criteria that the hit part must be a grass, but it seems it is ignoring, its detecting touch on every part of the vehicle, the chassis, etc, i did some print to see which part was touching and yea, its touching everything, even if its not grass. Here is the script:

for _, wheels in pairs(script.Parent.Parent.Wheels:GetChildren()) do
	wheels.Touched:Connect(function(hit)
		if hit.Material == Enum.Material.Grass then
			wheels.WheelFixed.Mark2.Trail.Enabled = true
			wheels.WheelFixed.Mark.Trail.Enabled = true
		end
		if hit.Material ~= Enum.Material.Grass then
			wheels.WheelFixed.Mark2.Trail.Enabled = false
			wheels.WheelFixed.Mark.Trail.Enabled = false
		end
	end)
end

the script changed way too much since I first started but the logic is kinda the same

I tried using this post as help too, and it didnt work: Run Script While Touching Part - #12 by AlerbusWasTaken

Make an if conditional to check if “wheels” is named “Wheel” (assuming this is what your wheels are called, if they aren’t, rename them or just change the conditional)

for _, wheels in pairs(script.Parent.Parent.Wheels:GetChildren()) do
      if wheels.Name == "Wheel" then
	    wheels.Touched:Connect(function(hit)

And for the material:

Try doing ```if hit.Material == “Grass” then``, it should work, it’s the same but worth to try, if it’s still touching everything even if they aren’t grass, there’s maybe something that touches the wheel that is a grass material, maybe part of the car.

Now the script its this

for _, wheels in pairs(script.Parent.Parent.Wheels:GetChildren()) do
	wheels.Touched:Connect(function(hit)
		if wheels:IsA("BasePart") then
			if hit.Material == "Grass" then
				print(wheels.Name.. "touched the".. hit.Name)
				wheels.WheelFixed.Mark2.Trail.Enabled = true
				wheels.WheelFixed.Mark.Trail.Enabled = true
			end
		end
	end)
end

and its not printing anything, and im surprised how hard the easy things are bruh

i tried sometime doing if the grass get touched by the wheels, but the collision is bad with moving parts and the trail appears and disappears after one sec

The trails are disabled as soon they touch another part that’s not grass in your script, that’s probably why, maybe it gets touched by the car parts itself, disabling the trail as soon the trail gets enabled, so you should add a debounce:

Debounce = false

for _, wheels in pairs(script.Parent.Parent.Wheels:GetChildren()) do
	wheels.Touched:Connect(function(hit)
		if hit.Material == Enum.Material.Grass then
			Debounce = true
			wheels.WheelFixed.Mark2.Trail.Enabled = true
			wheels.WheelFixed.Mark.Trail.Enabled = true
			wait(1) 
			Debounce = false
		end
		if hit.Material ~= Enum.Material.Grass and Debounce == false then
			wheels.WheelFixed.Mark2.Trail.Enabled = false
			wheels.WheelFixed.Mark.Trail.Enabled = false
		end
	end)
end

i thought that would be the best solution because when hitting the grass it starts the trail and when it touches the concrete which is the road it stops but i didnt think of that, and i think that debounce wouldnt work but i will test here, and i tested without the grass material criteria and it worked, is there any way i could detect if the part is grass?

and i couldnt get a proper screenshot but when driving on a straight line the trail appears and dissappears and i think that shouldnt be the way it works?

image

The debounce would work, your current problem is the trail is getting disabled again as soon as it’s enabled, because the wheel is colliding with another part that’s not grass, probably the car parts.

Yea, it’s colliding with the car parts, put a print(hit.Name) to see what the wheels are touching, better way is to get all descendants in your car model and then make the wheels ignore touched for it.

can i make a blacklist for the car parts?