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

:GetChildren() Not working in my cancollide / transparency script?

I'm trying to create a script where it gets the parts and either makes them cancollide or not. There are parts called open and parts called closed.

local close = script.Parent.Parent.SwitchRail.Switch:GetChildren("Close")
local open = script.Parent.Parent.SwitchRail.Switch:GetChildren("Open")

print("WAZZAP")
function onTouched(part)
	if part.Name == "ARS" then --Change this to the name of the correct part that trips the sensor
	if part.Value.Value == 1 then
        script.Parent.Parent.LeftSignal.JCT1.SurfaceGui.Whi.ImageTransparency = 0
        script.Parent.Parent.LeftSignal.JCT2.SurfaceGui.Whi.ImageTransparency = 0
        script.Parent.Parent.LeftSignal.JCT3.SurfaceGui.Whi.ImageTransparency = 0
        script.Parent.Parent.LeftSignal.JCT4.SurfaceGui.Whi.ImageTransparency = 0
	for i, v in pairs(close) do
    v.Transparency = 0.5
    v.CanCollide = false
    i.Transparency = 0
    i.CanCollide = true
end
     else
		for i, v in pairs(open) do
    v.Transparency = 0.5
    v.CanCollide = false
    i.Transparency = 0
    i.CanCollide = true
end
	end
	end
	end



script.Parent.Touched:connect(onTouched)
	

4 comments

0

:GetChildren() dont take arguments?

0

?

0

indent your code so it’s more legible maybe?

0

indent your code so it’s more legible maybe?

2 answers

✓ Accepted answer

The :GetChildren() function does not take any arguments.

And in for i, v in pairs(open) and for i, v in pairs(close), i is the iterator variable and v is the variable to represent the current object in the loop. In other terms, i is just a number that tells the for loop what object in the table you're on in the loop and thus can not be treated as an object like v.

If you want only certain children of "Switch" to be changed a certain way, then add an if statement. For example:

for i, v in pairs(close) do
	if v.Name == "Brick" or v.Name == "ADiffBrick" then
		v.Transparency = 0.5
		v.CanCollide = false
	else
		v.Transparency = 0
		v.CanCollide = true
	end
end

I've also noticed that a lot of your code is repetitive. A good way to shorten it down would be to create other functions.

Also :connect is deprecated, so I would recommend using :Connect instead.

I hope this helped!

1 comment

0

Oh, Thank you but I change all the bricks names lol

your useing getchildren() wrong you do it with for i,v not like the way your useing it as

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