Attempt to call a nil value when looking for a folder inside of a folder?

Hi I am trying to add types of zombies to a list and it keeps telling me [12:15:10.252 - ServerScriptService.Main:162: attempt to call a nil value] when the script gets to

local regularZombies = mapZombies:WaitForChild("RegularZombies"):GetChildren()

Am I doing something wrong? Here is my code:

		if round.Value == 0 then 	--load a random map
			statustag.Value = "Loading New Map"
			timertag.Value = 0
			print('First round, loading new map')
			mapholder:ClearAllChildren()
			wait(4)
			local allmaps = maps:GetChildren()
			mapIndex = math.random(1, #allmaps)
			mapName = allmaps[mapIndex].Name
			newMap = allmaps[mapIndex]:Clone()
			newMap.Parent=mapholder
			newMap.Name = "Map"
			local zombiesFolder = Instance.new("Folder")
			zombiesFolder.Name = "ZombiesHolder"
			zombiesFolder.Parent = mapholder
			round.Value = round.Value + 1 
			for k, player in pairs(contestants) do
				local leaderstats = player:WaitForChild("leaderstats")
				local points = leaderstats:WaitForChild("Cash")
				points.Value = 0
			end
			wait(4)
		end
		
		-- Zombie Selection --
		
		local availableZombies = {}
		local mapZombies = serverStorageZombies:WaitForChild(mapName):GetChildren()
		local regularZombies = mapZombies:WaitForChild("RegularZombies"):GetChildren()
		local fastZombies = mapZombies:WaitForChild("FastZinbues"):GetChildren()
		local strongZombies = mapZombies:WaitForChild("StrongZombies"):GetChildren()

Looks like there’s a few errors on the following lines

local mapZombies = serverStorageZombies:WaitForChild(mapName):GetChildren()
local regularZombies = mapZombies:WaitForChild("RegularZombies"):GetChildren()
local fastZombies = mapZombies:WaitForChild("FastZinbues"):GetChildren()
local strongZombies = mapZombies:WaitForChild("StrongZombies"):GetChildren()

Firstly, you’re attempting to call WaitForChild on mapZombies which is a table (returned by GetChildren). I’m guessing what you actually want is:

local mapZombies = serverStorageZombies:WaitForChild(mapName)

Then, you later call WaitForChild(“FastZinbues”), should this be WaitForChild(“FastZombies”). Maybe you typed it too fast :thinking:?

Also, I’m guessing this is running on the server as you’re indexing ServerStorage. In which case, WaitForChild is unnecessary. You can directly index the child or use FindFirstChild instead.

Here’s what your amended code should look like:

local mapZombies = serverStorageZombies[mapName]
local regularZombies = mapZombies.RegularZombies
local fastZombies = mapZombies.FastZombies
local strongZombies = mapZombies.StrongZombies

(You may still want to call GetChildren on each of the zombie types if you want them as tables instead of folders).

1 Like