Raycast blacklist not working

Hi,
I’ve got a monster model which has a Head, Torso, HumanoidRootPart and Body which is a MeshPart with bones inside for animating.

Monster model children:
MonsterChildren
Monster model descendants:

This monster is chasing players on a map but it can be distracted by a distract provided that he can see it. So I used raycasting and casted a ray from monster to distract to see if the way is clear. I also made a blacklist to prevent ray hitting monster’s body parts but when I’m trying to print raycastResult.Instance it says nil or Torso (sometimes Distract when it’s directly looking at it but that’s not how I want it to be), here’s the script:

local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {monster} -- `monster` is the monster model shown above
rayParams.FilterType = Enum.RaycastFilterType.Blacklist

local rayOrgin = monster.HumanoidRootPart.Position
local rayDestination = distract.Position
local rayDirection = rayDestination - rayOrgin

local raycastResults = workspace:Raycast(rayOrgin, rayDirection, rayParams)
--[[local raycastResults2 = workspace:Raycast(rayOrgin, rayDirection)
if not raycastResults and raycastResults2 then
	print(raycastResults.Instance)
	print(raycastResults2.Instance)
end]]
			
if raycastResults and raycastResults.Instance == distract then
	local direction = (distract.Position - monster.HumanoidRootPart.Position).Unit
	
	return distract.Position - (direction * ((tonumber(monster.Size.Value)+1) * 2))
end

Any help is very appreaciated

Not really sure this is the issue, but use CFrame.Position for both the distract and the monster’s positions, for most moving models, CFrame is correct and not Position.

local rayOrgin = monster.HumanoidRootPart.CFrame.Position
local rayDestination = distract.CFrame.Position

Also, try multiplying the rayDirection by something like 1.05 just to make it go past the distract as an extra layer of assurance to it.

local raycastResults = workspace:Raycast(rayOrgin, rayDirection*1.05, rayParams)
1 Like

No, everything is the same. I don’t think that BasePart.Position is in any degree different than BasePart.CFrame.Position.

It looks like you’ve done the raycasting correctly, which just means that maybe the monster variable is either not constructed correctly or its just nil, so you could show us how you’ve constructed your “monster” variable?

1 Like

There’s an ObjectValue in ReplicatedStorage which stores monster model when the game starts and it’s impossible that durning a game it suddenly becomes nil and back a monster

local ReplicatedStorage = game:GetService("ReplicatedStorage")
...
local monsterContainer = ReplicatedStorage:WaitForChild("Monster")
...
while active.Value and monsterContainer.Value:FindFirstChild("HumanoidRootPart") do
    local monster = monsterContainer.Value
    ...
end
1 Like

Well try using a different approach, since the monster is named “Monster” in workspace just check if the workspace has that:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
...
local monsterContainer = ReplicatedStorage:WaitForChild("Monster")
...
while active.Value and workspace:FindFirstChild("Monster") do
    local monster = workspace.Monster
    ...
end
1 Like

Without testing I am sure that it won’t change anything and with this approach the other scripts would die because all are using that ObjectValue. The monster name is also not “Monster” but 0, 1, 2, 3, 4 depending on its size, I changed it to “Monster” for better understanding

Oh alright, well i guess you could try printing the monster variable, check if its value is what you expect it to be.

1 Like

I put a print after declaring the variable and this is the result:
monsterExist