Raycasting Help

Hello, I’m making a boat and I want it to be able to go on water, and ice. But it either wont detect the water or wont detect the ice

for i, boat in pairs(script.Parent:GetDescendants()) do
	if boat:GetAttribute("Boat") then
		local Seat = boat.VehicleSeat
		
		local ray
		local canJump = true
		
		local params = RaycastParams.new()
		
		params.FilterType = Enum.RaycastFilterType.Exclude
		params.FilterDescendantsInstances = {workspace.Water:GetDescendants()} -- water is a folder with parts that look like water, the actaul water is invisible

		local mass = Seat.Parent.Main:GetMass()
		Seat.Jump.Force = Vector3.new(0, mass * 50, 0)

		Seat.Changed:Connect(function()
			Seat.AngularVelocity.AngularVelocity = Vector3.new(0,-1 * Seat.Steer,0)
			Seat.LinearVelocity.LineVelocity = Seat.MaxSpeed * Seat.Throttle
			if Seat.Occupant then
				Seat.Occupant.JumpPower = 0
			end
		end)

		coroutine.wrap(function()
			while task.wait() do
				if ray then
					if Seat.Occupant ~= nil and ray.Material == Enum.Material.Water then
						if Seat.Occupant.Jump == true and canJump == true then
							canJump = false
							Seat.Jump.Force = Vector3.new(0, mass * 300 * (0.5 * Seat.Parent.JumpPower.Value), 0)
							task.wait(0.2)
							Seat.Jump.Force = Vector3.new(0, mass * 50, 0)
							task.wait(1)
							canJump = true
						end
					end
				end
			end
		end)()

		coroutine.wrap(function()
			while task.wait() do
				local sPos = Seat.Position + Vector3.new(0, 5, 0) 
				local ePos = Seat.Position - Vector3.new(0, 20, 0)
				ray = workspace:Raycast(sPos, ePos - sPos, params)
				if ray and ray.Material == Enum.Material.Water then
					Seat.AngularVelocity.Enabled = true
					Seat.LinearVelocity.Enabled = true
				else
					Seat.AngularVelocity.Enabled = false
					Seat.LinearVelocity.Enabled = false
				end
			end
			print(ray)
		end)()
	end
end

“params.FilterDescendantsInstances = {workspace.Water:GetDescendants()} – water is a folder with parts that look like water, the actaul water is invisible”

This line alone is your issue.

So you’re saying it only detects like one (water or ice) gotcha

Could you explain?

I think that your FilterType should be Include
And as far I know, yout don’t need to use :GetDescendants() method um that table.

1 Like

That’s actually something I missed. Nice catching that! Yeah they’re trying to detect only water and so they’d need to Include those parts

Also correct, it filters all of the instance’s descendants so you don’t have to get them all.

Yeah I think your post might be the solution

1 Like

I fixed it by adding a folder called “ice” and make it so it detects stuff in the ice folder as well

params.FilterDescendantsInstances = {workspace.Terrain, workspace.Ice:GetDescendants()}