Unable to cast value to object error

I am using a ray to detect where a bullet should go, and damage anything it touches

local Part,Position = workspace:FindPartOnRay(ray, {player.Character,script.Parent.Handle},false,true)

This is the code to detect anything it touches.
When this line is executed I get this error:

16:50:38.518 - Unable to cast value to Object
16:50:38.519 - Stack Begin
16:50:38.520 - Script 'Workspace.Pistol.WeaponScript', Line 3
16:50:38.521 - Stack End

Please explain why this is happening and how I can fix and prevent it in futer projects.

Where are you defining ray and player? Is this actually line 3? Why is it so close to the top? I need a lot more of your script.

FindPartOnRay only accepts a single object/instance for its 2nd argument (not a table), but i would recommend using the new api (WorldRoot:Raycast) for raycasting since FindPartOnRay is deprecated now anyways.

1 Like

This is the full script. (The event dosnt matter because I have already tracked the problem to this script)

script.Parent.Fire.OnServerEvent:Connect(function(player,from,to,range)
	local ray = Ray.new(from, (to - from).unit*range)
	local Part,Position = workspace:FindPartOnRay(ray, {player.Character,script.Parent.Handle},false,true)
	if Part then
		local hum = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(script.Parent.Damage.Value)
		end
	end	
end)

Thanks for asking!

OnlyJaycbee is correct, you need FindOnRayWithIgnoreList if you are using a list. Should have noticed, my bad.