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)
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!
your useing getchildren() wrong you do it with for i,v not like the way your useing it as