Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I ignore a ray if another ray has the same target?

I am trying to make a combat system for my game. I decided on using raycasting to detect targets infront of the player, but I am having a problem where if two rays have the same target, the target will be hit twice.

		if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then return end
		if Blocking.Value == true then return end
		if Stunned.Value == true then return end

		local Animations = script.Animations

		local Hit1 = Humanoid:LoadAnimation(Animations.Kick1)
		local Hit2 = Humanoid:LoadAnimation(Animations.Kick2)

			--Raycasting
		local RayCastParams = RaycastParams.new()
		RayCastParams.FilterDescendantsInstances = {char}
		RayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
			--Settings
		local Eyes = char.HumanoidRootPart.CFrame.lookVector
		local RightVector = char.HumanoidRootPart.CFrame.RightVector
		local LeftVector = -char.HumanoidRootPart.CFrame.RightVector
		local Distance = 10
		local Damage = 12.5
			--Settings
		local RayHitBox1 = workspace:Raycast((char.HumanoidRootPart.CFrame).p, Eyes * Distance, RayCastParams)
		local RayHitBox2 = workspace:Raycast((char.HumanoidRootPart.CFrame).p, (Eyes + RightVector)*Distance, RayCastParams)
		local RayHitBox3 = workspace:Raycast((char.HumanoidRootPart.CFrame).p, (Eyes + LeftVector)*Distance, RayCastParams)

		local Rays = {RayHitBox1, RayHitBox2, RayHitBox3}
		local DetectedHumanoid = {}
		for i,v in pairs(Rays) do
			if v.Instance.Parent:FindFirstChild("Humanoid") then
				if v.Instance.Parent:FindFirstChild("Blocking") then
					DetectedHumanoid[#DetectedHumanoid+1] = v
				end
			end
		end

How could I detect if any of these 3 rays have the same target and then ignore one of them?

1 answer

Hi Ghost40Z

What I would do here is in the for loop, I would check if the instance of the ray is == to the others ray instance;

local Rays = {RayHitBox1, RayHitBox2, RayHitBox3}
local DetectedHumanoid = {}
for i,v in pairs(Rays) do
	if  v.Instance.Parent:FindFirstChild("Humanoid") and Rays[i] ~= v and Rays[i].Instance ~=  v.Instance then
		if v.Instance.Parent:FindFirstChild("Blocking") then
			DetectedHumanoid[#DetectedHumanoid+1] = v
		end
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.