Problem making script wait until folder has an item

I am trying to make a NPC queueing system and have come across a problem, the way it works is it will at random take a till out of the folder and go to it, (if the till is being used it will take it out of the folder so other NPC’s cannot go to it) Although there comes a point where there are no tills left in the folder and an error will be produced instead, is there a way to wait until there is something in the folder?

			if #Folder:GetChildren() == 0 then
			repeat 
			wait(2)	
			until #Folder:GetChildren() ~= 0 do
				end
			else
--NPC Will move normally

You can use Instance.OnChildAdded:Wait()

Thanks I’ll give that a go now to see if it works.

#folder does not give you the amount of instances inside the folder, in your case “folder” is an instance, but you can’t just get the lenght of an instance. What you meant was probably “#folder:GetChildren()” this will work since “folder:GetChildren()” returns a table and by putting a hashtag (#) in front of it, you will get a number (number of contents) returned. Your script would also crash besides of that, you forgot to add in a “wait()” or better a “game[“Run Service”].Heartbeat:Wait()”.

This is probably the most performant and clean method. You should try that.

How would I implement Instance.OnChildAdded:Wait() into the code, I have very little knowledge of this.

Just replace the “Instance” with your folder.

Example:

-- do cool stuff
game.ReplicatedStorage.CoolFolder:OnChildAdded:Wait()
-- do even cooler stuff
local folder = workspace.folder -- change this to a reference to the folder

folder.OnChildAdded:Connect(function(child)
   print(child.Name)
end)

(an example of an implementation)

This is not what he meant, your script is just printing the name of new added children to a folder. You are just connecting an event and not waiting for one.