:FindFirstChild() not finding things that are in folder

So i’m making world generation and basically it’ll pick a random position on the map, get the terrain material, and then place something down if there is a folder for the material. Here is the code:

local function place()
	for i = 1, maxspecimens, 1 do
		local x = rnd:NextInteger(mapz, mapx)
		local z = rnd:NextInteger(mapz, mapx)
		local ret = findterrainY(x,z)
		if ret then
			local pos = ret[1]
			local mat = ret[2]
			if ss.Stuff:FindFirstChild(mat) then
				print("ghibble")
				local folder = ss:FindFirstChild(mat):GetChildren()
				local item = folder[math.random(#folder)]
				local billy = item:Clone()
				local posi = pos + Vector3.new(0,3,0)
				billy.Parent = workspace:WaitForChild("Stuff")
				billy:PivotTo(CFrame.new(posi))
			else
				print("yeah yeaaah yeah")
			end
		end
	end
end

place()

This is what ss.Stuff looks like:
image
Help would be appreciated

Also just to clarify, everything else in the script is operating as normal.

is mat an Enum? Because FindFirstChild uses strings to find the first instance with the name equal to the string. Enums will not work.

What you are doing is using a Material EnumItem as the argument for :FindFirstChild(). However, you should actually be using a string instead. You can convert it to a string by doing the following:

local stringifiedEnumItem = tostring(Enum.Material.Grass)
print(stringifiedEnumItem) --> Enum.Material.Grass

I also notice you have inconsistencies with where you are looking for this material folder:

The first check is doing it in the proper place, but the second one is not.

In summary, you need to convert your material enum to a string and ensure you are using :FindFirstChild() on the correct instance. It should look something like this:

local stringifiedMaterialEnum = tostring(mat)
local materialFolder = ss.Stuff:FindFirstChild(stringifiedMaterialEnum)
if materialFolder then
	local itemsWithinFolder = materialFolder:GetChildren()
	...
end
1 Like

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