Open all doors LUA

Hey, I have been trying to find a way to make all doors open when a command is run in lua when on a map. Does anyone know how to do this?

Thanks

[lua]
function UnlockAllDoors()
for k,v in pairs(ents.GetAll()) do
if(v:GetClass() == (“func_door” or “prop_door_rotating” or “func_door_rotating”)) then
v:Fire(“unlock”,"",0)
v:Fire(“Open”,"",0)
end
end
end
[/lua]

In addition you can use v:Fire(“Open” , “”, 0); after that unlock to open them physically rather than just unlocking them.

Good point. I forgot about that. Fixed

Runs perfectly with what I was trying to do… Thank you very much!

That’s not how or works. It may seem to work but it’s actually going to fire only func_door’s and skip the rest. You would need
[lua]if v:GetClass()==“func_door” or v:GetClass()==“prop_door_rotating” or v:GetClass()==“func_door_rotating” then[/lua]

because a string always will be a string or !nil

!nil because means that it is NOT nil

if you wanna check if a string is a string using

[lua]if “str” != nil then
end
[/lua]

then you aren’t doing it right because what if you do this

[lua]if 123 != nil then
end
[/lua]

A string always evaluates to Boolean true. Nil always evaluates to Boolean false.
When you use string or string it will select the first string since it has already encountered a value that is true and doesn’t need to evaluate any more of the expression. In the case of nil or string it will evaluate nil to be false and will move on to the string which will evaluate to true and then return that string since it doesn’t need to evaluate anymore of the expression.

Lua always evaluates nil to false and any object or string to true, I’m not sure if numbers are always true or if it’s dependent on their value so someone else may need to confirm that.

Numbers evaluate to true, but aren’t equal to true

They’re truthy :v:

[lua]
– Checking if its a door.
function Door(ent)

local Doors = {
	"func_door",
	"prop_door_rotating",
	"func_door_rotating"
}

for k, v in pairs(Doors) do
	if ent:GetClass() == v then return true end
end

end

– Doing the actually opening.
for _, v in pairs(ents.GetAll()) do
if Door(v) then
v:Fire(“Open”,"",0) – No need to unlock, we’re god, we’re forcing the shit up…
end
end
[/lua]

Any non-nil value can be treated like true.

Is there a way to check if a door is already open so I can make it close instead?

replace “Open” with “Toggle”

You shouldn’t be creating the table inside the function. Create all tables outside the function unless you actually need the table created inside the function. We don’t need that, and we don’t even actually need a table either.

[lua]
function IsDoor(class)
return class == “func_door” or class == “prop_door_rotating” or class == “func_door_rotating”
end

for _, v in pairs(ents.GetAll()) do
if IsDoor(v:GetClass()) then
v:Fire(“Open”, “”, 0)
end
end[/lua]

Or you can use table.HasValue, which is essentially your original Door function, but GMod comes with this and we don’t need to make another function.

[lua]local Doors =
{
“func_door”,
“prop_door_rotating”,
“func_door_rotating”
}

for _, v in pairs(ents.GetAll()) do
if table.HasValue(Doors, v:GetClass()) then
v:Fire(“Open”, “”, 0)
end
end[/lua]