How to make Script scan childs in a folder to see if the child has a child named something?

Hello, i want my code to scan the childs of a folder to see if the child of that folder has a child named “Something”. So then it can do something to that supposed “GrandChild?”. but i dont know how.
i have 10 plots and i want to check if each of those plots have a part/model in it so that it can turn its transparency and cancollide to 0 and true.
here is my local script code:

script.Parent.SelectPlotFrame.Visible = false

local plots = workspace.Plots

local plotButton = script.Parent:WaitForChild("PlotButton")

local selectingPlot = false

local camera = workspace.CurrentCamera




plotButton.MouseButton1Click:Connect(function()

	local ownedPlot = nil

	for i, plot in pairs(plots:GetChildren()) do

		if tonumber(plot.Owner.Value) == game.Players.LocalPlayer.UserId then

			ownedPlot = plot
		end
	end

	if ownedPlot then

		game.ReplicatedStorage.PlotSystemRE:FireServer("removePlot")

		plotButton.Text = "PLOTS"

		selectingPlot = false
		script.Parent.SelectPlotFrame.Visible = false

	else

		selectingPlot = not selectingPlot

		if selectingPlot then

			camera.CameraType = Enum.CameraType.Scriptable

			local plotsToSelect = {}

			for i, plot in pairs(plots:GetChildren()) do

				if plot.Owner.Value == "" then

					table.insert(plotsToSelect, plot)
				end
			end

			local currentPlot = 1

			script.Parent.SelectPlotFrame.PlotName.Text = plotsToSelect[currentPlot].Name

			camera.CFrame = CFrame.new(plotsToSelect[currentPlot].Position + Vector3.new(0, 70, 0) + plotsToSelect[currentPlot].CFrame.LookVector * 50, plotsToSelect[currentPlot].Position)

			script.Parent.SelectPlotFrame.LeftArrow.MouseButton1Click:Connect(function()

				currentPlot -= 1

				if currentPlot == 0 then currentPlot = #plotsToSelect end

				script.Parent.SelectPlotFrame.PlotName.Text = plotsToSelect[currentPlot].Name

				camera.CFrame = CFrame.new(plotsToSelect[currentPlot].Position + Vector3.new(0, 70, 0) + plotsToSelect[currentPlot].CFrame.LookVector * 50, plotsToSelect[currentPlot].Position)
			end)

			script.Parent.SelectPlotFrame.RightArrow.MouseButton1Click:Connect(function()

				currentPlot += 1

				if currentPlot == #plotsToSelect + 1 then currentPlot = 1 end

				script.Parent.SelectPlotFrame.PlotName.Text = plotsToSelect[currentPlot].Name

				camera.CFrame = CFrame.new(plotsToSelect[currentPlot].Position + Vector3.new(0, 70, 0) + plotsToSelect[currentPlot].CFrame.LookVector * 50, plotsToSelect[currentPlot].Position)
			end)
			script.Parent.SelectPlotFrame.Visible = true

		else

			camera.CameraType = Enum.CameraType.Custom
			script.Parent.SelectPlotFrame.Visible = false
		end
	end
end)

script.Parent.SelectPlotFrame.SelectPlotButton.MouseButton1Click:Connect(function()

	game.ReplicatedStorage.PlotSystemRE:FireServer("buyPlot", script.Parent.SelectPlotFrame.PlotName.Text)
end)



game.ReplicatedStorage.PlotSystemRE.OnClientEvent:Connect(function(instruction)

	plotButton.Text = "P"

	selectingPlot = false
	camera.CameraType = Enum.CameraType.Custom
	script.Parent.SelectPlotFrame.Visible = false
	script.Parent.Enabled = false

end)

my second normal script:

local plots = workspace:WaitForChild("Plots")


for i, plot in pairs(plots:GetChildren()) do

	local ownerValue = Instance.new("StringValue", plot)
	ownerValue.Name = "Owner"
end


local re = game.ReplicatedStorage:WaitForChild("PlotSystemRE")


re.OnServerEvent:Connect(function(plr, instruction, plotName)

	if instruction == "removePlot" then

		local playerPlot = nil

		for i, plot in pairs(plots:GetChildren()) do

			if tonumber(plot.Owner.Value) == plr.UserId then
				playerPlot = plot
			end
		end

		if playerPlot then

			playerPlot.Owner.Value = ""
		end

	elseif instruction == "buyPlot" then

		if plots:FindFirstChild(plotName) and plots[plotName].Owner.Value == "" then

			plots[plotName].Owner.Value = plr.UserId

			re:FireClient(plr)

			plr.Character.HumanoidRootPart.CFrame = plots[plotName].CFrame + Vector3.new(0, 10, 0)
		end
	end
end)


game.Players.PlayerRemoving:Connect(function(plr)

	for i, plot in pairs(plots:GetChildren()) do

		if tonumber(plot.Owner.Value) == plr.UserId then
			plot.Owner.Value = ""
				

				
		end
	end
end)

here is a picture:
image

1 Like

You could use GetDescendants on the plots folder, it will return a table of all children, ‘grandchildren,’ etc. of the plots. You would want to loop through the table, and see if there is a part named “Something” and do something to that part.

local descendants = plots:GetDescendants()
for i=1, #descendants do
    local part = descendants[i]
    if part.Name == "Something" then
        --do something
    end
end
1 Like
local function findFirstDescendant(parent, descendantName) 
     local found
     for _, item in ipairs(parent:GetDescendants()) do 
         if item.Name == descendantName then found = item; break end
     end
     return found
end

You can call it like

local descendant = findFirstDescendant(workspace, "Part"); -- nil if not found

In your case, the first argument would be the plots folder and the second would be the name of the child

You need to use FindFirstChild recursion.
Example:

local part = plot:FindFirstChild("Name", true)

This searches recursively in your model/folder (or whatever you have) and if found you will get that part, otherwise nil

It works but i dont know if i stated that i want the part to become visible ONLY when the plot is selected.
because for example, a part i want to become visible is it part9, when i join part9 it becomes visible, but it also becomes visible when you join plot8 7 6 ect. how do i make it so the part only becomes visible when that specific part/plot has been joined

1 Like

Oh I see. Then you would probably want to only call GetDescendants on the plot that you have joined/selected.

So instead of plots:GetDescendants() you would use your selected plot and call GetDescendants on it.

1 Like

Im new to studio and my knowledge is limited, can you show me how to do it?

local plot = game.Workspace.Plots.Plot1 --For example, take 1 plot
for _, v in ipairs(plot:GetDescendants()) do
	if v.Name == "Name" then
		--action
	end
end
2 Likes

yeah but how do i make it so the game automaticly checks which plot has been selected so it does that

because i think if i do that code when you join a plot it automaticly does plot1 whilst you did plot9.

You need to store the playerplots somehow, either via Attribute or tables

1 Like

whats a attribute?
ik what a table is its for example {–blahblahblah–}

Attribute: Instance Attributes | Roblox Creator Documentation
You can assign your plot to a player via an attribute

Or create a Value (Object or Name) in the plot, where value is the player or the nickname of the player who owns the plot will be.

Or through tables

local playerplots = {}
playerplots["PlayerName"] = "Plot1"
print(playerplots["PlayerName"]) --> "Plot1"
playerplots["PlayerName"] = nil
print(playerplots["PlayerName"]) --> nil
2 Likes

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