Script Help FindFirstAncestorOfClass

local FoundPart = player.Character:FindFirstAncestorOfClass("Part").Name
			if FoundPart == "Ring" then		

This script in ServerScriptService

and the error --attempt to index nil with ‘Name’

It means that :FindFirstAncestorOfClass("Part") function couldn’t find an ancestor with ClassName property set to Part so it returned nil and rest of the line turned into nil.Name.

Unless you’re checking for any 3D part (which in that case you should switch :FindFirstAncestorOfClass("Part") with :FindFirstAncestorWhichIsA("BasePart")) you should check if :FindFirstAncestorOfClass("Part") is returning nil or not.

so how will it be. Can you send a sample if possible?

What are you trying to achieve here?

Sure here is 2 examples for each case:
nil check with :FindFirstAncestorOfClass("Part") to check for an instance with ClassName “Part”:

local FoundPart = player.Character:FindFirstAncestorOfClass("Part")
if FoundPart ~= nil and FoundPart.Name == "Ring" then

nil check with :FindFirstAncestorWhichIsA("BasePart") to check for an instance which is a 3D rendered object:

local FoundPart = player.Character:FindFirstAncestorWhichIsA("BasePart")
if FoundPart ~= nil and FoundPart.Name == "Ring" then

Reminder that :FindFirstAncestorOfClass("Part") only checks for an instance with ClassName property set to “Part” which only includes parts that are either blocks, balls or cylinders like you see on studio toolbar when creating a new part.

:FindFirstAncestorWhichIsA("BasePart") checks for any instance that is a 3D part which alongside with parts also includes meshparts, wedges, cornerwedges, unions and negativeparts.

Would you try it yourself if possible?because can’t detect

I just realized something that you’re trying to find an ancestor of player’s character model that is a “Part” which makes a lot more sense why this code doesn’t work for you now.

That will always return nil because player’s character model is parented to Workspace which is a service and parented to datamodel itself and neither of them are parts.

I have question for you. Are you trying to find a descendant that is named “Ring” instead of ancestor?

yes it is cloned into “player.Character”

Then you should just use :FindFirstChild("Ring", true) instead:

local FoundPart = player.Character:FindFirstChild("Ring", true)

if FoundPart ~= nil then

but ring is meshpart. Would it be a problem?

No because :FindFirstChild("Ring", true) tries to find a part that is named “Ring” that is a descendant, it doesn’t care about classes.

1 Like