How do I find a child of a child?

I want my script to find an IntValue called “dmg” (short for “damage”) but I keep running into errors like this:

  04:54:38.726  Workspace.Fish.ProximityPrompt.Main:51: attempt to call missing method 'FindFirstChild' of table  -  Server - Main:51

here is a part of my code that I just need help with…

prox.Triggered:Connect(function(plr)
	local hook = plr.Backpack:GetChildren()

	for i = 1, #hook do
		print(i, hook[i].Name)
	end

	local StopGui = plr.PlayerGui.ScreenGui.StopFishing

	local FishingV = plr.FishingParts

	local dmg = hook:FindFirstChild("dmg")
end)
1 Like

You have two choices:

  1. Loop through “hook” table and do what you were doing with the FindFirstChild
  2. or, you can do plr.Backpack:FindFirstChild(“name”, true) for recursive search under Backpack.

The error you are experiencing is because “hook” is a table, a table does not have the function “FindFirstChild”, but its children do!
To correct it you have to loop through the children:

local dmg
for _, child in ipairs(hook) do
      dmg = child:FindFirstChild("dmg") -- check if dmg exists under the child
      if dmg then break end -- break out of the loop because we found dmg
end

-- use dmg variable/value

But it is easier to just use FindFirstChild recursive:

local dmg = Backpack:FindFirstChild("dmg", true) -- true for "recursive mode"
1 Like

Your code is not working because hook is the backpack’s children, which is a plain list. Use plr.Backpack:FindFirstChild() instead.

The issue is caused by calling :FindFirstChild on hook. This is because hook is not an object, but a table, which are used like lists to store values.

Here is what I suggest:

  • Remove the :GetChildren call after plr.Backpack when defining the hook variable
  • Remove this loop:
for i = 1, #hook do
	print(i, hook[i].Name)
end
  • You can replace it with:
print(hook:GetChildren())

If it looks like this:

It is because the output has “Log Mode” enabled:

Disable it to make it look like this:

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