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

Offline training - bed problem

Rycina

Member
Joined
Nov 7, 2024
Messages
59
Solutions
1
Reaction score
8
Hello, I have a problem with offline training, because when I log in the character stays in bed and is occupied (as in the screenshot)

TFS 1.4.2

CMD:

LUA:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/offlinetraining.lua:onLogin
LuaScriptInterface::getNumber(). Argument 2 has out-of-range value for unsigned int: -1
stack traceback:
        [C]: in function 'setOfflineTrainingSkill'
        data/creaturescripts/scripts/offlinetraining.lua:10: in function <data/creaturescripts/scripts/offlinetraining.lua:1>

Offlinetraining.lua:


Code:
function onLogin(player)
    local lastLogout = player:getLastLogout()
    local offlineTime = lastLogout ~= 0 and math.min(os.time() - lastLogout, 86400 * 21) or 0
    local offlineTrainingSkill = player:getOfflineTrainingSkill()
    if offlineTrainingSkill == -1 then
        player:addOfflineTrainingTime(offlineTime * 1000)
        return true
    end

    player:setOfflineTrainingSkill(-1)

    if offlineTime < 600 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You must be logged out for more than 10 minutes to start offline training.")
        return true
    end

    local trainingTime = math.max(0, math.min(offlineTime, math.min(43200, player:getOfflineTrainingTime() / 1000)))
    player:removeOfflineTrainingTime(trainingTime * 1000)

    local remainder = offlineTime - trainingTime
    if remainder > 0 then
        player:addOfflineTrainingTime(remainder * 1000)
    end

    if trainingTime < 60 then
        return true
    end

    local text = "During your absence you trained for"
    local hours = math.floor(trainingTime / 3600)
    if hours > 1 then
        text = string.format("%s %d hours", text, hours)
    elseif hours == 1 then
        text = string.format("%s 1 hour", text)
    end

    local minutes = math.floor((trainingTime % 3600) / 60)
    if minutes ~= 0 then
        if hours ~= 0 then
            text = string.format("%s and", text)
        end

        if minutes > 1 then
            text = string.format("%s %d minutes", text, minutes)
        else
            text = string.format("%s 1 minute", text)
        end
    end

    text = string.format("%s.", text)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)

    local vocation = player:getVocation()
    local promotion = vocation:getPromotion()
    local topVocation = not promotion and vocation or promotion

    local updateSkills = false
    if isInArray({SKILL_CLUB, SKILL_SWORD, SKILL_AXE, SKILL_DISTANCE}, offlineTrainingSkill) then
        local modifier = topVocation:getAttackSpeed() / 1000
        updateSkills = player:addOfflineTrainingTries(offlineTrainingSkill, (trainingTime / modifier) / (offlineTrainingSkill == SKILL_DISTANCE and 4 or 2))
    elseif offlineTrainingSkill == SKILL_MAGLEVEL then
        local gainTicks = topVocation:getManaGainTicks() * 2
        if gainTicks == 0 then
            gainTicks = 1
        end

        updateSkills = player:addOfflineTrainingTries(SKILL_MAGLEVEL, trainingTime * (vocation:getManaGainAmount() / gainTicks))
    end

    if updateSkills then
        player:addOfflineTrainingTries(SKILL_SHIELD, trainingTime / 4)
    end

    return true
end
 

Attachments

TFS 1.4.2
TFS 1.4 function setOfflineTrainingSkill is bugged.
You got to replace ( https://github.com/otland/forgottenserver/blob/1.4/src/luascript.cpp#L8772 ):
C++:
        uint32_t skillId = getNumber<uint32_t>(L, 2);
        player->setOfflineTrainingSkill(skillId);
with:
C++:
        int32_t skillId = getNumber<int32_t>(L, 2);
        player->setOfflineTrainingSkill(skillId);
to make it load negative numbers from Lua.

If was fixed in Fixed player:setOfflineTrainingSkill(skillId) (#3971) · otland/forgottenserver@75721c6 (https://github.com/otland/forgottenserver/commit/75721c60973df34ee881476bfc20b0998a3658c3)
 
Last edited:
Thanks you :)

The error no longer exists, but after getting into bed and logging in again, it still appears as if there was a character lying in the bed
 

Attachments

The error no longer exists, but after getting into bed and logging in again
Maybe it's engine bug, but it can be also problem with running bugged Lua code before. Maybe sleeper ID is stored or is not stored in house data saved in database.
If it's test server, shut down OTS, then in MySQL remove all rows (truncate) table tile_store and start server again.
 
player:setOfflineTrainingSkill(-1)
 
Work! Thanks! :D
 
Work! Thanks! :D
I hope you also followed (replace -1 to 0):
Otherwise 'offline training time` won't regenerate, when you are offline and NOT training. IDK, if it regenerates with change from -1 to 0 at all.
Official TFS change was to make Lua accept -1 value as parameter in C++ ( Fixed player:setOfflineTrainingSkill(skillId) (#3971) · otland/forgottenserver@75721c6 (https://github.com/otland/forgottenserver/commit/75721c60973df34ee881476bfc20b0998a3658c3) ).
I see that you cannot train 'fist fighting', which is skill = 0, so it should work.
 
I hope you also followed (replace -1 to 0):
Otherwise 'offline training time` won't regenerate, when you are offline and NOT training. IDK, if it regenerates with change from -1 to 0 at all.
Official TFS change was to make Lua accept -1 value as parameter in C++ ( Fixed player:setOfflineTrainingSkill(skillId) (#3971) · otland/forgottenserver@75721c6 (https://github.com/otland/forgottenserver/commit/75721c60973df34ee881476bfc20b0998a3658c3) ).
I see that you cannot train 'fist fighting', which is skill = 0, so it should work.
I haven't tested this case yet. As soon as I get home, I'll undo it, follow the commit you sent, and run a test to check.
 
Back
Top