Raycast returning nil

Hey, everyoneI’ve been making a script that detects obstacles and check if you can jump over it with raycasts.
Something like this:
Screenshot_2481
(great drawing skills i know)

However the 2nd ray returns nil when it shouldn’t,
here’s the code:

			local FrontPosition = RaycastFront(Blob.RootPart)
			
			if FrontPosition ~= nil then
				if (FrontPosition.Position - Blob.RootPart.Position).Magnitude <= (Position - Blob.RootPart.Position).Magnitude and (FrontPosition.Position - Blob.RootPart.Position).Magnitude <= 2 then
					
					local BlacklistTable = {Blob}
					
					local function RaycastDown(Cast, Blacklist)
					
						local raycastParams = RaycastParams.new()
						raycastParams.FilterDescendantsInstances = Blacklist
						raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
						local raycastResult = workspace:Raycast(Cast, Cast - Vector3.new(0,100,0) * 500, raycastParams)
	
						if raycastResult then
								return raycastResult
							end
						end 
					
					local DownPosition = RaycastDown(FrontPosition.Position + Vector3.new(0,10000,0), BlacklistTable).Position
                 --[[^^ ERROR HAPPENS IN THIS LINE ^^ ]]				

					if (DownPosition - Blob.RootPart.Position) <= 6 then 
						Blob.RootPart.Position = DownPosition + Vector3.new(0,1,0)
						Animations.MoveTo(Blob, Position)
						break
					end
				end
			end

Error: ServerScriptService.Blob Animations:131: attempt to index nil with ‘Position’

Sorry about messy code.
Any help is appreciated!

2 Likes

Can we see the full script? It seems like you made some custom functions, and there should be codes behind these functions.

Shouldn’t your raycasts Vector3 Y Axis be negative instead of positive since you’re shooting the ray down on the Y axis, not up? It returns nil because it can’t find the property “Position” of the instance since no Raycast Instance was returned.

1 Like

That’s the origin Vector3, not the direction as later in the function I make the direction go downwards:

This is the function that you don’t see:

local function RaycastFront(Cast)
	local rayOrigin
	local rayDirection

	rayOrigin = Cast.Position
	rayDirection = Cast.CFrame.LookVector * 500

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Cast.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if raycastResult then
		return raycastResult
	else
		return nil
	end
end

You’re right, my bad. Maybe you’re going over the Raycasts range limit. Don’t set the origin to 10K blocks high. Lower the origin point and ray direction.

1 Like

Thank you! I never knew raycasts had limits (I’m new to raycasts), thank you for pointing it out :)!

1 Like