Raycast Parameters cause a raycast to return a nil value and it is driving me insane

Alright. So, I have a raycast. It should be a simple projection from Point A to Point B and then returns the position it hit, or nil if it hits the void. Right?

I have a Raycast that starts at one position of an instance, follows the LookVector of the Instance's CFrame and then has parameters to stop it from hitting itself or particular bits around it.

However the Params variable seems to disagree unless I have done something horribly wrong. Whenever the Raycast tries to not hit the part it starts from or some slight parts around it, it will just immediately go nil, not even go and hit the next closest part, just nil outright. And it has driven me insane trying to fix it.

The error goes something like this when it nils
[script location]:[line]: attempt to index nil with 'Position'

Now for a code sample:

			local RayCastParamsVariable = RaycastParams.new()
			RayCastParamsVariable.FilterType = Enum.RaycastFilterType.Blacklist
			RayCastParamsVariable.FilterDescendantsInstances = {
			local RayCastResult1 = workspace:Raycast(PartRaycastedFrom1.Position, (PartRaycastedFrom1.CFrame.LookVector * 25), RayCastParamsVariable)
			local RayCastResult2 = workspace:Raycast(PartRaycastedFrom2.Position, (PartRaycastedFrom2.CFrame.LookVector * 25), RayCastParamsVariable)

Messing around with the params variable has hurt my brain a lot.

Say the parts Raycasted from are not in the table but the parts around them are, the Raycast then hits the parts Raycasted from. If both of them are in the Params variable it returns nil, it also returns a completely wrong value which I don’t want if those nearby parts are excluded from the table.

To put it simpler:
The blacklist function doesn’t want to cooperate with me and instead makes my Raycasts return nil and also makes certain instances cause the Raycast to return nil even if it doesn’t go into the void.

Extra notes:
There is no way for the Raycasts to hit the void, I have put several parts surrounding the area of the Raycast which would result in the Raycast hitting them if it were to have somehow escaped the intended area.

Removing the Params variable from the Raycast results in it working fine but the Raycast will immediately hit the parts around the origin point as they are not blacklisted.

Edit: Feel free to request any other bits of info that might help an effort to resolve the issue, I’m completely open to give info that might prove useful.

Why are you giving it a part for the first parameter?
According to the API reference the first parameter should be a vector (In this case, the position of PartRaycastedFrom1)

1 Like

Oh apologies, that was just a mess up I made while copying the code over, it should’ve had a .Position after it.

Where have you defined the above?

The error is saying that PartRaycastedFrom1 is nil; below:

local RayCastResult1 = workspace:Raycast(PartRaycastedFrom1.Position, (PartRaycastedFrom1.CFrame.LookVector * 25), RayCastParamsVariable)

In the actual script those are defined as parts in the workspace, I just wanted to shorten them to avoid confusion, apologies.

Here is an old-timey raycast, but it still works. Put any black-listed parts in a model called AI.

function DrawRay(origin, point) – 7 studs long
local Ray = Ray.new(origin, (point).unit * 7) --Make the ray.
local Hit,Position = game.Workspace:FindPartOnRay(Ray,AI) --Check for collisions along the ray, ignoring any Parts of us.

if true then --Graphics
local RayPart = Instance.new(“Part”,AI)
if Hit then
if Logic == 1 then
RayPart.BrickColor = BrickColor.new(“Black”) --Set its color.
RayPart.BrickColor = BrickColor.new(“Bright red”) --Set its color.
if Logic == 1 then
RayPart.BrickColor = BrickColor.new(“White”) --Set its color.
RayPart.BrickColor = BrickColor.new(“Olive”) --Set its color.
RayPart.Transparency = 0.2 --Set its transparency.
RayPart.Anchored = true --Set whether it will fall or not.
RayPart.CanCollide = false --Set whether people can walk though it or not.
RayPart.formFactor = Enum.FormFactor.Custom --Make it so it can be small.
local Distance = (Position-origin).magnitude --Find the distance between the hit and the torso.
RayPart.Size = Vector3.new(0.4,.2,Distance) --Set its size to the distance.
RayPart.CFrame = CFrame.new(Position, origin) * CFrame.new(0,0,-Distance/2) --Move it halfway.
game.Debris:AddItem(RayPart,2) --Add it to the debris.
end – Graphics

return Hit
end – DrawRay

The error message says that you are asking for the Position from SOME part which doesn’t exist. Put print statements every-other line of your code to check all of your assumptions…

–This is a direction; not a point.

I’ve already done that to an extent, I’ve checked all three values entered into the raycasts and nothing seems off.

Origin is a Vector3
Direction is a CFrame.LookVector which just converts into a Vector3

and then there’s the params which is what causes the problem if it’s present in the raycast

more specifically the LookVector is multiplied by 25 which doesn’t result in a nil value

The problem isn’t quite getting the raycast to raycast, it does that fine as I’ve ran it through quite a few tests without the raycast params variable, it’s, again, the raycast params variable that’s causing the issue. Not the Origin or Direction.

I should probably also post the full script since it’s probably not helping to not have the full thing visible.

			local LeftArmClone = game.ServerStorage.LeftArmPunchingClone:Clone()
			local RightArmClone = game.ServerStorage.RightArmPunchingClone:Clone()
			LeftArmClone.Parent = workspace
			RightArmClone.Parent = workspace
			LeftArmClone.CFrame = LeftArm.CFrame
			RightArmClone.CFrame = RightArm.CFrame	
			local RayCastParamsVariable = RaycastParams.new()
			RayCastParamsVariable.FilterType = Enum.RaycastFilterType.Blacklist
			local Array1 = {}
			local Array2 = {}
			local Array3 = {}
			for _, Thing in pairs(Boss:GetDescendants()) do
				if Thing.ClassName == "Part" or Thing.ClassName == "UnionOperation" then
					table.insert(Array1, Thing)
			for _, Thing in pairs(LeftArmClone:GetDescendants()) do
				if Thing.ClassName == "Part" or Thing.ClassName == "UnionOperation" then
					table.insert(Array2, Thing)
			for _, Thing in pairs(RightArmClone:GetDescendants()) do
				if Thing.ClassName == "Part" or Thing.ClassName == "UnionOperation" then
					table.insert(Array3, Thing)
			RayCastParamsVariable.FilterDescendantsInstances = {Array1, Array2, Array3, LeftArmClone, RightArmClone, game.Workspace.TestCastleBossArena.Target}
			local RayCastResult1 = workspace:Raycast(LeftArmClone.Position, (LeftArmClone.CFrame.LookVector * 25), RayCastParamsVariable)
			local RayCastResult2 = workspace:Raycast(RightArmClone.Position, (RightArmClone.CFrame.LookVector * 25), RayCastParamsVariable)

I tried to shorten it for this post to reduce confusion but I think the full thing might be better for people trying to help here.

Those four instances isolated in the params variable before were the four that were causing problems, for clarification.

PartRaycastedFrom1 is LeftArmClone
PartRaycastedFrom2 is RightArmClone

PartNearRaycastedPart1 & PartNearRaycastedPart2 are two specific parts in the workspace that are attached to a rigged model.

I have not used the new raycast, but this page seems to indicate that …Instances wants a model; not multiple arrays, but what do I know…?

  1. Build a “RaycastParams” object
  2. local raycastParams = RaycastParams.new()
  3. raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
  4. raycastParams.FilterDescendantsInstances = {workspace.Model}
  5. raycastParams.IgnoreWater = true

WorldRoot:Raycast (roblox.com)

It also seems to say that you can change just the parameters and re-ask the result from the same raycast.

It also mentions another type of parameter based on Collision settings which Bam-bozzels me at this point…

And you have always got to draw the ray when debugging rays… Draw it.

Your rays are hitting nil because you are using the Blacklist FilterType. Blacklist means the list of instances provided will be ignored.

1 Like

But that’s exactly what I want, to ignore hitting the parts that the rays come from as well as a fair few parts around them.

The reason there are so many instances loaded into the list is because the rig is quite complex so there’s a lot of parts that can potentially be accidentally hit when I don’t want them to.

Why then the ray hits nothing when there are quite a few other parts around to be hit beats me.

Can you send a screenshot of your model setup? Perhaps your range of 25 studs is too small.

Sure thing.
A word of warning though: The main model itself is incredibly complicated, and the arm clones are almost as complex in of themselves.



Left Leg:
Left Arm:

Right Leg:
Right Arm:

Left Arm Punching Clone:

Right Arm Punching Clone:

I specifically wanted to avoid having to send screenshots of the entire list of instances since it would probably get too confusing, but since you asked here it is.

Also it’s not a range of 25 studs, that is a Vector3 position having a multiplier of 25 so the Raycast reaches quite far, the value is not nil however and I have tested it quite a few times with prints to be sure of that.

( you know this is serious when I have to close the properties window )

Also, here’s what the model looks like in full when the error occurs:
Note the parts around the immediate area, there is no way a raycast shouldn’t be able to hit anything.

When you multiply a LookVector the multiplier is the range in studs. LookVectors are between -1 and 1 on every axis. Your issue is most likely that your ray is hitting nothing because it isn’t reaching far enough. If not perhaps some collision groups are interfering?

For a moment I thought that might’ve worked, however it unfortunately did not. Unless I’ve done something wrong here the LookVector itself is either being multiplied by a number on all three axis or I’ve done something weird here.

local RayCastResult1 = workspace:Raycast(LeftArmClone.Position, (LeftArmClone.CFrame.LookVector * 100), RayCastParamsVariable)
local RayCastResult2 = workspace:Raycast(RightArmClone.Position, (RightArmClone.CFrame.LookVector * 100), RayCastParamsVariable)

The two lines of code that Raycast are shown above, maybe they are being multiplied instead of the * acting as an indication of how many studs in the direction it needs to go? I’m not too sure here to be honest.

( compared to last time I changed the supposed amount of studs to travel to 100 in the event that it was just on too short of a range )