Unable to cast Vector3 to Ray

Greetings, Developers.

I am attempting to make a gun engine and I am running into an error at the minute while trying to cast a ray.

I have just modified my Raycast from the below:

local player = LocalPlayer
	local position = cursor.Hit.Position
	local LocalPlr = Workspace:FindFirstChild(LocalPlayer.Name)
	local rayOrigin = LocalPlr.Head.Position
	local bulletOrigin = shoot_part.Position
	local direction = (position - rayOrigin).Unit*300
	local result = Workspace:Raycast(rayOrigin, direction)

To this:

	local player = LocalPlayer
	local position = cursor.Hit.Position
	local LocalPlr = Workspace:FindFirstChild(LocalPlayer.Name)
	local rayOrigin = LocalPlr.Head.Position
	local bulletOrigin = shoot_part.Position
	local direction = (position - rayOrigin).Unit*300
	print(direction)
	local headRay = Ray.new(rayOrigin, direction)
	local result = Workspace:FindPartOnRayWithIgnoreList(bulletOrigin, LocalPlr)
	local wallcheck = Workspace:FindPartOnRayWithIgnoreList(rayOrigin, LocalPlr) --Added to determine if player is trying to shoot through a wall.

I did this so that I can successfully ignore the player whenever the ray casts as the user can damage themselves whenever firing, however now I am running into an issue now I am using FindPartOnRay as opposed to Raycast.

I am not sure exactly what I have done wrong when I switched from Raycast to FindPartOnRay so any help is appreciated!

Thank you!

Edit

I forgot to post the console error.

  14:53:22.017  Unable to cast Vector3 to Ray  -  Client - WeaponMain:41

(WeaponMain:41 refers to “local result =…”)

1 Like

The first parameter to FindPartOnRayWithIgnoreList is a Ray instance.
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRayWithIgnoreList

You are passing Vector3 instances.

You will want to pass Rays created from bulletOrigin to whatever direction. Perhaps something like this?

local result = Workspace:FindPartOnRayWithIgnoreList(Ray.new(bulletOrigin, Direction), LocalPlr)
local wallcheck = Workspace:FindPartOnRayWithIgnoreList(headRay, LocalPlr)
3 Likes

That has fixed it, thank you.

However now It’s saying it’s “Unable to cast value to Objects” on the same line again.

Never-mind, I have resolved this issue. Thank you for the help!

Sorry I did not reply. I was doing something.

Okay that is good.

1 Like