How to easily check that an Object down-a-chain exists?

Hey all
I’ve spent so many hours of my life just writing out if statements to check if an object deep in GUI or in a Character actually exists.

Check:

  • Character exists
  • HumanoidRootPart exists
  • Gui exists
  • Gui element
  • etc…

Surely theres a way to write it like this in 1 line, somehow?
Did someone make a module, perhaps? Hmm

Ideal scenario:
if check(Character).check(“HumanoidRootPart”).check(“MyGui”)… then

2 Likes

Can’t you just use the recursive parameter of FindFirstChild? It’s basically a FindFirstDescendant.

I think this belongs in #help-and-feedback:code-review. you also don’t need to do one line as I’m not sure it’s possible without making a module script or making it super long. You can instead make it more readable by doing this

local function reviveUpdate()
	local player = Players:FindFirstChild(folder.Name)
	
	if not player and not player.Character then return end
	player.Character:WaitForChild("Humanoid", 5) -- don't know the context

	local humanoid = player.Character:FindFirstChild("Humanoid")
	local root = player.Character:FindFirstChild("HumanoidRootPart")
	
	if not humanoid or not root then return end
	
	local healthTag = root:FindFirstChild("HealthTag")
	
	if not healthTag or healthTag:FindFirstChild("ReviveFrame") then return end
	
	local reviveFrame = healthTag:FindFirstChild("ReviveFrame")
	
	if folder.Game.ReviveMode.Value then
		reviveFrame.Visible = true
	else
		reviveFrame.Visible = false
	end
end

also reminder you can paste your code and format it in devforum

Unfortunately, we often can’t use a “return” statement in the middle of the code, cause we might need to do something after the if-statement as well.

The single line statement would be awesome. Ill probably write a module script :rofl:

2 Likes

Sorry, didn’t know the context. But it shouldn’t be too hard to write a module for this

if you want to do it in 1 single line you can do what @Vanniris Stated by setting the recursive parameter to true on FindFirstChild()

Example:

local Object = Player.Character:FindFirstChild("ReviveFrame", true)

This will return the object if it exist or it will return nil if it doesn’t.

What if there happens to be another object named XYZ that isnt the exact Path you wanted? Seems problematic

If that’s the case you’ll have to write a custom function.

1 Like

Shoutout to this guy. This seems to be the answer. Awesome function

Heres my 1 line function now :heart_eyes:

if Find(player.Character, "HumanoidRootPart", "HealthTag", "ReviveFrame") then
 -- valid
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.