• 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+ [TFS 1.5] save character when moving, or moving item

This script automatically saves players when they move items. If you already have an automatic onThink global save elsewhere, remove the onThink part and keep only the onMoveItem function to update the database when items are moved. A 5-second storage cooldown is used to prevent excessive CPU usage due to frequent item movements, but I'm not sure if the script is efficient. Monitor CPU usage to evaluate its performance.

LUA:
local globalSavePlayers = GlobalEvent("GlobalSavePlayers")
function globalSavePlayers.onThink()
    for _, player in ipairs(Game.getPlayers()) do
        player:save()
    end
    print("All players have been automatically saved.")
    return true
end
globalSavePlayers:interval(600000) -- Periodic global save every 10 minutes (600,000 milliseconds)
globalSavePlayers:register()

local CONTAINER_POSITION = 65535
local SAVE_COOLDOWN_STORAGE = 99999
local SAVE_COOLDOWN_TIME = 5 -- Cooldown in seconds to avoid frequent saves
local DEBUG_MODE = true -- false or true

local function delayedSave(playerId)
    local player = Player(playerId)
    if player then
        player:save()
        if DEBUG_MODE then
            print(string.format("Player %s saved after item movement.", player:getName()))
        end
    end
end

local saveOnMoveItem = EventCallback
function saveOnMoveItem.onMoveItem(player, item, fromPosition, toPosition, fromCylinder, toCylinder)
    if not player or not item or item:getId() <= 0 then
        if DEBUG_MODE then
            print("Invalid item or player detected during movement.")
        end
        return true
    end

    fromPosition = type(fromPosition) == "table" and fromPosition or {x = -1, y = -1, z = -1}
    toPosition = type(toPosition) == "table" and toPosition or {x = -1, y = -1, z = -1}

    local lastSaveTime = player:getStorageValue(SAVE_COOLDOWN_STORAGE)
    if lastSaveTime ~= -1 and os.time() < lastSaveTime then
        if DEBUG_MODE then
            print(string.format("Skipping save for player %s due to cooldown. Item ID: %d, From: (%d, %d, %d), To: (%d, %d, %d).",
             player:getName(), item:getId(), fromPosition.x, fromPosition.y, fromPosition.z, toPosition.x, toPosition.y, toPosition.z))
        end
        return true
    end

    player:setStorageValue(SAVE_COOLDOWN_STORAGE, os.time() + SAVE_COOLDOWN_TIME)
    addEvent(delayedSave, 1000, player:getId())
    return true
end
saveOnMoveItem:register()

There is another commit, but I'm not sure if it works. I have never tested it, so it's a good idea to take a look.

Note: My TFS 1.7 8.60, which you saw, has a different Magic Sword ID because I'm using clientID. But check the GIF, it's working for me when moved, saved, and updated in the database. However, I know it's not ideal because it consumes CPU... that's all.

 
Last edited:
Back
Top