Raycasting not detecting part only returning nil

when i Jump in front of a wall, the ray doesnt hit anything and The output says “nil”. Was this raycasting supposed to be done in server-side.?

origin = hrp.Position
direction = hrp.CFrame.LookVector * 10

origin = hrp.Position
direction = hrp.CFrame.LookVector * 10

function wallCheck()
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	if raycastResult then
		local hitpart = raycastResult.Instance
		if hitpart then
			return true
		end
	end
end

hum.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping and can_long_jump == true then
		hrp.Velocity = hrp.CFrame.LookVector * (longJumpSpeed+50) + Vector3.new(0, 50, 0)
		loadedLongJumpAnim:Play()
	elseif newState == Enum.HumanoidStateType.Landed then
		loadedLongJumpAnim:Stop()
		hum.WalkSpeed = 16
	elseif newState == Enum.HumanoidStateType.Jumping and can_long_jump == false then
		local walldetection = wallCheck()
		print(walldetection)
		if walldetection == true then
			hrp.Velocity.Y = Vector3.new(0, 50, 0)
		end
	end
end)

Probably because it only does it once and uses those old results for the checking instead of new results, try moving

origin = hrp.Position
direction = hrp.CFrame.LookVector * 10

Inside of the wallcheck function

function wallCheck()
    local origin = hrp.Position
    local direction = hrp.CFrame.LookVector * 10
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	if raycastResult then
		local hitpart = raycastResult.Instance
		if hitpart then
			return true
		end
	end
end
2 Likes

should i do it for frame by frame or put it inside while true do loop.?

Put those 2 lines I mentioned i nthe wallCheck function so when you call it, it’ll get the newest position and LookVector

1 Like

thnx.! I found out the problem…! The two lines I put before the function, worked…but It had some issue…like… The hrp spawned in Vector3.new(0, 0, 0) position, but when i called the function .its origin was the same didnt change…thnx…!

1 Like