Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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.

01local close = script.Parent.Parent.SwitchRail.Switch:GetChildren("Close")
02local open = script.Parent.Parent.SwitchRail.Switch:GetChildren("Open")
03 
04print("WAZZAP")
05function 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.JCT1.SurfaceGui.Whi.ImageTransparency = 0
09        script.Parent.Parent.LeftSignal.JCT2.SurfaceGui.Whi.ImageTransparency = 0
10        script.Parent.Parent.LeftSignal.JCT3.SurfaceGui.Whi.ImageTransparency = 0
11        script.Parent.Parent.LeftSignal.JCT4.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
View all 31 lines...
0
:GetChildren() dont take arguments? EliteMackan 0 — 6y
0
? CarlPlandog 20 — 6y
0
indent your code so it’s more legible maybe? User#19524 175 — 6y
0
indent your code so it’s more legible maybe? User#19524 175 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

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:

1for 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
9end

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!

0
Oh, Thank you but I change all the bricks names lol CarlPlandog 20 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question