Hi, I have an error in which my code doesn’t detect an object in Lighting. It works in play, but not when a server is present. It is multi-code so I need someone to message me or reply down here to help me work with it.
Im assuming you are trying to use a local script to get the object.
Put the object in replicated storage, nothing should be stored in lighting.
It is a lighting effect, I replicate it from ReplicatedStorage TO lighting and back.
Try putting a “wait()” at the top of your script.
Assuming you’re calling it from a LocalScript, use :WaitForChild() on the object
To better help, a snippet of code (or a repro) provided, along with additional problem details (code and lines where the error appeals) would be appreciated.
For now, so that your game can run while you figure out the error, you can wrap the code in a protected call.
local success, result = pcall(function () -- code end) if not success then print(result) end
It’s not really a good idea to rely on wait() from the Client’s end, this can be unreliable.
A better method would be:
if not Object then repeat wait() until Location:WaitForChild("ObjectName") end
You only need WaitForChild…
All he has to do is:
local object = Location:WaitForChild(objectName)
It will yield (or timeout) until the object is returned by the WaitForChild function
Oh really? I always thought WaitForChild() wouldn’t yield if an object was already in existence when it was called. Thanks for letting me know.
Yeah, WaitForChild is favorable in this situation. It automatically handles the “repeat wait() until” for you. Proper implementation is key though - until the object is found, the scope it was declared in will yield (the whole script if it’s not in a focused scope).
Normally I use WaitForChild when I know an object will eventually come into existence and the wait time should be low, or if I need to depend on an object and need to disallow the script from running until it exists.
Yeah, pretty much the same from me, I just didn’t know it automatically handled the repeat wait() until.
You can replicate lighting effects to CurrentCamera in Workspace
But there’s no reason to replicate it to lighting and back if you can just keep it in lighting and use remoteevents to change its property (“Enabled”)