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

Why is this in pairs code line not working correctly?

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

1 comment

0

Please consider at least reading my answer.

2 answers

✓ Accepted answer

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.

2 comments

0

Wow good job! Really well explained.

0

Thanks! Really appreciate it.

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

1 comment

0

Ah, knew it'd be an easier fix that what I thought. Thank you very much.

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