Raycast returning nil

print("lol")
local db = false
script.Parent.Humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
	if db == false then
		db = true
		if script.Parent.Humanoid.Jump == true then

			script.Parent.PlayerSphere.AssemblyLinearVelocity += Vector3.new(0,60,0)
			print("Jump")
		end
		repeat wait() 
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			local raycastResult = workspace:Raycast(script.Parent.PlayerSphere.Position, (script.Parent.PlayerSphere.Position + Vector3.new(0,-1,0)).Unit * 3.6, raycastParams)
			print(raycastResult)
		until raycastResult.Instance

		db = false
	end
end)

Why does it print out nil?, i’m pretty sure it hits a part.

Have you checked if it’s looking using the front surface?

How do get that?, do rays even have a face? aren’t they a line?
The script does not make the ray point in a realtive direction, it always points straight below.

Right, so a big problem for developers who are new to raycasting is that they don’t fully understand what a raycast is comprised of. Raycasts always take two vectors:

  • Origin: This is a Vector3 space in the 3D environment from which your ray will start at. The origin can be any point you want, it doesn’t particularly matter.

  • Direction: Yes, it’s a direction not an endpoint. The second Vector3 that raycasts take is a direction relative to the origin. Unlike the origin that works in world space, the direction is an object-space vector that determines where the raycast needs to point.

In your specific case, if you want the direction to be 3.6 studs downwards then your direction is simply a Vector3 with 0 on the X and Z axis and -3.6 as your Y axis. This tells the raycast to start at the PlayerSphere’s position and the direction is 3.6 studs downward from the origin point.

workspace:Raycast(script.Parent.PlayerSphere.Position, Vector3.new(0, -3.6, 0), raycastParams)

Ok gonna try that in a minute.

Ok, raycastresult still returns nil despite a part being right below the playersphere.

Funky.

local sphere = workspace.Sphere
local raycastParams = RaycastParams.new()

local result = workspace:Raycast(sphere.Position, Vector3.new(0, -10, 0), raycastParams)
if result then
	print(result.Instance)
end

Is there something I’m missing here? Could I see a visual of what you’re working with or what your results are turning up as?

Ok, in a minute
:shallow_pan_of_food:

:exploding_head:

I see an error in your console. Remember that your conditional is going to get checked after each iteration to see if it needs to continue running this code. You should fix that first before moving onto any further debugging unless that’s your very issue, that being the error causing the loop to terminate.

When raycasting, it’s imperative to actually check if raycastResult exists. If your print is showing raycastResult as nil then naturally you shouldn’t try to index nil.

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

repeat
    local raycastResult = workspace:Raycast(script.Parent.PlayerSphere.Position, Vector3.new(0, -3.6, 0), raycastParams)
    if not raycastResult then wait() end
until (raycastResult and raycastResult.Instance)

Also took the liberty to move the RaycastParams creation and property setting outside of the repeat loop since it doesn’t appear that you dynamically change these, so better to save on performance and reuse the RaycastParams in each iteration.

1 Like

The script returns no errors but if i print raycastresult it still returns nil

Are you able to create a repro file or include steps to recreate your environment so I can try getting an on-hands look at the problem in Studio? The only two oversights or issues I know from what I can see have been resolved but RaycastResult is apparently still nil so there might be something I can’t find.

The script:


local db = false
script.Parent.Humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
	if db == false then
		db = true
		if script.Parent.Humanoid.Jump == true then

			script.Parent.PlayerSphere.AssemblyLinearVelocity += Vector3.new(0,60,0)

		end
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

		repeat
			local raycastResult = workspace:Raycast(script.Parent.PlayerSphere.Position, Vector3.new(0, -3.6, 0), raycastParams)
			if not raycastResult then wait() end
			print(raycastResult)
		until (raycastResult and raycastResult.Instance)

		db = false
	end
end)

Location in hierarchy:
image

1 Like

Aand I just realised I blacklisted the entire workspace lol thanks for the help anyway o_O

:flushed:

Does it… does it work now? That’s interesting lol but would make sense after seeing the hierarchy.

Yes it works just fine, it was just that mistake :///

1 Like