Ad
Log in to vote
0

How can I use GetChildren or GetDecendants?

Asked by 14 days ago

I was wondering how I can use them

1 answer

Log in to vote
1
Answered by 14 days ago
Edited 14 days ago

[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.

Ad

Answer this question