Having some trouble with raycast blacklisting

Hey guys I’ve been trying to write a code that will detect if the player is looking at water when their player state is swimming, and that means the character needs to be blacklisted / ignored to scan for it. The raycast is running and detects water when the player is underneath it, however it will return nil if the player is above water (presumably because the ray hits the blacklisted objects in it). I was just wondering if items that get blacklisted are just instant stop collisions for raycast that cause it to return nil or there was a problem with the way the parameters are set.

local Player = game.Players.LocalPlayer

print(Player.Character)

local Character = Player.Character

Character:WaitForChild(“Humanoid”).StateChanged:Connect(function(oldState, newState)

local Humanoid = Character.Humanoid	
	
	if newState == Enum.HumanoidStateType.Swimming then
		local camera = game.Workspace.CurrentCamera
		
		while Humanoid:GetState() == Enum.HumanoidStateType.Swimming do

			local raycastParams = RaycastParams.new()
				raycastParams.FilterDescendantsInstances =	{Character}
				raycastParams.FilterType = Enum.RaycastFilterType.Blacklist					
		
			local raycastResult = workspace:Raycast(camera.CFrame.Position, Character.Head.Position - camera.CFrame.Position, raycastParams)
			
			print(raycastResult)
			
			wait(0.05)
		end
end

end)

To reiterate, it seems that if the ray hits the player (that has been blacklisted), it will always return nil. Is there a way for it to simply ignore these blacklisted objects instead of returning nil when colliding?

try this:

local Player = game.Players.LocalPlayer
local Character = Player.Character
Character:WaitForChild("Humanoid").StateChanged:Connect(function(oldState, newState)
local Humanoid = Character.Humanoid
if newState == Enum.HumanoidStateType.Swimming then
local camera = game.Workspace.CurrentCamera
while Humanoid:GetState() == Enum.HumanoidStateType.Swimming do
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayStart = camera.CFrame.Position
local rayEnd = Character.Head.Position - camera.CFrame.Position
local raySize = 1
local raycastResult
repeat
raycastResult = workspace:Raycast(rayStart, rayEnd * raySize, raycastParams)
raySize = raySize * 2
until raycastResult or raySize > 32
print(raycastResult)
wait(0.05)
end
end
end)

Hi there! The issue is that when the raycast hits the blacklisted player object, it returns nil instead of ignoring it. To fix this, you could try changing the FilterType in the RaycastParams to Enum.RaycastFilterType.Whitelist and including only objects that you want the raycast to detect. This way, the raycast will only detect objects that are on the whitelist and ignore everything else, including the blacklisted player.