Why is my raycast always returning nil?

Hey there! I’m trying to cast a raycast to the right side of the humanoid, but it’s always returning nil, what is going on?

Script (local, StarterCharacterScripts):

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {character}

local maxDistance = 2.5

repeat
	runService.RenderStepped:Wait()
	local rayR2 = workspace:Raycast(humanoid.RootPart.Position, humanoid.RootPart.CFrame.RightVector * maxDistance, rayParams)
	print(rayR2)
until not rayR2

Any help is appreciated!

1 Like

You probably need to define rayR2 out of the repeat function, then reset it using “rayR2 =”

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = {character}

local maxDistance = 2.5
local rayR2

repeat
	runService.RenderStepped:Wait()
	rayR2 = workspace:Raycast(humanoid.RootPart.Position, humanoid.RootPart.CFrame.RightVector * maxDistance, rayParams)
	print(rayR2)
until not rayR2
1 Like

It still returns nil, but it does seem to work when I do it on the left side:

repeat
	runService.RenderStepped:Wait()
	local rayL2 = workspace:Raycast(humanoid.RootPart.Position, humanoid.RootPart.CFrame.RightVector * -maxDistance, rayParams)
	print(rayL2)
until not rayL2

(all the variables stay the same)

1 Like

Can you print the ray direction on the r2?

1 Like

It prints 0, 0, -2.5

(- character limit -)

1 Like

2.5 studs is suppppper short. Basically, if something isn’t touching this blue line you’re going to get nil.


1 Like

Yes, I know that. It’s supposed to be short since it is used for a wallrunning script, where the arm will clip into the wall anyways. It does seem to be working when I do it on the left side, just not the right side for whatever reason.

1 Like

So if you increase it to 5 is it still nill.

2 Likes

Yes, it is.

(- character limit -)

1 Like

And humanoid.rootpart isn’t character.HumanoidRootPart.

Sorry never got to it this way

2 Likes

It works everywhere else, so it should work here too.

1 Like

Can you send an image or video? You’re starting with something on your side that should instantly trigger it?

2 Likes

Oh ok what if you just print.

humanoid.RootPart.CFrame.RightVector

2 Likes

Then it will print out 0, 0, -1

1 Like

Yes, there is a wall on my side.

1 Like

Yes sorry just trying to figure why would the left work.

2 Likes

Oh my god I feel so stupid, I just realized it was because of the way I turn my character to be adjacent with the wall, and that being the wrong way. Sorry everyone!

3 Likes

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