FindFirstChild not working

I am making a local script where if the player clicks yes it should a print statement printing template but it prints nil meaning the template is not there even though it is there.
Screenshot 2022-07-01 173116

LOCAL SCRIPT

local Yes = script.Parent
local Inv = game.Players.LocalPlayer.PlayerGui:WaitForChild("Inventory")
local template = Inv.LowerInvFrame.ScrollingFrame:FindFirstChild("template")

while wait(2) do 
	print(template)
end


2 Likes

Also, where exactly do you have the script? And in this script it doesn’t show anything relating to a click function

1 Like
while task.wait(2) do 
	if template then
		print(template)
		
		template.Activated:Connect(function()
			print("ae")
		end)
	else
		print("no.")
	end
end

does the output says no

1 Like

The client doesn’t always load things in before it’s code is instiated as opposed to the server, so you have to use :WaitForChild

In you’re case, to make things much more easier to manage you can just use a custom recursive find function so you don’t have to call :WaitForChild for every child

local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

function Recursive_Find(Parent, ChildName, IsRecursive)
	local Child = Parent:FindFirstChild(ChildName, IsRecursive)
	while not Child do
		if Child and Child.Name == ChildName then break end
		Child = IsRecursive and Parent.DescendantAdded:Wait() or Parent.ChildAdded:Wait()
	end 
	return Child
end

local Inv = PlayerGui:WaitForChild("Inventory")
local template = Recursive_Find(Inv,"template",true)
2 Likes

Please correct me if I’m wrong, but roblox already has a built in recursive-find system:

Parent:FindFirstChild("Part",true)

Not for WaitForChild, recursive FindFirstChild will only return the most relative descendant that exists currently rather than wait for one

1 Like

The second parameter of FindFirstChild tells the program whether or not to search the children of the child. By recursive it means it will use FindFirstChild on every child object inside the parent to search for whatever you want.

Ah. I think I understand what you mean, sorry.

Sad thing there is no WaitForAncestor or WaitForDescendant.

1 Like

Yea, The output still says no, and I made sure that the Activated Event ran.