Folder parented to ModuleScript sometimes not found? (PlayerGui)

In my game, there’s a really weird bug where a cloned Frame from ReplicatedStorage to the PlayerGui ends breaking for a small percentage of user’s. Personally I’ve never experienced this issue, and I don’t know what’s causing it

For breaking the meaning; A folder is not being found

This issue shouldn’t be related to other scripts in the game
I have looked up many similar posts but unfortunately nothing helped.
Really appreciate any help! Thanks

image-51

Yield is happening in ModuleScript

Content of localscript

local TweenService, tweenInfo = game:GetService("TweenService"), TweenInfo.new(.15, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Definition = require(ReplicatedStorage.Modules.Pets.Definition)
local ItemsLib = require(ReplicatedStorage.Modules.Pets.Lib)

local PetsLib = require(script:WaitForChild("PetsLib"))

local gamepassLib = require(ReplicatedStorage.Modules.gamepassLib)
local ButtonAnimation = require(ReplicatedStorage.Modules.ButtonAnimation)

local player = game:GetService("Players").LocalPlayer

for _, item in pairs(PetsLib.Inventory::Types.inventory) do
    local asset = ItemsLib:assetById(item.Id)
    
    local template = PetsLib.Assets.Template:Clone()
    template.Name = asset.Name
    template.Body.Body.Text = `{asset.Boost}`
    template.Body.PetImage.Image = `rbxassetid://{asset.Image}`
    template.Placeholder.ImageColor3 = Definition[asset.Type].Color
    template.LayoutOrder = -asset.Boost*100
    template:SetAttribute("Boost", asset.Boost)
    template:SetAttribute("Gold", asset.Gold)
    template:SetAttribute("Void", asset.Void)
    template:SetAttribute("Id", item.Id)
    template:SetAttribute("UUID", item.UUID)
    template.Body.UIScale.Scale = 0
    template.Parent = PetsLib.Label.ScrollingFrame
    template.Body.MouseButton1Click:Connect(function()
        PetsLib:OnPetImageClick(template)
    end)

    TweenService:Create(template.Body.UIScale, tweenInfo, { Scale = 1 }):Play()        

    ButtonAnimation(template.Body)
end

PetsLib:UpdateScrollingFrame()
PetsLib:UpdateSort()

PetsLib.Label.ScrollingFrame:AddTag("PetFramesAdded")

local gamepassBigInventory = gamepassLib.gamepassByName("Big Inventory")
player.gamepasses[gamepassBigInventory.Name].Changed:Connect(function()
    PetsLib:UpdateScrollingFrame()
    PetsLib:UpdateSort()
end)

ModuleScript

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage.RemoteFunctions.Pets

local Lib = {}
Lib.Label = script.Parent.Parent
Lib.Assets = Lib.Label.InventoryScript.Assets
Lib.Bindables = Lib.Label.InventoryScript.Bindables
Lib.LastSelected = nil :: Frame?
Lib.Sort = "High"
Lib.MultiDelete = false
Lib.Inventory = remoteFunction:InvokeServer("getInventory")
Lib.Equipped = remoteFunction:InvokeServer("getEquipped")

for _, module in pairs(script:WaitForChild("Modules"):GetDescendants()) do
    if not module:IsA("ModuleScript") then continue end
    task.spawn(function() Lib[module.Name] = require(module) end)
end

for _, module in pairs(Lib.Label.InventoryScript.Interaction:GetChildren()) do
    task.spawn(require(module), Lib)
end

return Lib

Not gonna lie, I don’t know if I’m blind or something but I can’t find where you do WaitForChild on the module :sob:

1 Like

Can’t find it in the code either but judging by the output I think it’s because you’re using the required variable instead of something like script.PetsLib:WaitForChild(Modules)

1 Like

That should be the WaitForChild yielding, not sure tho. Error report doesnt give put the line nor
script

Posted the right code on the post, the module is yielding for some reason

Yeah my bad, the yield is somewhere else. I posted the module which yields on the post

1 Like

Try waiting for the ScreenGUI at the FIRST LINE of your script. That fixed it for me when I had similar issues.

repeat task.wait() until plr.PlayerGui:FindFirstChild("YourScreenGUI")

If that still doesn’t remedy the problem, then you’re experiencing the same issue I’ve had before where, only a few random players, would be infinite yielding a certain thing. In that case, I fixed it by just cloning the Asset I wanted to use from somewhere like ReplicatedStorage if the script yielded for more than 5 seconds.

I’ll consider trying this out, thanks :pray:

Do you know another solution other than storing it in ReplicatedStorage? And cloning stuff if its not found? It’s very strange that the folder appears to be missing sometimes. I changed
:WaitForChild(“Modules”) to :FindFirstChild(“Modules”) and now it throws an error

**Players..PlayerGui.MainGUI.InsertedGui.Pets.InventoryScript.PetsLib:14: attempt to index nil with ‘GetDescendants’

Script ‘Players..PlayerGui.MainGUI.InsertedGui.Pets.InventoryScript.PetsLib’, Line 14**

This is super weird considering the folder is supposed to be there always

Maybe it’s caused by the ModuleScript itself? As everytime the UI is loaded in the cloned modules are being required, maybe the required “PetsLib” is in an old state or thinking its nil-parented.

I will consider replacing PetsLib with a LocalScript, maybe that fixes the issue