I have a model of a turret that casts a ray to detect if it can shoot the player. To prevent the ray from being blocked by the turret itself, I loop through the turret and its descendants, and add the parts to the raycastParam blacklist array. Despite this, the output in console shows that parts are blocking the ray; nothing is printed as being inside the blacklist as well:
> Ray found player
> Ray blocked by Tip (x7)
> Ray found player (x21)
local function rayReachedPlayer(player)
--[[ Checks if a ray casted from the turret to the passed player is blocked by an object. Returns true if blocked, false if clear path. ]]
local torso = player:FindFirstChild("HumanoidRootPart")
local raycastParams = RaycastParams.new()
for i,v in pairs(turret:GetDescendants() ) do
if v:IsA("BasePart") or v:IsA("Part") then
-- add all parts in turret to blacklist
table.insert(raycastParams.FilterDescendantsInstances, v)
end
end
for i=1, #raycastParams.FilterDescendantsInstances do
print(raycastParams.FilterDescendantsInstances[i].Name .. "is in the blacklist!!!!!")
end
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayDirection = (torso.Position - turretBase.Position).Unit * aggroDistance -- strange equation
local raycastResult = workspace:Raycast(turretBase.Position, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
local reachedPlayer = false
-- check if hit part is part of player
for i,v in pairs(player:GetDescendants()) do
if hitPart == v then
reachedPlayer = true
end
end
if reachedPlayer then
print("Ray found player")
return true -- nothing blocking! ray has found player
else
print("Ray blocked by " .. hitPart.Name)
return false -- something is in the way.
end
else
print("Ray failed to cast. raycastResult does not exist!")
end
end