Trying to check if part is related to player

How would I identify if the part im aiming at is a body part or an accesory? Ive tried different methods such as checking parts parent but it didnt quite work.

1 Like

What code were you using for this? In what way did it “[not] quite work”?

1 Like

I found a similar topic and tried to use methods from there: How do I check if a part is part of ANY player?

And by “did not quite” work I meant it didn’t work at all. Sorry a bad habit of mine to add “quite” everywhere possible.

1 Like

Check maybe for Part:IsA(“Accessorry”) or Part.Parent:IsA() and also check if Part.Parent == PlayerCharacter, i don’t know how you’re code looks like but it shoud do the work

2 Likes

Assuming your game doesn’t modify how accessories are handled, to check if a part is a limb or not you can just do:

local IsLimb = Part.Parent:IsA("Model")

There’s more ways but this just checks if the part is a direct descendant of the character itself rather than inside of an accessory object.


If you want to filter out accessory objects from detection from raycasts and other queries, turn off CanQuery in the accessory handle properties.

1 Like

The “PlayerCharacter” is something that really exists or is something that I have to get manually (or do I just check if parent is “Character”)? Because I don’t understand how I would do that. Also, I have tried doing if part:IsDescendantOf(game.Players) but it didnt quite work.

What if I have a model that is not a player character? That probably would not be good, as im planning to have multiple different models player must interact with.

Use tags to determine if a model is whether a player or npc.
Like Char:AddTag(“Player”), and npcChar:AddTag(“NPC”)

Char:AddTag("Player")
npcChar:AddTag("NPC")
-- so you'll be able to check
local isPlr = Char:HasTag("Player")
local isNpc = Char:HasTag("NPC")

-- also, to easily get the root model use :FindFirstAncestorOfClass("Model")
local Char = detectedPart:FindFirstAncestorOfClass("Model")

That’s pretty much it!

I was wondering if this could work:

local players = game.Players
local characterParts ={}

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		for i, part in pairs(character:GetChildren()) do
			table.insert(characterParts, part)
		end
		print(characterParts)
	end)
end)

I don’t see any reason for this, as you can access parts directly by indexing the character.

If you are using raycast, just make sure to set correct RaycastParams. You can set FilterDescendants as a folder where characters are parented. If the characters aren’t parented to a folder, I’d suggest making a folder called something like “Entities”, and parent all characters inside it.

The problem is that your first solution adds a tag to a caracter, and it would mean that I first would need to check if part is parented to that character, but as I told already i have not managed to make this work. I need to check if the part is parented to character, and not if model is player or an NPC, and the problem is that I can’t just set a specific character, I need to check if its ANY character.

You might be overcomplicating it, I don’t quite get what’s the issue. You assign tags when characters spawn in. When you have a part in any kind of detection e.g. raycast, some hitbox, .Touched etc you use detectedPart:FindFirstAncestorOfClass(“Model”) and get the character model. Simple as that.

1 Like

So basically, I add a tag whenever player joins a game, and when the time comes to check, I find the model that the part is inside of, and if that model has a tag, something happens. Is this what you are suggesting?

Pretty much. But not exactly when a player joins, but rather when character spawns. Use CharacterAdded for this.

I guess i will give it a try keystrokes, keystrokes

To get player character just get it by “game.Player[PlayerNickname].Character” and you will receive this player character, also make some checks
For example:

local player = game.Players.hinkalka230
if player.Character then 

if your script will hit player leg, torso or anything like that then Part.Parent will be player character, soo check if not Part:IsA(“Accessory”) or if its a part or something like that.
Basicly Player.Character gives you game.Workspace.PLAYERNAME - model of a player character by default in Workspace

I made a quick script for you, also i dont know if it would help but better to send it than not to

local Player = game.Players:WaitForChild("LlgPl") -- JUST GETING MY PLAYER FOR EXAMPLE

Player.CharacterAdded:wait()

local PlayerCharacter = Player.Character

if PlayerCharacter then
	local ExampleHitPart = PlayerCharacter:WaitForChild("Professional Glasses").Handle -- PART THAT YOU HIT WITH YOUR EVENT
	
	if ExampleHitPart.Parent == PlayerCharacter and ExampleHitPart:IsA("MeshPart") then -- Typical R16 body parts are MeshParts
		print("Hit part was indeed a body part")
	elseif ExampleHitPart.Parent:IsA("Accessory") or ExampleHitPart:IsA("Accessory") then
		-- KEEP IN MIND THAT Accessories have Handle inside of them that is also a MeshPart
		print("Hit part was an accessorry")
	else
		-- If not a body part or an accessory, then it's a tool or something else, you can also make if ExampleHitPart.Parent:IsA("Tool") or something like that
	end
end

-- Better to check by printing parts that are beeing hit and checking for the logic to make, if your current script will not handle it, just add a new logic

I hope i’ve helped

This did not work. Despite player model having a tag, I can still not reach the desired behaviour

*Nevermind, I have forgotten to insert something to make it actually work. This helped me

This means I would need to get individually character of each player, which does not seem ideal

then just check

if HitPart.Parent:FindFirstChild("Humanoid") then
    print("You just hit a player")
end

no need to know if its a player model, you can do it like that.
If you have in your game NPC’s or other things with Humanoid then just check
1:

if HitPart.Parent:FindFirstChild("Humanoid") then
    -- You hit something with humanoid
    if game.Players:FindFirstChild(HitPart.Parent.Name) then
        print(HitPart.Parent.Name.. " got hit")
    end
end

or you can check if its a player in other way, and way safer:
2:

if HitPart.Parent:FindFirstChild("Humanoid") then
    -- You hit something with humanoid
    for _, player in pairs(game.Players:GetPlayers()) do -- make a list of players
        if player.Character then -- Check if player has Character
            if HitPart.Parent == player.Character then -- Compare model that have been hit with player character
                print(player.Name.. " got hit")
            end
        end
    end
end

also you can make it without for loop:
3:

if HitPart.Parent:FindFirstChild("Humanoid") then
    -- You hit something with humanoid
    local Player = game.Players:FindFirstChild(HitPart.Parent.Name) -- find a player with HitPart.Parent.Name nickname
    if Player and Player.Character then -- check if found a player and if has character
        if Player.Character == HitPart.Parent then -- check if Player character is the same as HitPart.Parent
            print(Player.Name.. " got hit")
        end
    end
end

the last option will be the best i think, no need to loop and should work fine too, when you combine it with my previous script it will work even better

1 Like