GetChildren() sometimes returns nil i need a nil check help

yo yo yo waddup devforum,

basicly there is a folder in replicatedstorage filled with tools from died players, however if the player had no tools its basicly a empty folder.

so i get this error when the folder is empty, how can i add a nil check if i tried it, it broke the script it didnt work at all.

https://i.gyazo.com/354a05dd9f6d5180cc826047dab17256.png

for _, tool in ipairs(game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead.."'s Tools"):GetChildren()) do
            print("RANTHEIPAIRSLOOKINGFORFOLDERCHILDREN")
            if tool == nil then
                WarnGUIEvent:FireClient(player, "Looting", nameOfDead.."'s Body seems to be Empty.", 3, Color3.fromRGB(255, 0, 4))
                game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
            else
                tool.Parent = player.Backpack
                game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
                WarnGUIEvent:FireClient(player, "Looting", "You Succsessfully looted "..nameOfDead.."'s Body.", 3, Color3.fromRGB(255, 0, 4))
            end
        end

When you do a for loop for a getchildren in an empty folder, it doesn’t run at all; because there’s nothing to run for. To check if it’s ran, use a bool variable
local var = false
just put var = true right after the for loop, and if there is a child, it’ll turn the variable true, and if it’s not, then the variable will stay false

local var = false
for i,v in pairs(whatever:GetChildren()) do 
  var = true
end

if var then
  --children do stuff
else
  --no children do stuff
end

The script basically tries to look for something that doesnt exist, and expects to find something, naturally it will error, so you should utilize the FindFirstChild function a bit better

Here’s an example

local toolParent = tool.Parent
local toolName = tool.Name

if toolParent:FindFirstChild(toolName) then
     print("Tool Found")
end

Why would you ever do that?!? Use the length operator!

if #whatever:GetChildren() > 0 then
    -- at least 1 child
end

But the problem here is that, :FindFirstChild most likely returned nil, :GetChildren never returns nil. So check that first.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local tools = ReplicatedStorage:FindFirstChild(nameOfDead .. "'s Tools")

if tools then
    -- continue here
end
5 Likes

I never thought of that, holy moly. I’m wondering now why my brain didn’t think of that.

so ive run into something more complex,

the thing is when i completly removed any check and tried to parent it instantly. It worked only when the folder was empty i got the error saying nil.

the thing is that when i reset my tools disappear before even going into that folder, but when i kill myself with admin command my tools go into the folder

what in the roblox universe is going on, is reset deleting my backpack or sum ?

yea so this didnt work at all here is my code

still basicly my same problem however it didnt return any error however it still didnt work the print inside the loop never ran

local YOINK = false
		for _, tool in ipairs(game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead.."'s Tools"):GetChildren()) do
			print("RANTHEIPAIRSLOOKINGFORFOLDERCHILDREN")
			YOINK = true
			if YOINK == true then
				tool.Parent = player.Backpack
				game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
				WarnGUIEvent:FireClient(player, "Looting", "You Succsessfully looted "..nameOfDead.."'s Body.", 3, Color3.fromRGB(255, 0, 4))
			elseif YOINK == false then
				WarnGUIEvent:FireClient(player, "Looting", nameOfDead.."'s Body seems to be Empty.", 3, Color3.fromRGB(255, 0, 4))
				game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
			end
		end

hey atleast you brain can think somehow,

my brain cant think of all so i paid scripters but who cant think at all too so im stuck with this shi

Because you never did what incapaz said, check if the folder exists first.

yea im doing that rn i want to try all solutions i tried bens first cause he responded first.
my brain just had to think i came up with this now

local toolsfolder = RS:FindFirstChild(nameOfDead.."'s Tools")
		if toolsfolder then
			for _, tools in ipairs(RS:FindFirstChild(nameOfDead.."'s Tools"):GetChildren()) do
				print("RANTHEIPAIRSLOOKINGFORFOLDERCHILDREN")
				if tools > 0 then
					tools.Parent = player.Backpack
					game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
					WarnGUIEvent:FireClient(player, "Looting", "You Succsessfully looted "..nameOfDead.."'s Body.", 3, Color3.fromRGB(255, 0, 4))
				else
					WarnGUIEvent:FireClient(player, "Looting", nameOfDead.."'s Body seems to be Empty.", 3, Color3.fromRGB(255, 0, 4))
					game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
				end
			end
		end

uh i tried ur solution it still didnt work, did i do something wrong ?
https://i.gyazo.com/6d4c3488ac5c225b6a9f296644289615.jpg

i reseted once and i also used ingame command to kill me once, there are 2 folders but looting dosent work.
here is the full code from beginning

lootBodyBar.OnServerEvent:Connect(function(player)
	print("OhNoBeginns")
	if player.Character:WaitForChild("Humanoid").Health > 0 and game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, 20129787) or player.MembershipType == Enum.MembershipType.Premium or player.UserId == 192311210 or player.UserId == 111049139 or player.UserId == 125165008 or player.UserId == 30698801 or player.UserId == 73508343 then
		print("YeaUserisGucci")
		if player.PlayerGui:FindFirstChild("LootProgressGui") then
			player.PlayerGui:FindFirstChild("LootProgressGui"):Destroy()
		end
		print("yo")
		local toolsfolder = RS:FindFirstChild(nameOfDead.."'s Tools")
		if toolsfolder then
			for _, tools in ipairs(RS:FindFirstChild(nameOfDead.."'s Tools"):GetChildren()) do
				print("RANTHEIPAIRSLOOKINGFORFOLDERCHILDREN")
				if tools > 0 then
					tools.Parent = player.Backpack
					game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
					WarnGUIEvent:FireClient(player, "Looting", "You Succsessfully looted "..nameOfDead.."'s Body.", 3, Color3.fromRGB(255, 0, 4))
				else
					WarnGUIEvent:FireClient(player, "Looting", nameOfDead.."'s Body seems to be Empty.", 3, Color3.fromRGB(255, 0, 4))
					game:GetService("ReplicatedStorage"):FindFirstChild(nameOfDead .. "'s Tools"):Destroy()
				end
			end
		end
	else
		print("YeaUserAintGucci")
		player.PlayerGui:FindFirstChild("LootProgressGui"):Destroy()
		WarnGUIEvent:FireClient(player, "Nice Try", "Nice try bro, looks like wont work", 3, Color3.fromRGB(255, 0, 4))
	end
end)

and these are the prints i receive
https://i.gyazo.com/8af3d3d37743d48faa1dc0cd292fbdf9.png