"Unable to cast RaycastResult to Ray" help with raycasting?

I have a raycast script that creates a raycast, then checks if the raycast hits a part that contains a humanoid.

However, I keep getting this error: Unable to cast RaycastResult to Ray

I’m not really good at raycasting so i don’t know how to fix this. Can someone help please?

local params = RaycastParams.new()
local direction = currentNormal*(overrideDistance or stepDistance)
params.FilterDescendantsInstances = {script.Parent.Parent}
params.FilterType = Enum.RaycastFilterType.Exclude
local result = game.Workspace:Raycast(currentPos,direction,params)
local pos

if result then
	print(result)
local hit = game.Workspace:FindPartOnRay(result,chr) -- Error occurs here 
-- "Unable to cast RaycastResult to Ray'

	local hum = hit.Parent:FindFirstChild('Humanoid')

	if hum then
		hum:TakeDamage(30)
	end
end

That function is deprecated, and only works with the old Ray system. Instead, you should use RaycastResult.Instance, which is result.Instance in your case.

1 Like

when i use

if result.Instance then

end

it says "attempt to index nil with ‘Instance’ "

That means you never got a result. Did you put it within if result then?

1 Like

edit: never mind i misunderstood what you said

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