Ignoring accessories in a raycast

When my raycast hits a accessory i get the character by going RayInst.Parent but the problem is if it hits an accessory the parent isnt the character its the accessory i have tried an if statement

if raycastResult.Instance.Name == "Handle" or raycastResult.Instance.Parent:IsA("Accessory") or raycastResult.Instance.Parent:IsA("Accoutrement") then

but the problem is that if the player has a big accessory on them, it will just straight up not hit them at all, i want the raycast to ignore accessories and find thats through the accessory

You could continue the raycast through the accessory as well as marking the accessory handle’s CanQuery to false to prevent future hits.

1 Like

how do you set accessory’s can query to false, is it a setting or do you need to loop through workspace and do it manualy

Personally I always just make the first accessory hit damage the player anyway, and then just set the accessory’s handle’s CanQuery to false in the same swoop. Obviously this isn’t perfect, but usually fits what I’m trying to do.

You can also listen to the CharacterAdded event and loop through a character’s children and set it then.

the problem is that i need to find the character from the accessories

Lot of ways you can do it.

First way is to pre-emptively make any accessory not respond to raycasts in the first place:

  1. Retrieve a freshly spawned character from CharacterAdded
  2. Get the character’s children with :GetChildren()
  3. If the child is an accessory, find the handle and set its CanCollide, CanTouch, and CanQuery to false

Second way is to reactively respond from the raycast:

  1. if raycastResult.Instance.Name == "Handle" and raycastResult.Instance:FindFirstAncestorWhichIsA("Accessory") then
  2. Optionally set the raycastResult.Instance’s CanCollide, CanTouch, and CanQuery to false to simplify future hits
  3. Then either do your logic on the character as if you hit them, or continue the raycast to see if it would hit a character’s limb

ill attempt the

raycastResult.Instance:FindFirstAncestorWhichIsA(“Accessory”) then

and see if it works out

You can add a server script in the StarterCharacterScripts folder and disable the CanQuery option from the handle of each Accessory the player’s character has. If you have manually made the accessories then disable their CanQuery option already in the studio. Otherwise, the following script will automatically do so to Roblox ones.

local chr = script.Parent
local plrs = game:GetService("Players")
local plr = plrs:GetPlayerFromCharacter(chr)

repeat
	wait()
until plr:HasAppearanceLoaded()

for i, v in pairs (chr:GetChildrent()) do
   if v:IsA("Accessory") then
      local hnd = v:FindFirstChild("Handle")
      if hnd then
          hnd.CanQuery = false
      end
   end
end
3 Likes