I have been trying to get a ray to ignore player’s / NPC’s accessory(s), but to no avail. I have looked at other people’s issues with the same thing, but none of those solutions have worked. Can someone please help me with this?
tool.Activated:Connect(function()
local rO = mouse.Origin.Position
local rDe = mouse.Target.Position
local rDi = rDe - rO
local rP = RaycastParams.new()
if ammo > 0 then
ammo = ammo - 1
tool.ToolTip = ammo
print(ammo)
local ShootSound = tool.Handle.Shoot
local fire = tool.fire
local animplayerfire = plr.Character:WaitForChild("Humanoid"):LoadAnimation(fire)
animplayerfire:Play()
ShootSound:Play()
local iP = {}
rP.FilterType = Enum.RaycastFilterType.Exclude
rP.FilterDescendantsInstances = iP
local rR = workspace:Raycast(rO, rDi, rP)
if rR.Instance.Name == "Handle" then
table.insert(iP, rR.Instance)
end
if rR.Instance.Parent:FindFirstChildWhichIsA("Humanoid") then
print(rR)
if rR.Instance.Name == "Head" then
local hum = rR.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
hum.Health = hum.Health - 20
else
local hum = rR.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
hum.Health = hum.Health - 10
end
end
rE:FireServer(rO, rDi)
end
end)
local iP = {}
for _,p in game.Players:GetPlayers() do
if not table.find(iP, p.Character) then
table.insert(iP, p.Character)
end
end
rP.FilterDescendantsInstances = iP
The primary and most efficient method to address this issue would be to iterate through each player in the game and subsequently iterate through the child elements of each player’s character. Create an array containing the character of the player from whom the ray originates, in addition to the accessories belonging to each player. Subsequently, set the FilterDescendantsInstances property to this newly created array.
CanQuery makes it so the part ignores every ray and lets it pass through
workspace.DescendantAdded:Connect(function(Descendant) -- new descendant appears
if Descendant:IsA("Accessory") then
wait() -- I put a wait here bc sometimes it takes time to completely load in
for i, Part in pairs(Descendant:GetChildren()) do
if Part:IsA("BasePart") then
Part.CanQuery = false
end
end
end
end)
You can simply disable the CanQuery property on every accessory part that appears, or if you want you can add every accessory to the FilterDescendantsInstances every time you fire the gun like my friend said above