Need help loading the game before firing a local script

So I have a couple of local scripts inside of starterPlayerScripts, and I am getting multiple errors when trying to access parts in the workspace: example local portal = workspace.Portal
but I am just constantly getting errors about the part not existing.

I tried looking for answers in the dev forum, and I tried a few code snippets such as repeat wait() until game:IsLoaded() but none of those worked.

Let me know if you know what I could do :slight_smile:

1 Like

Instead of directly accessing the parts, you would make use of :WaitForChild, which will wait until the part is loaded

i.e.
instead of

local portal = workspace.Portal

you would do

local portal = workspace:WaitForChild("Portal")

Oh, I don’t know how I didn’t think about that! Thank you very much! :smile:

Nvm, I got an infinite yield on the second test on the game, even tho all the children are in perfect order and with correct names, the portal refuses to load for the local script. Do you have any other solution?

Is the portal created by a script or is it already in the workspace/

It is already in the workspace, inside of a model

IG if a particular thing isn’t loading, you can use an alternative to wait for child:

local portal = nil
while not portal do
    portal = game.Workspace:FindFirstChild("Portal")
    task.wait()
end

A thing I do on local scripts or server scripts is at the top of the script

repeat task.wait() until script.Parent

Usually this gives enough time for the game to load or

if not game:IsLoaded() then game.Loaded:Wait() end

I managed to solve this myself by using some tips from other developers, the solution I found the most efficient was
repeat task.wait() until workspace:FindFirstChild("NAME") and workspace:FindFirstChild("NAME").

Which meant I could specify each script with their own waiting loop until the parts I needed for that code block were loaded.

But thanks to everyone for guiding me in the right direction!

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