• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Optimize Script Remove Players

Sigoles

Discord: @sigoles
Joined
Nov 20, 2015
Messages
1,209
Solutions
2
Reaction score
154
Is there any way to optimize this script? I'm thinking it can cause a lot of lag ... It's an immense area with multiple floors

tfs 1.3

LUA:
function onTime(interval)
   
    -- remove all from multifloors
    local spec = getSpectators({x = 30443, y = 32336, z = 4}, 7, 20)
        if spec ~= nil then
            for _, s in pairs(spec) do
                    if isPlayer(s) then
                        doTeleportThing(s, getTownTemplePosition(getPlayerTown(s)))  
                    end
            end
        end
       
    local spec2 = getSpectators({x = 30463, y = 32336, z = 5}, 25, 35)
        if spec2 ~= nil then
            for _, a in pairs(spec2) do
                    if isPlayer(a) then
                        doTeleportThing(a, getTownTemplePosition(getPlayerTown(a)))  
                    end
            end
        end
       
    local spec3 = getSpectators({x = 30222, y = 32326, z = 5}, 91, 76)
        if spec3 ~= nil then
            for _, b in pairs(spec3) do
                    if isPlayer(b) then
                        doTeleportThing(b, getTownTemplePosition(getPlayerTown(b)))  
                    end
            end
        end
       
    local spec4 = getSpectators({x = 30447, y = 32336, z = 6}, 50, 85)
        if spec4 ~= nil then
            for _, c in pairs(spec4) do
                    if isPlayer(c) then
                        doTeleportThing(c, getTownTemplePosition(getPlayerTown(c)))  
                    end
            end
        end
   
    local spec5 = getSpectators({x = 30202, y = 32329, z = 6}, 135, 110)
        if spec5 ~= nil then
            for _, d in pairs(spec5) do
                    if isPlayer(d) then
                        doTeleportThing(d, getTownTemplePosition(getPlayerTown(d)))  
                    end
            end
        end
       
    local spec6 = getSpectators({x = 30158, y = 32314, z = 7}, 70, 78)
        if spec6 ~= nil then
            for _, e in pairs(spec6) do
                    if isPlayer(e) then
                        doTeleportThing(e, getTownTemplePosition(getPlayerTown(e)))  
                    end
            end
        end
       
    local spec7 = getSpectators({x = 30323, y = 32328, z = 7}, 54, 78)
        if spec7 ~= nil then
            for _, f in pairs(spec7) do
                    if isPlayer(f) then
                        doTeleportThing(f, getTownTemplePosition(getPlayerTown(f)))  
                    end
            end
        end
       
    local spec8 = getSpectators({x = 30463, y = 32336, z = 7}, 65, 125)
        if spec8 ~= nil then
            for _, g in pairs(spec8) do
                    if isPlayer(g) then
                        doTeleportThing(g, getTownTemplePosition(getPlayerTown(g)))  
                    end
            end
        end
       
    local spec9 = getSpectators({x = 30235, y = 32346, z = 8}, 52, 105)
        if spec9 ~= nil then
            for _, h in pairs(spec9) do
                    if isPlayer(h) then
                        doTeleportThing(h, getTownTemplePosition(getPlayerTown(h)))  
                    end
            end
        end
       
    local spec10 = getSpectators({x = 30351, y = 32347, z = 8}, 70, 74)
        if spec10 ~= nil then
            for _, i in pairs(spec10) do
                    if isPlayer(i) then
                        doTeleportThing(i, getTownTemplePosition(getPlayerTown(i)))  
                    end
            end
        end
       
    local spec11 = getSpectators({x = 30458, y = 32343, z = 8}, 59, 129)
        if spec11 ~= nil then
            for _, j in pairs(spec11) do
                    if isPlayer(j) then
                        doTeleportThing(j, getTownTemplePosition(getPlayerTown(j)))  
                    end
            end
        end
       
    local spec12 = getSpectators({x = 30481, y = 32361, z = 9}, 44, 66)
        if spec12 ~= nil then
            for _, k in pairs(spec12) do
                    if isPlayer(k) then
                        doTeleportThing(k, getTownTemplePosition(getPlayerTown(k)))  
                    end
            end
        end
   
    return true
end
 
Solution
At the bottom of:
data/lib/core/position.lua

I would add:
LUA:
function Position:isInRange(northWest, southEast)
    if  self.x >= northWest.x and self.x <= southEast.x
    and self.y >= northWest.y and self.y <= southEast.y 
    and self.z >= northWest.z and self.z <= southEast.z then
        return true
    end
    return false
