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

Why is this in pairs code line not working correctly?

Asked by
Zeluxis 100
5 years ago

Hi,

I'm making a gate that when you click it all the parts in the gate model disappear. This works, however throws errors out when it gets to the ClickDetector and then doesn't work correctly.

However, I don't see why it should? I've made it so it checks the ClassName of each child to ensure the correct things are happening to the correct parts. I'm a bit stumped.

Here's the code:

local function CloseClose()
    for i,v in pairs(script.Parent.Closed:GetChildren()) do
            if v.ClassName=='Part' or 'UnionOperation' then
                v.Transparency=1
                v.CanCollide=false
            else
                v.MaxActivationDistance=0
                end
    end
end
0
Please consider at least reading my answer. User#24403 69 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago

Before you fix your problem you must first know truthy values and falsey values.


Truthy values

Truthy values in Lua are anything that is not false or nil. Strings, numbers and tables are examples of truthy values.

Falsey values

Falsey values are just false or nil.

Logical operators

or, and, not are all logical operators.

  • x or y evaluates to x if x is truthy, y otherwise.

  • x and y evaluates to x if x is falsey, y otherwise

  • not x evaluates to true if x is falsey, false otherwise.

How do we fix this?

Change line 3 to if v:IsA("BasePart") then

BasePart is the superclass of all the part classes. Part, UnionOperation, TrussPart, and ect. all inherit this class.

0
Wow good job! Really well explained. starmaq 1290 — 5y
0
Thanks! Really appreciate it. Zeluxis 100 — 5y
Ad
Log in to vote
1
Answered by
starmaq 1290 Moderation Voter
5 years ago

I'm assuming this problem is coming from this v.ClassName=='Part' or 'UnionOperation', that's not how the or operator work, you need to do it like this

if v.ClassName=='Part' or v.ClassName =='UnionOperation' then

You can also use :IsA() which is cooler if v:IsA("Part") then

0
Ah, knew it'd be an easier fix that what I thought. Thank you very much. Zeluxis 100 — 5y

Answer this question