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.
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
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)
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.