Script is unable to find instaces

This script claims that the Pants, Shirt, or any other item being cloned from this snippet of code does not exist. This script is a serverscript and is accessing a folder in a folder in SS.

v.Character.Pants:Destroy()
v.Character.Shirt:Destroy()
					
local mtf = "Eta-10"
local give = game.ServerStorage.Uniforms:WaitForChild(mtf)

give:WaitForChild("Pants"):Clone().Parent = v.Character
give:WaitForChild("Shirt"):Clone().Parent = v.Character
				
give:WaitForChild("Vest"):Clone().Parent = v.Character
give:WaitForChild("Mask"):Clone().Parent = v.Character

mtf
outputaa

Does anyone know why it isn’t able to find the requested instances?

local give = game.ServerStorage.Uniforms:WaitForChild(mtf)

Do you need :WaitForChild(mtf)?

Wouldn’t that line be better as:
local give = game.ServerStorage:WaitForChild("Uniforms")
since your next lines have give:WaitForChild… in them as well

1 Like

anything without the waitforchild will just throw an error

A server Script in ServerScriptService has immediate access to everything that’s in ServerStorage at publish time, without needing any WaitForChild calls. If it errors, something is not located in the right place: Either you have another empty folder in there also called “Eta-10” (like from an accidental Ctrl-D duping), or your “Pants” is really named "Pants " (with whitespace) or something like this. Check all the paths, and if necessary, do print(give:GetChildren()) and see what’s there.

1 Like

realized where i screwed up, i have another model named Uniforms in SS for storing the uniforms on character models so it was going to the model instead of the folder

thanks for your time

1 Like

Having an immediate access to a storage do not mean everything in this storage has been loaded, scripts frequently start to run before most of the objects anywhere in the game are fully loaded which is why the use of WaitForChild is great for all “starting instance variables” to prevent errors… it cost nothing to do this:

local ServerStorage = game:GetService("ServerStorage")
local UniformsStorage = ServerStorage:WaitForChild("Uniforms")

--Access the folder in a safty state from anywhere in your script

In this case it is actually guaranteed by the system. By the time any Script in ServerScriptService runs, all instances inside of ServerStorage, ReplicatedStorage, and ReplicatedFirst that exist in these locations at authoring time (when the place is published) will exist and be able to be referenced by the scripts.

WaitForChild is still needed on the server for anything that is created or loaded at runtime, included instances created by other server Lua scripts, and instances that are created by C++ code (like spawning Characters).

I’m not sure about it, i tried to access storage instances without the use of WaitForChild multiple times in the past and while playing in-game (not studio), it was almost always giving me errors saying that the script didn’t found the instances… so from this time i started to always use WaitForChild to setup variables reference of all main instances of the different storages, then i never got theses errors anymore.

Please mark your post as the Solution so people don’t keep posting here to try solving it.
It also may help someone else if they are using the Search tool.

1 Like

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