Raycast always returning nil

Ok, so, I’m making a sort-of top down camera, and I’m trying to use raycasts to detect when a player is hidden behind an object, so I can highlight them so they’re aware of their position.

But, for whatever reason this raycast always returns nil, despite there being a very obvious break in the line of sight of the player.

    local RayBlacklist = RaycastParams.new()
    RayBlacklist.FilterDescendantsInstances = {Character}
    RayBlacklist.FilterType = Enum.RaycastFilterType.Blacklist
	local CollisionRay = workspace:Raycast(Camera.CFrame.Position, Character.HumanoidRootPart.Position, RayBlacklist)
	
	Visualiser:Destroy()
	
	Visualiser = Instance.new("Part", workspace)
	Visualiser.CFrame = CFrame.new(CollisionRay.Position,CollisionRay.Position)
	Visualiser.Anchored = true

The “Visualiser” part is a debug part that I tried to make to diagnose why it wouldn’t execute a print() when the ray fired. But, since it’s not returning anything, that didn’t work…

1 Like

The second argument is the direction, not the target. The direction is (target-startpos).unit, and then you can multiply by however many studs long you want the ray to be.

A raycast returns nil when it doesnt hit anything

You need to get direction via camera or mouse or something.