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.
01 | local close = script.Parent.Parent.SwitchRail.Switch:GetChildren( "Close" ) |
02 | local open = script.Parent.Parent.SwitchRail.Switch:GetChildren( "Open" ) |
03 |
04 | print ( "WAZZAP" ) |
05 | function onTouched(part) |
06 | if part.Name = = "ARS" then --Change this to the name of the correct part that trips the sensor |
07 | if part.Value.Value = = 1 then |
08 | script.Parent.Parent.LeftSignal.JCT 1. SurfaceGui.Whi.ImageTransparency = 0 |
09 | script.Parent.Parent.LeftSignal.JCT 2. SurfaceGui.Whi.ImageTransparency = 0 |
10 | script.Parent.Parent.LeftSignal.JCT 3. SurfaceGui.Whi.ImageTransparency = 0 |
11 | script.Parent.Parent.LeftSignal.JCT 4. SurfaceGui.Whi.ImageTransparency = 0 |
12 | for i, v in pairs (close) do |
13 | v.Transparency = 0.5 |
14 | v.CanCollide = false |
15 | i.Transparency = 0 |
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:
1 | for i, v in pairs (close) do |
2 | if v.Name = = "Brick" or v.Name = = "ADiffBrick" then |
3 | v.Transparency = 0.5 |
4 | v.CanCollide = false |
5 | else |
6 | v.Transparency = 0 |
7 | v.CanCollide = true |
8 | end |
9 | 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!
your useing getchildren() wrong you do it with for i,v not like the way your useing it as