Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I use GetChildren or GetDecendants?

I was wondering how I can use them

1 answer

✓ Accepted answer

[MARK THIS ANSWER AS ACCEPTED IF THIS ANSWERS]

:GetDescendants() is a function, it returns an array that contains all of the descendants of that object. Unlike Instance:GetChildren(), which only returns the immediate children of an object, GetDescendants will find every child of the object, every child of those children, and so on.

GetDescendants is often used to do something to all the descendants that are a particular type of object. The code in this example uses GetDescendants and if statements for names to find all of the weapons in the Weapons folder in Replicated Storage with the correct name and gives them to the player.

Local script:

local button = script.Parent
local ScrollingFrame = script.Parent.Parent.Parent
local Rep = game.ReplicatedStorage

button.MouseButton1Click:Connect(function()
	ScrollingFrame.Visible = false
	Rep.RemoteEvent:FireServer(game.Players.LocalPlayer)
end)

Server script:

local Rep = game:GetService("ReplicatedStorage")
local event = Rep.RemoteEvent
local descendants = Rep.Weapons:GetDescendants()

local function giveTools(player)
	for index, descendant in pairs(descendants) do
		if descendant.Name == "Weapon1 Name" or descendant.Name == "Weapon2 Name" then
			local weaponsToGive = descendant:Clone()
			weaponsToGive.Parent = player.Backpack
		end
	end
end

event.OnServerEvent:Connect(giveTools)

In this case you can use GetChildren, but you may add subfolders in the Weapons Folder to classify/sort weapons which GetChildren won't cover but GetDescendants will.

This discussion is preserved as it was. New answers, comments, and votes are closed.