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

Is this GetChildren Right?

Im trying to make a tool open/close, code is below

open = script.Parent.Parent.Parent.Open

while true do
if open.Value == true then
	local c = script.Parent:GetChildren()
	for i = 1, #c do
	if c[i].ClassName == "UnionOperation" then
		if c[i]:FindFirstChild("LeftFlang")  ~= nil then
        c[i].Transparency = 0
        elseif c[i]:FindFirstChild("RightFlang")  ~= nil then
        c[i].Transparency = 0
else 
	local c = script.Parent:GetChildren()
	for i = 1, #c do
	if c[i].ClassName == "UnionOperation" then
		if c[i]:FindFirstChild("LeftFlang")  ~= nil then
        c[i].Transparency = 1
        elseif c[i]:FindFirstChild("RightFlang")  ~= nil then
        c[i].Transparency = 1
	wait()
	end
	wait()
end


2 comments

0

There should be an end between line 11 and 12 and line 19 and 20. I would recommend using "for _,c in pairs(script.Parent:GetChildren()) do". Otherwise, yes, you are using "GetChildren" correctly.

0

if statements need to be closed by an 'end'

1 answer

Your code is missing an end for most of the if statements.

Here it is re-written a lot shorter:

local open=script.Parent.Parent.Parent.Open
while wait()do
	local transparency=open.Value and 0 or 1
	for _,c in pairs(script.Parent:GetChildren())do
		if c.ClassName=="UnionOperation"and(c:FindFirstChild("LeftFlang")or c:FindFirstChild("RightFlang"))then
			c.Transparency=transparency
		end
	end
end

edit: Actually, since you seem to be using some sort of storage value, this is bad. You should be using the Changed event:

local open=script.Parent.Parent.Parent.Open
open.Changed:connect(function()
	local transparency=open.Value and 0 or 1
	for _,c in pairs(script.Parent:GetChildren())do
		if c.ClassName=="UnionOperation"and(c:FindFirstChild("LeftFlang")or c:FindFirstChild("RightFlang"))then
			c.Transparency=transparency
		end
	end
end)

2 comments

0

didnt work

0

thanks

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