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

How to use GetAllchildren?

I'm trying to make a script that effects all the children of a parent because I don't want to use too much scripts parented in a part, but I got confused on how to use GetAllChildren. I tried to name all the parts in the model, "Part" but this script located in the model only effected 1 part. Here is a script in the parent.

note that im not asking to FIX the script, im asking on how to use GetAllChildren.

local parent = script.Parent.Part
local basicColor = BrickColor.new("Institutional white")
local basicMaterial = "SmoothPlastic"
local material = "Neon"
local orange = BrickColor.new("Br. yellowish orange")
local black = BrickColor.new("Black")
local neon = BrickColor.new("Neon orange")
local brown = BrickColor.new("CGA brown")
local debounce = false

local function onTouch(hit)
    if debounce then
        return
    end
    debounce = true
    parent.FireEffect.Enabled = true
    parent.Material = material
    parent.BrickColor = orange
    wait (0.8)
    parent.Transparency = 1
    parent.FireEffect.Enabled = false
    parent.CanCollide = false 
    debounce = false 
    wait (4)
    parent.Transparency = 0
    parent.CanCollide = true
    parent.Material = basicMaterial 
    parent.BrickColor = basicColor
end

parent.Touched:Connect(onTouch)

The "Parent" is a model and the "Children" are blocks called parts. If anyone would give me a script instead of just sending the ROBLOX Wiki page of GetAllChildren, that would be helpful since I'm a beginner scripter. Thanks!

1 comment

0

Please accept my answer if that works. Thank you!

1 answer

Well , to have listen to touched events on all children or descendants of an object, you would simply iterate through a GetChildren or GetDescendants table, then listen to touched events on those parts.

local parts = workspace.Object:GetChildren()
for _,part in pairs(parts) do
	if part:IsA("BasePart") then
		part.Touched:Connect(function(hit)
			...
		end
	end		
end

There is no such thing as "GetAllChildren", functions like :GetChildren and :GetDescendants return an array of objects that are the children / descendants of the object the method is called from.

To access all of these as a string, you can use table.concat, to access them as a tuple, you can use unpack()

Ex:

local tbl = workspace:GetDescendants()

for k,v in pairs(tbl) do
	tbl[k] = v.Name
	--tbl[k] = tostring(v) would also but, but using the name property is better
end

print(unpack(tbl))
print(table.concat(tbl," ,"))
print(type(table.concat(tbl," ,")))--string

now, as unpack returns a tuple, if you try to use the type function on it, it will just return the type of the first value of the table

2 comments

0

the concat function will fail in this case. It can only concatenate string and number elements. Instances are not strings or numbers.

0

oof,couldnt really test it in studio at school

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