How do you detect multiple instances with the new Raycasting method?

So with the old method you could use this function to detect multiple instances.

OLD METHOD
function findAllPartsOnRay(ray)
	local targets = {}
	repeat
		local target = game.Workspace:FindPartOnRayWithIgnoreList(ray, targets)
		if target then
			table.insert(targets, target)
		end
	until not target
	return targets
end

But as you all know, Ray.new() was deprecated so how can I mimic this old functionality.

So that this firebreath attack can hit multiple dummies.

CODE I USED FOR SOME CONTEXT
while firebreathParticle:IsDescendantOf(workspace) do
	wait()
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {character}
	params.FilterType = Enum.RaycastFilterType.Blacklist
		
	local hit = workspace:Raycast(character.HumanoidRootPart.Position, character.HumanoidRootPart.CFrame.LookVector * 10)
		
	if hit and hit.Instance.Parent:FindFirstChild("Humanoid") then
		local target = hit.Instance.Parent
			
		target.Humanoid:TakeDamage(0.125)
	end
		
end
4 Likes

Raycasts can only return a single instance, not an array of instances.

I think your best bet is to send another raycast with the appropariate distance left and set the character you hit in a blacklist to get NPCs behind it.

It’s basically the same

local function findAllPartsOnRay(rayOrigin, rayVector)
	local targets = {}

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = targets

	repeat
		local raycastResult = game.Workspace:Raycast(rayOrigin, rayVector, raycastParams)
        if raycastResult then
			table.insert(targets, raycastResult.Instance)
            --You have to set FilterDescendantsInstances again, it's not a reference to the table it's a clone of it.
			raycastParams.FilterDescendantsInstances = targets
        end
    until not raycastResult

    return targets
end

9 Likes

Thank you so much! I’ll dissect that and lean how that solution works.

2 Likes