Having trouble ignoring accessories on raycast, and ignoring team members on a certain team

Recently I finished a script that’s very close to being finished. I have everything that I want, though I’ve ran into 2 issues, and I can’t seem to solve them as I’m relatively new to scripting.

1) Player accessories will block the raycast.
For example, if a player was wearing a hat or some sort of back items, the raycast will collide with the accessory instead of dealing damage. I haven’t figured out a way to ignore that, even with some tinkering with the code.

2) Team members take damage.
I got lost when trying to make sure team members can’t take damage from an allies raycast. I tried many things, such as attempting if statements and whatnot but unless I’m doing something horribly wrong (which is probably the case :woozy_face:) then it’ll either deal no damage, or continue damaging an ally.

Here’s the code I currently have so far. It works as is, but as I stated, I’m not sure on how I can fix the issues I’m running into above.

(also this is my first post on the devforum. I apologize for my lack of professionalism! :sweat_smile:)

Thank you in advance!

local RANGE = 75
local DAMAGE = 15
local HEADSHOT = 30

local Team = game.Teams

game.ReplicatedStorage.RemoteEvents.RPFired.OnServerEvent:Connect(function(Player, TargetLocation, Handle)
	if Player.Character == nil then -- Make sure the character exists!
		return
	end
	
	Handle.Sound:Play()
	
	local Beam = Instance.new("Part", workspace)
	Beam.BrickColor = BrickColor.new("Sea green")
	Beam.Material = "Neon"
	Beam.FormFactor = "Custom"
	Beam.Transparency = 0.25
	Beam.Anchored = true
	Beam.CanCollide = false

	
	--Maths :(
	local Distance = (Handle.Position - TargetLocation).magnitude
	Beam.Size = Vector3.new(0.45, 0.45, Distance)
	Beam.CFrame = CFrame.new(Handle.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)
		
	game.Debris:AddItem(Beam, 0.2)
	
	--Raycasting
	local NewRay = RaycastParams.new()
	local RayDirection = (TargetLocation - Handle.Position) * RANGE -- how far you want the ray to travel
	
	NewRay.FilterDescendantsInstances = {Player.Character}
	
	local Result = workspace:Raycast(Handle.Position, RayDirection, NewRay)
	
	if Result then -- if we got result back
		if Result.Instance then
			-- Something was hit
			
			
			if Result.Instance.Parent:FindFirstChild("Humanoid") then
				Result.Instance.Parent.Humanoid.Health -= DAMAGE
				elseif Result.Instance.Parent:FindFirstChild("Head") then
					Result.Instance.Parent.Humanoid.Health -= HEADSHOT
				end
			end
		end
	end)

Answer 1:
You need to add a filter type to the raycast.

NewRay.RaycastFilterType = Enum.RaycastFilterType.Blacklist

Answer 2:
You need to ignore the raycast result if it hits someone with the same team color.

if not Result.Instance.Parent:FindFirstChild("Humanoid") then -- return if result is not a player
	return
end

if Player.Team == game.Players:GetPlayerFromCharacter(Result.Instance.Parent).Team then
	return
end
1 Like

The best way I’ve found to ignore accessories is to set them to a CollisionGroup that does not collide with anything. You must of course do this every time a player respawns. In fact, you could create different CollisionGroups that do not collide with themselves for different teams and specify those in your RaycastParams (e.g. NewRay.CollisionGroup = “Team1”). This would make your raycasts entirely ignore friendly players.

3 Likes

Thanks a bunch!
While I’m still having problems with the accessories, I’m sure I’ll figure it out eventually myself as I’m not too familiar with Blacklisting raycasts (if that makes sense).

Team members don’t take damage anymore, and now I can use that as a reference for future scripts that use a form of raycasting.
Cheers for that!

But, I thought that only baseparts can be set to a collision-groups. Not accessories.

Accessories all have MeshParts in them named Handle, and you can set the CollisionGroupId property of those to whatever you want.

Some examples from my character

picture
picture

2 Likes