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

Lua Lua Loop Issue

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,551
Solutions
1
Reaction score
88
Location
Portugal
Hey, I created this to regenerate stamina while people are in trainers but my loop is not working correctly I guess, cuz I left my char there for alot of hours with 0 stamina and when I woke up it still had 0 stamina XD

Could someone have a look and tell me what am I doing wrong please?

Thanks <3

Code:
local STORAGE = 616261
local EXHAUST_STORAGE = 616262

local function loopHealStamina(playerId)
    local player = Player(playerId)
    if not player then
        return
    end

    if player:getExhaustion(EXHAUST_STORAGE) >= 1 then
        addEvent(loopHealStamina, 1000, playerId)
        return true
    end

    if player:getStorageValue(STORAGE) <= 0 then
        return
    end

    if player:getStorageValue(STORAGE) >= 1 then
        player:setStamina((player:getStamina() + 1))
        addEvent(loopHealStamina, 1000, playerId)
    end
return true
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(STORAGE, 1)
    loopHealStamina(player:getId())
    player:setExhaustion(EXHAUST_STORAGE, 600000)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your stamina started regenerating.\n\nYou will receive 1 minute stamina for each 10 minutes training.")
return true
end

function onStepOut(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(STORAGE, 0)
    loopHealStamina(player:getId())
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Your stamina stopped regenerating.")
return true
end
 
As far as I can tell, this piece of code is literally not doing anything, and it's most likely the cause of your script not functioning as you want it to.
Green text it, and see what happens.
Code:
if player:getExhaustion(EXHAUST_STORAGE) >= 1 then
         addEvent(loopHealStamina, 1000, playerId)
         return true
end
 
Replace the function loopHealStamina with this:
Code:
local function loopHealStamina(playerId)
    local player = Player(playerId)
    if not player or player:getExhaustion(EXHAUST_STORAGE) < 1 or player:getStorageValue(STORAGE) ~= 1 then
        return
    end

    local stamina = player:getStamina()
    if stamina == 2520 then
        return
    end

    player:setStamina(stamina + 1)
    addEvent(loopHealStamina, 1000, playerId)
end
 
@Xikini yeah ur right

@Ninja with your function everytime player goes out and inside training tile he restores 1 stamina, no what cooldown I set on movement script

here's the script edited with your function:

Code:
local STORAGE = 616261
local EXHAUST_STORAGE = 616262

local function loopHealStamina(playerId)
    local player = Player(playerId)
    if not player or player:getExhaustion(EXHAUST_STORAGE) < 1 or player:getStorageValue(STORAGE) ~= 1 then
        return
    end

    local stamina = player:getStamina()
    if stamina == 2520 then
        return
    end

    player:setStamina(stamina + 1)
    addEvent(loopHealStamina, 1000, playerId)
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(STORAGE, 1)
    loopHealStamina(player:getId())
    player:setExhaustion(EXHAUST_STORAGE, 10000)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your stamina started regenerating.\n\nYou will receive 1 minute stamina for each 10 minutes training.")
return true
end

function onStepOut(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(STORAGE, 0)
    loopHealStamina(player:getId())
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Your stamina stopped regenerating.")
return true
end
 
Try this
Code:
local STORAGE = 616261
local EXHAUST_STORAGE = 616262

local function loopHealStamina(playerId)
    local player = Player(playerId)

    local stamina = player:getStamina()
    if stamina == 2520 then
        return
    end

    player:setStamina(stamina + 1)
    addEvent(loopHealStamina, 1000, playerId)
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() or player:getExhaustion(EXHAUST_STORAGE) == 1 then
        return false
    end

    player:setStorageValue(STORAGE, 1)
    loopHealStamina(player:getId())
    player:setExhaustion(EXHAUST_STORAGE, 10000)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your stamina started regenerating.\n\nYou will receive 1 minute stamina for each 10 minutes training.")
    return true
end

Returning a value as false prevents you from stepping on a tile, casting a spell, using an item etc..
 
Last edited by a moderator:
Try this
Code:
local STORAGE = 616261
local EXHAUST_STORAGE = 616262

local function loopHealStamina(playerId)
    local player = Player(playerId)

    local stamina = player:getStamina()
    if stamina == 2520 then
        return
    end

    player:setStamina(stamina + 1)
    addEvent(loopHealStamina, 1000, playerId)
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() or player:getExhaustion(EXHAUST_STORAGE) == 1 then
        return false
    end

    player:setStorageValue(STORAGE, 1)
    loopHealStamina(player:getId())
    player:setExhaustion(EXHAUST_STORAGE, 10000)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your stamina started regenerating.\n\nYou will receive 1 minute stamina for each 10 minutes training.")
    return true
end

Returning a value as false prevents you from stepping on a tile, casting a spell, using an item etc..

yeah but that's not the issue at all XD

true that in my script I forgot to make if player had full stamina to stop the event, but he's still allowed to train, so thats why return true in custom function

but in ur script the loop issue still continues

The way it's suppost to work is:

You step in trainers if ur stamina is under 42 it will heal your stamina each 10 minutes while you are in those trainers thats why setStorage 616261, 1 on stepIn and 0 on stepOut, so the script knows if the player is training or not

I just need a way to add cooldown to the loop function, the rest of the script is working :p
 
Didn't really have time this morning to look over the metamethods, such as getExhaustion, i was late for work, however, i would have written your script a bit differently to handle most possibilities..

Am at work at the moment, so I will have to look at this later when I am home or not so busy.
I just thought this warranted a response.
 
I would have done something like this. I'm assuming that Player:getExhaustion(...) returns the remaining time in seconds, I'm not entirely sure if that's the case though.
Code:
local STORAGE = 616261
local EXHAUST_STORAGE = 616262
local INTERVAL = 10 * 60 -- 10 minutes in seconds

local function loopHealStamina(playerId)
    local player = Player(playerId)
    if not player or player:getStorageValue(STORAGE) ~= 1 then
        return
    end

    local exhaustion = player:getExhaustion(EXHAUST_STORAGE)
    if exhaustion >= 1 then
        addEvent(loopHealStamina, exhaustion * 1000, playerId)
        return
    end

    local stamina = player:getStamina()
    if stamina == 2520 then
        return
    end
    player:setStamina(stamina + 1)
    player:setExhaustion(EXHAUST_STORAGE, INTERVAL * 1000)
    addEvent(loopHealStamina, INTERVAL * 1000, playerId)
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if not player or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(STORAGE, 1)
    loopHealStamina(player:getId())
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your stamina started regenerating.\n\nYou will receive 1 minute stamina for each 10 minutes training.")
    return true
end


function onStepOut(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if not player or player:isInGhostMode() then
        return true
    end

    player:setStorageValue(STORAGE, 0)
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Your stamina stopped regenerating.")
    return true
end
 
no errors and it doesnt work XD
but so far yeah what u made makes sence

btw how to store stamina that player started with when he step on trainers?
 
Last edited:
sorry for double post but it's worth it!! XD

took me alot of time to figure out what I was doing wrong but it was worth it!! XD

the script looks way more messy now but at least it Works :p

here it is for those in need!

Code:
local STORAGE = 616261
local EXHAUST_STORAGE = 616262

local function trainMe(playerId)
    local player = Player(playerId)
    if not player then
        return
    end

    local stamina = player:getStamina()
    if stamina == 2520 then
        return
    end

    if player:getStorageValue(STORAGE) == 0 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Your stamina stopped regenerating.")
    return false
    end

    if player:getStorageValue(STORAGE) >= 1 and player:getExhaustion(EXHAUST_STORAGE) >= 1 then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "Your next stamina minute will regen in: "..player:getExhaustion(EXHAUST_STORAGE).." ")
        addEvent(trainMe, 1000, playerId)
    elseif player:getStorageValue(STORAGE) >= 1 and player:getExhaustion(EXHAUST_STORAGE) <= 0 then
        player:setStamina((player:getStamina() + 1))
        player:setExhaustion(EXHAUST_STORAGE, 600)
        addEvent(trainMe, 1000, playerId)
    end
end

function onStepIn(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return false
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your stamina started regenerating.")
    player:setStorageValue(STORAGE, 1)
    trainMe(player:getId())
return true
end

function onStepOut(creature, item, position, fromPosition)
    local player = creature:getPlayer()
    if player == nil or player:isInGhostMode() then
        return false
    end

    player:setStorageValue(STORAGE, 0)
    trainMe(player:getId())
return true
end
 
Back
Top