ObjectValue and WaitForChild

I have a question about referencing an ObjectValue via WaitForChild in a LocalScript, such as:

local myObjectRef = script:WaitForChild("MyObjectValue")  -- An ObjectValue that references another object on the Client

Will this wait for the actual object to be loaded? Or is there ever a chance that this will return the object reference before the actual object is loaded?

It waits for the ObjectValue to load, not whatever ObjectValue.Value is. @kylerzong: Not only was that not what he’s asking, it also won’t error. It just gives you a warning if 5 seconds pass without it finding anything.

8 Likes

Oops, anyways you can increase the timeout by passing a second parameter if you need

:WaitForChild("", math.huge)

The short answer is no, WaitForChild makes it wait for the value to load before letting anything run on it. This helps prevents scripts from running when the actual value isn’t loaded, in return breaking.

My goal is to wait until the actual object is loaded, not just the reference to the object. Is there a way to know when the ObjectValue.Value is loaded?

In theory it is possible to “Wait” for the reference of an ObjectValue’s Value using a loop (don’t recommend it) or much better, utilizing the “GetPropertyChangedSignal” connection and its :Wait() method

--- Local script in ReplicatedFirst or ReplicatedStorage  ---
--- For this example I parented the object value to the script)

local ObjVal = script.ObjVal
print(ObjVal.Value)

--- Loop variant --- ( I don't recommend doing so as loops in general should not have to be used everywhere)
repeat task.wait(yourintervalhere) until ObjVal.Value
while not ObjVal.Value do task.wait(yourIntervalHere) end
------------------------

--- I'd much rather recommended an event based construct, but be sure to handle cases of timing out on your own if needed ----
ObjVal:GetPropertyChangedSignal("Value"):Wait()
print("Object value loaded! Woop.", ObjVal.Value)

--- If you would like other code procedures to happen, wrap the call in task.spawn() ---
task.spawn(function()
	--- Time out stuff here and/or additional code if desired ---
	ObjVal:GetPropertyChangedSignal("Value"):Wait()
	--------------------------------------------------
	--ObjVal2:GetPropertyChangedSignal("Value"):Wait()
end)

You can see that the event does fire when the object (thus Value reference ) has loaded on the client
(Tested using StreamingEnabled with a max streaming radius of 100)

Some side notes:
For a more simple approach you need define the objectvalue’s value within studio (That way the server knows that the object already exists) so that the client simply waits for the object to be replicated and therefore fires the event.

If you place the ObjectValue in a shared container (such as ReplicatedStorage or Workspace) you can have the server create an instance and set the ObjVal’s value to that instance and then use a remote event to fire to the client, creating a ChangedSignal connection to wait for it to load if it doesn’t get it right away. (A system on paper, of course you’d have to make it yourself and this isn’t a tutorial of course.)

Do note that the ideas and approaches in my reply - I don’t know if they’re entirely correct or the best choice for performance. I also tested using streaming enabled as the process is practically the same for when a client receives a copy of an instance created on the server at any given time.

1 Like