How do i :GetChildren() a child?

so i have to get the “buy” button in my frames, how do i do that?

script below only takes the FRAMES, not THEIR CHILDREN
i wanted to do ScrollingFrame.Invicibility:GetChildren(), ScrollingFrame.[“High Speed”]:GetChildren(), etc. at first, but it didnt work :confused:

for _,mutatorBtn in pairs(script.Parent.ScrollingFrame:GetChildren()) do -- struggling on this one help lol
	if not mutatorBtn:IsA("TextButton") then continue end
	mutatorBtn.MouseButton1Click:Connect(function()
		if Player.leaderstats.Coins.Value < (tonumber(mutatorBtn.Price.Value)) then return end
		mutatorEvent:FireServer({Name = mutatorBtn.Name, Price = tonumber(mutatorBtn.Price.Value)})
	end)
end

image

What are you using continue here?

You can have a for loop inside a foor loop like this:

for _, child in pairs(something:GetChildren()) do
        for _, childOfChild in pairs(child:GetChildren()) do
            --This will loop through every child of every child of "something"
        end
    end
end

It seems like you are trying to fire the same event with different arguments to buy an item using a button.

I recommend using CollectionService and loop using :GetDescendants()

1 Like

Assuming each instance has the same structure (each child is a Frame which has a child called ‘Buy’ which is a TextButton), it would be best to directly get this Instance, as opposed to loop to find it.

for _, frame in pairs(<ScrollingFrame>:GetChildren()) do
	if frame:IsA('Frame') and frame:FindFirstChild('Buy') then
		-- Connect your MouseButton1Click signal here and the remaining code
	end
end
local tagName = "buyingButtons"
local CollectionService = game:GetService("CollectionService")
local Player = game:GetService("Players").LocalPlayer
local mutatorEvent = nil -- where your event is

-- Reducing if statements is unnecessary

CollectionService:GetInstanceAddedSignal(tagName):Connect(function(button)
	button.Activated:Connect(function()
		if Player.leaderstats.Coins.Value >= (tonumber(button.Price.Value)) then
			mutatorEvent:FireServer {
				Name = button.Name, 
				Price = tonumber(button.Price.Value)
			}
		else
			print("not enough coins")
		end
	end)
end)

for _,mutatorBtn in ipairs(script.Parent.ScrollingFrame:GetDescendants()) do
	if not mutatorBtn:IsA("TextButton") then 
		CollectionService:AddTag(mutatorBtn, tagName)
	end
end

i did like you said, but how do i connect findfirstchild:(“buy”) and mutatorBtn?

Your mutatorBtn is the Buy we get from the Frame, so you’d either just make local mutatorBtn = frame.Buy or you can switch out mutatorBtn to frame.Buy.

Still, the script is not working.

What is the error or is it just not giving the expected response?

no response at all. Nothing at the output

Hey, to search through all the children of the children (hope that makes sense!), you can use :GetDescendants() opposed to :GetChildren().

Would it not be possible to just use :GetDescendants here?

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