"Unable to cast value to object" while raycasting

Hello! I’ve ran into a roadblock while scripting a boat for my game. I tried using raycasts to detect when the boat is on land, but when defining FilterDescendentsInstances, the output gave me this error:

“Unable to cast value to Objects”

Here’s my code:

local Seat = script.Parent.VehicleSeat
local TurnedOn = script.Parent.Engine.TurnedOn
local OnCD = script.Parent.Engine.On.ClickDetector
local OffCD = script.Parent.Engine.Off.ClickDetector
local params = RaycastParams.new()
params.FilterDescendantsInstances = game.Workspace.Terrain
params.FilterType = Enum.RaycastFilterType.Include

OnCD.MouseClick:Connect(function()
	TurnedOn.Value = true
end)

OffCD.MouseClick:Connect(function()
	TurnedOn.Value = false
end)

Seat.Changed:Connect(function()
	if TurnedOn.Value ~= false then 
	script.AngularVelocity.AngularVelocity = Vector3.new(0, -1 * Seat.Steer, 0)
	script.LinearVelocity.LineVelocity = 50 * Seat.Throttle
	print("works")
	end
end)

while task.wait() do
	local sPos = script.Parent.VehicleSeat.Position + Vector3.new(0,5,0)
	local ePos = script.Parent.VehicleSeat.Position + Vector3.new(0,15,0)
	local ray = workspace:Raycast(sPos,ePos - sPos, params)
	if ray and ray.Material == Enum.Material.Water then
		script.AngularVelocity.Enabled = true
		script.LinearVelocity.Enabled = true
	else
		script.AngularVelocity.Enabled = false
		script.LinearVelocity.Enabled = false
	end
end

Please let me know what I did wrong. I’m thankful for any suggestions or help!

Thanks
PikeEnthusiast

Which line is throwing this error?

Sorry for the confusion. Line six.

The .FilterDescendantsInstances property of a RaycastParams object is supposed to be a table with the relevant instances in it, not a parent instance.

You’ll need to do:

params.FilterDescendantsInstances = game.Workspace.Terrain:GetChildren()
3 Likes

Not getting any errors anymore and my print commands are going through, but the boat isn’t moving anymore. I think that’s a whole other issue so I’m just going to put your response as the solution. Thank you for your help!

In case anyone was curious as to why it wasn’t moving, the additional parts on the boat were being hit by the raycast, so I added elseif conditions whitelisting them in the while loop.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.