Hey so, I’ve made a hit detection system that uses Raycast, as you can probably assume by the title, When the cast hits a character, I get the exact part I hit (e.g. RightHand, RightLowerLeg, etc)
I know way to get the Humanoid from this while removing duplicate Humanoid’s but mine is kind of lengthy and janky lol. (just a bunch of array sorting)
I’m wondering if there’s a known “best/efficient way” to filter the Humanoids out of the Raycast!
What I like to do is setting FilterDescendantInstances of the parameters to {Character}. This is the easiest way to exclude all character’s parts from raycasting as far as I know.
AFAIK each raycast can only hit 1 part, so I guess you’re asking how to find unique humanoids for a bunch of raycasts?
You can use a dictionary (hash map) to efficiently find unique values in a table:
function unique(t)
local found = {}
local unique = {}
for _, v in pairs(t) do
if found[t] then continue end
found[t] = true
table.insert(unique, v)
end
return unique
end
function findHumanoidsHitByRayCasts(rayCasts: {RaycastResult}): {Humanoid}
local humanoids = {}
for _, rayCast in rayCasts do
local hit = rayCast.Instance
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then table.insert(humanoids, humanoid)
end
return unique(humanoids)
end