Is there a way to create multiple safe zones on gmod maps?

I know there is a way to create one safe zone, and I used this LUA code:



local function PlayerSpawnProtection(victim, attacker)
    local BoxCorner = Vector(-166.657898, -8377.381836, 10790.562500)
    local OppositeCorner = Vector(400.776825, -8906.498047, 10468.986328)
    local PlayersInArea = ents.FindInBox(BoxCorner,OppositeCorner)
    return not table.HasValue(PlayersInArea,victim)
end
 hook.Add( "PlayerShouldTakeDamage", "PlayerSpawnProtection", PlayerSpawnProtection)


This worked for creating one safe zone but when I tried using two of them, with multiple combonations, it didn’t work and I could only create one safe zone. There are no decent Addons to do this, so it would be nice if I could get some help to make it work with more than one safe zone.

Thanks!

The solution is really easy if you know basic programation, do you understand the code? The code itself tells you what do you have to add



local safeZones = {
    {Vector(-166.657898, -8377.381836, 10790.562500), Vector(400.776825, -8906.498047, 10468.986328)},
    {Vector(firstcorner), Vector(othercorner)},
    etc
}

local function PlayerSpawnProtection(victim, attacker)
    for _,v in ipairs(safeZones) do -- iterate over all of the safe zones
        local PlayersInArea = ents.FindInBox(v[1],v[2]) -- elements 1 and 2 (remember, Lua arrays start at 1!) correspond to the two corners
        if table.HasValue(PlayersInArea, victim) then -- only if they're present do we return.
            return false
        end
    end
    -- don't return if we're letting the rest of the game take its course.
end
 hook.Add( "PlayerShouldTakeDamage", "PlayerSpawnProtection", PlayerSpawnProtection)


But real talk, you tried on your own. Do you see why what you did didn’t work? It’s because the first one that runs would return true or false, and prevent the others from running, because returning a non-nil value from a hook tells the game that “Hey I handled it, don’t do any others”

Well I tried



local function PlayerSpawnProtection(victim, attacker)
    local SpawnRoomCorner = Vector(-166.657898, -8377.381836, 10790.562500)
    local SpawnRoomOppositeCorner = Vector(400.776825, -8906.498047, 10468.986328)
    local SpawnPlanetCorner = Vector(13167.078125, -1593.921753, -15656.645508)
    local SpawnPlanetOppositeCorner = Vector(4383.745605, -14560.382813, -3580.529297)
    local PlayersInArea = ents.FindInBox(SpawnRoomCorner, SpawnRoomOppositeCorner, SpawnPlanetCorner, SpawnPlanetOppositeCorner)
    return not table.HasValue(PlayersInArea,victim)
end
 hook.Add( "PlayerShouldTakeDamage", "PlayerSpawnProtection", PlayerSpawnProtection)


and that didnt work

[editline]18th September 2017[/editline]

Ah I see, and thanks for the help, ill try it now, and get back to you if it doesnt work

Under what circumstances did you believe that just adding more args to ents.FindInBox would work?

You need to read the documentation. https://wiki.garrysmod.com/page/ents/FindInBox