Script Not Finding Children

This script is supposed to find whether or not there is anything in a folder. The stuff inside the folder are number values. The script won’t respond at all. It doesn’t print anything on the output, or tell me any errors. Can’t seem to figure out why. Here’s the script

while true do
wait(.5)
if #ItemSave:GetChildren() > 0 then
    print("found")
elseif #ItemSave:GetChildren() < 0 then
    print("nothing found")
    end
end

Ridiculous suggestion; but considering no print appears, is your script currently disabled ?
Otherwise, try a more straightforward check : print(#script.Parent:GetChildren())

That condition will never pass. An object can never have less than 0 objects. You should probably just use an else there.

if #ItemSave:GetChildren() > 0 then
   print("Found")
else
   print("Nothing found")
end

Due to the conditional logic, the else block will be used if there are 0 children.

2 Likes

Thanks for the suggestion but I’ve tried that earlier and it didn’t work.

How are the Instances added to it, in studio?


FYI: The ChildAdded/ChildRemoving events can be used here instead of a ineffecient while loop.

Is that the whole script? How are you referencing ItemSave?

Where in the game is the script located?

Try this (it’s working for me when I’m testing it)

local ItemSave = game:GetService("Workspace").ItemSave

while wait(.5) do
	if table.getn(ItemSave:GetChildren()) > 0 then
		print("Found")
	else
		print("Nothing")
	end
end

All told, @ReturnedTrue is right, using an event would be a better test for this. Here’s what the code would probably look like:

local ItemSave = game:GetService("Workspace").ItemSave
local Found = false --// Set this to false if there is nothing in the folder (whatever your default state is)

local function CheckValues()
	if table.getn(ItemSave:GetChildren()) > 0 then
		Found = true
		print("Found is true")
	else
		Found = false
		print("Found is false")
	end
end
ItemSave.ChildAdded:Connect(function()
	Found = true
	print("Found is true")
end)

ItemSave.ChildRemoved:Connect(function()
	CheckValues()
end)
3 Likes

In a ScreenGUI in the StarterGUI. The script creates a new box whenever an Item is added to the Player’s ItemSave. I’m helping OP (im his friend) since he’s unable to respond in time due to personal issues.

1 Like

The folder is located in the player and a test value is created and parented into that folder.
Also ChildAdded and ChildRemoved are not working for some reason, is this right?

ItemSave.ChildAdded:Connect(function()
    print("child added")
end)

ItemSave.ChildRemoved:Connect(function()
    print("child removed")
end)

Can you post a screenshot of the object hierarchy (where the script and folder are in the explorer)?


1 Like

Solution was found, it was actually something else erroring but for whatever reason wasn’t being shown through output, thanks for all your contributions

2 Likes