For loops and GetChildren() Help

Hi guys, I have a kill manager which should get each kill part from each map so that the code is not repeated for every part, could you guys help me find out what is wrong with it, it doesn’t print any errors or “touched”


for i,b in ipairs(a) do
	for i,v in ipairs(b:GetChildren()) do
		for i,x in ipairs(v.Killers:GetChildren()) do
			x.Touched:Connect(function(Object)
				if Object.Parent:FindFirstChild("Humanoid") then
					print("touched")
				end
			end)
		end
	end
end

image
Kill parts are inside each model

2 Likes

Are you checking if players have touched the model or the parts inside the model? Since you have to check if they have touched the parts.

can you show the ‘Maps’ folder ancestors and the Lava, Rocks, Spikes descendants

It’s in the code I provided, make sure you have read it

local Maps =  game.Workspace.Maps
local Map1 = Maps.Volcano
local Killers = Map1.Killers

for _,model in pairs(Killers:GetChildren()) do
    for _,child in pairs(model:GetChildren()) do
       child.Touched:Connect(function(object)
          if object.Parent:FindFirstChild("Humanoid") then
          --do code
          end
       end)
    end
end

From what I can see here, I assume you have killing parts in each of these folders.

The maps folder is in the workspace, the descendants for those are just parts that’s it

From the code you have provided it seems you are checking if players have touched the models, you would need another for loop to check inside every model for parts.

Killing parts are only in the killer folder inside the models

This wouldn’t be useful, if I have 30 maps I would have to make map1 map2, that is why I am using a loop

Then just make the loop go through each map. Simple.

Oh, I think I would need to make another loop to GetChildren() of the models, let me try that, completely forgot about it

That’s what I made, I don’t know what you even read in my topic, that is what I made, I was asking why it wasn’t working not how to make it

local Maps =  game.Workspace.Maps

for _,map in pairs(Maps:GetChildren()) do
   for _,child in pairs(map.Killers:GetChildren()) do
      for _,descendant in pairs(child:GetChildren()) do
         if descendant:IsA("BasePart") then
             descendant.Touched:Connect(function(obj)
                if obj.Parent:FindFirstChildWhichIsA("Humanoid") then
                  print("A player has touched the brick!")
                end
             end) 
         end
      end
   end
end


Not useful at all, please read the information I provided, that is pretty much the code that I wrote

This should work shouldn’t it?

local a = game.Workspace.Maps:GetChildren()

for i,b in ipairs(a) do
	for i,v in ipairs(b:GetChildren()) do
		for i,x in ipairs(v.Killers:GetChildren()) do
			for i,p in ipairs(x.GetChildren()) do
				p.Touched:Connect(function(Object)
					if Object.Parent:FindFirstChild("Humanoid") then
						print("touched")
					end
				end)
			end
		end
	end
end

In this, p would be the parts inside the models in killer wouldn’t it? It still doesn’t work for some reason

local Maps =  game.Workspace.Maps

for _,map in pairs(Maps:GetChildren()) do
   for _,child in pairs(map.Killers:GetChildren()) do
      for _,descendant in pairs(child:GetChildren()) do
         if descendant:IsA("BasePart") then
             descendant.Touched:Connect(function(obj)
                if obj.Parent:FindFirstChildWhichIsA("Humanoid") then
                  print("A player has touched the brick!")
                end
             end) 
         end
      end
   end
end

You dont need the first loop, just put maps in the second loop.

local a = game.Workspace.Maps

	for i,v in ipairs(a:GetChildren()) do
		for i,x in ipairs(v.Killers:GetChildren()) do
			for i,p in ipairs(x.GetChildren()) do
				p.Touched:Connect(function(Object)
					if Object.Parent:FindFirstChild("Humanoid") then
						print("touched")
					end
				end)
			end
		end
	end

Thanks that works, isn’t mine the same, does mine not work because of ipairs?

1 Like

Xd

for _, Object in pairs(game.Workspace.Maps:GetDescendants()) do 
	if Object:IsA("BasePart") and Object:FindFirstAncestorWhichIsA("Model") then
		Object.Touched:Connect(function()
			
		end)
	end
end