• 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!

TFS 1.X+ help to decrease or remove lag from this script

WikiArk21

Member
Joined
Oct 24, 2023
Messages
23
Reaction score
5
I was making a script for the posts and similar to change from on to off at a certain time on the server, but this causes a lag in the time. Is there any way to reduce this lag or even eliminate the lag altogether? Remembering that this is a globalevents

LUA:
local LIGHT_ON_TIME = 18
local LIGHT_OFF_TIME = 6

local LAMPS = {
    [1479] = 1480,
    [26782] = 26781,
    [26783] = 26784,
}

function toggleLamps()
    local currentHour = tonumber(os.date("%H"))
    local shouldTurnOn = (currentHour >= LIGHT_ON_TIME or currentHour < LIGHT_OFF_TIME)
    local worldLightState = Game.getStorageValue(65412)

    -- Se o estado já está correto, não faz nada
    if worldLightState == (shouldTurnOn and 1 or 0) then
        return true
    end

    for x = 100, 1000 do
        for y = 100, 1000 do
            for z = 0, 15 do
                local tile = Tile(Position(x, y, z))
                if tile then
                    local item = nil
                    for lampOff, lampOn in pairs(LAMPS) do
                        item = tile:getItemById(lampOff) or tile:getItemById(lampOn)
                        if item then
                            item:transform(shouldTurnOn and lampOn or lampOff)
                        end
                    end
                end
            end
        end
    end

    -- Atualiza o estado das luzes no servidor
    Game.setStorageValue(65412, shouldTurnOn and 1 or 0)
    return true
end

function onThink(interval)
    toggleLamps()
    return true
end
 
Solution
How many lamps do you have? If they're on their position all the time then you can use RemereEditor -> Edit -> Find Item to find specific item on map (in your case it's a lamp) and then use export option to save externally list of lamps with their position.
Such list converted to lua structure will be useful to iterate over it instead of looping on whole world area.
How many lamps do you have? If they're on their position all the time then you can use RemereEditor -> Edit -> Find Item to find specific item on map (in your case it's a lamp) and then use export option to save externally list of lamps with their position.
Such list converted to lua structure will be useful to iterate over it instead of looping on whole world area.
 
Solution
How many lamps do you have? If they're on their position all the time then you can use RemereEditor -> Edit -> Find Item to find specific item on map (in your case it's a lamp) and then use export option to save externally list of lamps with their position.
Such list converted to lua structure will be useful to iterate over it instead of looping on whole world area.

I had thought about this alternative, but I thought it would cause lag anyway. I did it and it worked, thanks anyway.
 
I was making a script for the posts and similar to change from on to off at a certain time on the server, but this causes a lag in the time. Is there any way to reduce this lag or even eliminate the lag altogether? Remembering that this is a globalevents

LUA:
local LIGHT_ON_TIME = 18
local LIGHT_OFF_TIME = 6

local LAMPS = {
    [1479] = 1480,
    [26782] = 26781,
    [26783] = 26784,
}

function toggleLamps()
    local currentHour = tonumber(os.date("%H"))
    local shouldTurnOn = (currentHour >= LIGHT_ON_TIME or currentHour < LIGHT_OFF_TIME)
    local worldLightState = Game.getStorageValue(65412)

    -- Se o estado já está correto, não faz nada
    if worldLightState == (shouldTurnOn and 1 or 0) then
        return true
    end

    for x = 100, 1000 do
        for y = 100, 1000 do
            for z = 0, 15 do
                local tile = Tile(Position(x, y, z))
                if tile then
                    local item = nil
                    for lampOff, lampOn in pairs(LAMPS) do
                        item = tile:getItemById(lampOff) or tile:getItemById(lampOn)
                        if item then
                            item:transform(shouldTurnOn and lampOn or lampOff)
                        end
                    end
                end
            end
        end
    end

    -- Atualiza o estado das luzes no servidor
    Game.setStorageValue(65412, shouldTurnOn and 1 or 0)
    return true
end

function onThink(interval)
    toggleLamps()
    return true
end
Bro you are looping over 12,960,000 positions, constructing a tile on each position, and then searching that tile for an item that might exist and so if you find it, you turn it on... this is why you are lagging, the other suggestion was good... I would just make the script registered to an actionid instead, and then assign the action id to the item on map, and have the script confirm the item that is calling it is indeed a lamp..... that beats saving, storing, loading all those positions... and its much easier and efficient than what is going on now
 
You can also save the positions when the server starts, so you don't have to iterate over all the positions later.

For example:
LUA:
local LIGHT_ON_TIME = 18
local LIGHT_OFF_TIME = 6

local LAMPS = {
    [1479] = 1480,
    [26782] = 26781,
    [26783] = 26784,
}

local LAMPS_CACHE = {}

function toggleLamps()
    local currentHour = tonumber(os.date("%H"))
    local shouldTurnOn = (currentHour >= LIGHT_ON_TIME or currentHour < LIGHT_OFF_TIME)
    local worldLightState = Game.getStorageValue(65412)

    -- Se o estado já está correto, não faz nada
    if worldLightState == (shouldTurnOn and 1 or 0) then
        return true
    end

    for _, pos in ipairs(LAMPS_CACHE) do
        local tile = Tile(pos)
        if tile then
            local item = nil
            for off, on in pairs(LAMPS) do
                item = tile:getItemById(off) or tile:getItemById(on)
                if item then
                    item:transform(shouldTurnOn and on or off)
                end
            end
        end
    end

    -- Atualiza o estado das luzes no servidor
    Game.setStorageValue(65412, shouldTurnOn and 1 or 0)
    return true
end

function onThink(interval)
    toggleLamps()
    return true
end

function onStartup()
    for x = 100, 1000 do
        for y = 100, 1000 do
            for z = 0, 15 do
                local tmpPos = Position(x, y, z)
                local tile = Tile(tmpPos)
                if tile then
                    for off, on in pairs(LAMPS) do
                        if tile:getItemById(off) or tile:getItemById(on) then
                            LAMPS_CACHE[#LAMPS_CACHE + 1] = tmpPos
                        end
                    end
                end
            end
        end
    end
    return true
end
 
Back
Top