Raycast Problem

I’m trying to make a mop script that cleans a puddle by using raycasting but it always errors in the output: Unable to cast value to Object.

Can someone explain to me why this is happening?

Here’s my script:


local Tool = script.Parent

local MopRaycast = Ray.new(Tool.CleaningPart.Position,Tool.CleaningPart.Position + Vector3.new(0,3.5,0))

CanClean = false

while wait(1) do
	if CanClean == true then
		local position, hit = workspace:FindPartOnRay(MopRaycast,{Tool.Handle},false,true)
		
		if hit then
			if hit.Name == "Puddle" then
				
				local Health = hit:FindFirstChild("Health")
				
				Health.Value = Health.Value - 1
				
			end
		end
	end
end

which line does it says the error

local position, hit = workspace:FindPartOnRay(MopRaycast,{Tool.Handle},false,true) is the line where it errors

can i see your explorer on the Tool
and if im not wrong, i dont think you’re suppose to be using FindPartsOnRay since its old

FindPartOnRay’s ignore filter can only be one Instance, that being said, it will use all descendants of that Instance.

For what you’re trying to do, just doing Tool.Handle outside of an array would work.

Consider using the newer workspace:Raycast method alongside RaycastParams

It has a nicer API and you dont need to make a Ray object

1 Like