Hello, I have a local script with a lot of “local…=…”. The problem is that I often receive errors because the object has not loaded yet.
I tried putting this at the start of my local script: repeat task.wait() until game:IsLoaded()
But it doesn’t work and I’m still getting errors.
Did you use :WaitForChild() ?
I think thats the only solution, otherwise you could just use ContentProvider and just loop through these objects
It’s sometimes possible that :WaitForChild waits too long, maybe because the object is too far from the spawn?
What i do, which is really just a preference, is i use ContentProvider: ContentProvider | Documentation - Roblox Creator Hub
I wait until everything has been loaded in, or a large portion of it, and after that i will fire my functions/modules.
Just so i know everything that is important has been loaded in.
Add this to the top of your LocalScript.
It is a basic solution to this issue that is commonly used.
--if the game is not loaded, then we get the .Loaded event and use the
--:Wait() method to wait until the game IS loaded.
if not game:IsLoaded() then
game.Loaded:Wait()
end
What is it exactly that’s causing the errors? Is it physical stuff in the workspace or is it stuff like modules etc?
I already tried this but it doesn’t work
These are physical elements such as a door for exemple
Thanks, I’ll try it tomorrow, I’m go to sleep
Ok, if the doors are too far away from the spawn and they’re being streamed out, and if disabling streaming isn’t an option, it might be a good idea to swap to a ChildAdded layout instead, so you can handle the doors when they’re added instead of just yielding indefinitely.
I don’t know how your code is laid out but generally it would look something like this:
local function handleDoor(door)
-- handle the door
end
-- 1. handle the doors that exist when this code is first run
for _, door in doors do -- `doors` would be an array containing the doors
handleDoor(door)
end
-- 2. handle any new doors
doorAddedSignal:Connect(handleDoor) -- doorAddedSignal would be a signal that fires whenever a door is added (say a ChildAdded signal or a CollectionService instance added signal)
Another alternative if it doesn’t matter whether or not a door is actually loaded in, would be to add in something like this before trying to reference a door
if door == nil then return end -- replace return with continue if in a for loop
Note: game:IsLoaded() doesn’t actually wait until the whole project is loaded to return true, it just returns true as soon as the project is open and starts loading things in, so you would need to do what @BobbieTrooper suggested if that’s a better method than adding existance checks everywhere you need a door