opening map barriers

On rp_paraleake_city_v3 the barriers that stop people driving into the back of the pd require a script for them to work properly. This is what i came up wit first.


function policebarrier()

	for k, v in pairs(ents.FindInBox( Vector( -8467, 9394, 352), Vector(-9015, 9685, 297))) do

		if v:IsVehicle() then

                        local ent1 = Entity(573)
			ent1:Fire("Open","",0)
			local ent2 = Entity(572)
			ent2:Fire("Open","",0)	
			
		else

			local ent1 = Entity(573)
			ent1:Fire("Close","",0)
			local ent2 = Entity(572)
			ent2:Fire("Close","",0)

		end

	end

end
hook.Add("Think", "policebarrier", policebarrier)

It works fine but because its in a think hook it constantly loops ent:Fire(“Open”) which makes a really annoying noise. So i came up with an idea to use a variable like this.


function policebarrier()

	local isOpen

	for k, v in pairs(ents.FindInBox( Vector( -8467, 9394, 352), Vector(-9015, 9685, 297))) do

		if v:IsVehicle() and !isOpen then

			isOpen = true
			local ent1 = Entity(573)
			ent1:Fire("Open","",0)
			local ent2 = Entity(572)
			ent2:Fire("Open","",0)
			
		elseif !v:IsVehicle() and isOpen then

			isOpen = false
			local ent1 = Entity(573)
			ent1:Fire("Close","",0)
			local ent2 = Entity(572)
			ent2:Fire("Close","",0)

		end

	end

end
hook.Add("Think", "policebarrier", policebarrier)

my idea was when a vehicle wasn’t in the box and the isOpen was true it would close but if isOpen was true and a vehicle was inside the box it wouldn’t close and i thought this would stop it from looping but it didn’t and after a while it just wouldn’t work at all. Does anyone have any idea on how to make this work because i have no idea what else to try.

Put the local isOpen declaration outside of the function and make it so the loop breaks if it detects one vehicle. You’ll then have to add something to make it so it only fires the close portion if it’s looped through everything and none of the entities were cars.