end

Its a quick way to see if a position is inside an area.
And I would recode your script to this:

Con: It loops through every player on the server
Pro1: It only does 1 quick check per player if they are inside the zone of relevance for further checks
Pro2: If it is inside the zone of relevance, it will loop through each "spec zone", if it finds the player in one of them, it will stop looking through...
At the bottom of:
data/lib/core/position.lua

I would add:
LUA:
function Position:isInRange(northWest, southEast)
    if  self.x >= northWest.x and self.x <= southEast.x
    and self.y >= northWest.y and self.y <= southEast.y 
    and self.z >= northWest.z and self.z <= southEast.z then
        return true
    end
    return false
end

Its a quick way to see if a position is inside an area.
And I would recode your script to this:

Con: It loops through every player on the server
Pro1: It only does 1 quick check per player if they are inside the zone of relevance for further checks
Pro2: If it is inside the zone of relevance, it will loop through each "spec zone", if it finds the player in one of them, it will stop looking through the rest of the spec zones.

I think the end result is a big performance gain, since your not calling getSpectators 12 times on lots of tiles. This is pure positional calculations.

LUA:
zone = {
    -- North west point of all the zones
    northWest = Position(30000, 32100, 4),
    -- South east of all zones
    southEast = Position(30650, 32500, 9),
    -- All positions here should be inside the above specified zone
    areas = {
        {    -- spec1 7, 20
            from = {x = 30443 - 7, y = 32336 - 20, z = 4},
            to = {x = 30443 + 7, y = 32336 + 20, z = 4}
        }, { -- spec2 25, 35
            from = {x = 30463 - 25, y = 32336 - 35, z = 5},
            to = {x = 30463 + 25, y = 32336 + 35, z = 5}
        }, { -- spec3 91, 76
            from = {x = 30222 - 91, y = 32326 - 76, z = 5},
            to = {x = 30222 + 91, y = 32326 + 76, z = 5}
        }, { -- spec4 50, 85
            from = {x = 30447 - 50, y = 32336 - 85, z = 6},
            to = {x = 30447 + 50, y = 32336 + 85, z = 6}
        }, { -- spec5 135, 110
            from = {x = 30202 - 135, y = 32329 - 110, z = 6},
            to = {x = 30202 + 135, y = 32329 + 110, z = 6}
        }, { -- spec6 70, 78
            from = {x = 30158 - 70, y = 32314 - 78, z = 7},
            to = {x = 30158 + 70, y = 32314 + 78, z = 7}
        }, { -- spec7 54, 78
            from = {x = 30323 - 54, y = 32328 - 78, z = 7},
            to = {x = 30323 + 54, y = 32328 + 78, z = 7}
        }, { -- spec8 65, 125
            from = {x = 30463 - 65, y = 32336 - 125, z = 7},
            to = {x = 30463 + 65, y = 32336 + 125, z = 7}
        }, { -- spec9 52, 105
            from = {x = 30235 - 52, y = 32346 - 105, z = 8},
            to = {x = 30235 + 52, y = 32346 + 105, z = 8}
        }, { -- spec10 70, 74
            from = {x = 30351 - 70, y = 32347 - 74, z = 8},
            to = {x = 30351 + 70, y = 32347 + 74, z = 8}
        }, { -- spec11 59, 129
            from = {x = 30458 - 59, y = 32343 - 129, z = 8},
            to = {x = 30458 + 59, y = 32343 + 129, z = 8}
        }, { -- spec12 44, 66
            from = {x = 30481 - 44, y = 32361 - 66, z = 9},
            to = {x = 30481 + 44, y = 32361 + 66, z = 9}
        }
    }
}

function onTime(interval)
    -- For each player
    for _, player in ipairs(Game.getPlayers()) do

        -- Are they in range of any of the zones?
        local pos = player:getPosition()
        if pos:isInRange(zone.northWest, zone.southEast) then

            -- Then lets look through each of the spec zones to see if they are in any of those.
            for k, spec in pairs(zone.areas) do
               
                -- If they are inside one of the smaller "spec" zones
                if pos:isInRange(spec.from, spec.to) then

                    -- Teleport player to town
                    local town = player:getTown()
                    player:teleportTo(town:getTemplePosition())
                    break -- Stop Looping through the rest of the zones, we found player in this zone.
                end
            end
        end
    end
    return true
end
 
Solution
Back
Top