Filtering Character For Headshot / Bodyshot

Hello! I am making an FPS game using R6 characters. I have a problem in which my code cant tell if the shot should do headshot damage or bodyshot damage.

The problem is parts such as the Handler’s in character’s hats and other accessories. Since these accessories are not parented to the head how am I supposed to tell if I should do a body or head shot?

Is there a way for my raycast to ignore these parts or something?

Here is my current code:

local origin = self.mouse.UnitRay.Origin
local direction = self.mouse.UnitRay.Direction
			
local ignore = {self.character, self.camera, self.weapon	
local ray = Ray.new(origin, direction * self.settings.firing.distance)
local hit, hitPos = workspace:FindPartOnRayWithIgnoreList(ray, ignore)

if hit then
    local partParent = hit:FindFirstAncestorWhichIsA("Model")
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        if hit.Name == "Head" then
	    print("headshot")
        else
	    print("bodyshot")
    end
end

The best solution to this, would be to ignore the accessories as you mentioned, and Raycast through to the Head/Body.

Here is an example of one method of doing this:

However, if you are trying to follow roblox’s wishes, you should probably use RaycastParams and WorldRoot:RayCast(), since FindPartOnRayWithIgnoreList() is not preferred by roblox anymore.

An alternative method to the one in the post I linked, would be to do the opposite and whitelist only parts that are children of characters.

1 Like