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

Bug with script regen stamina in Protect Zone TFS 1.3

Grillo1995

New Member
Joined
Feb 8, 2018
Messages
21
Solutions
1
Reaction score
1
Location
Brazil
Hello, I need help with this script.

I made the changes in the source to add onChangeZone and so far so good.

However these scripts are bug, he add more stamina after leaving the protect zone does not stop adding! It does not end the event of adding stamina!

There is no mistake, only the player is gaining stamina.

data/global.lua

Code:
function addStamina(id, amountStamina, delay)
    local staminaRegen = {}
    local event = staminaRegen[id]

    local player = Player(id)
    if not player then
        stopEvent(event)
        staminaRegen[id] = nil
        return false
    end

    local actualStamina = player:getStamina()

    if actualStamina > 2400 and actualStamina < 2520 then
        delay = 12 * 60 * 1000 -- Stamina verde 12 mins
    elseif actualStamina == 2520 then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You are no longer refilling stamina, because your stamina is already full.")
        stopEvent(event)
        staminaRegen[id] = nil
        return false
    end  
  

    player:setStamina(actualStamina + 1)
    player:sendTextMessage(MESSAGE_STATUS_SMALL, "One minute of stamina has been refilled.")

    stopEvent(event)
    staminaRegen[id] = addEvent(addStamina, delay, id, amountStamina, delay)
    return true
end

events/scripts/player.lua

Code:
function Player:onChangeZone(zone)
    if not self:isPremium() then
        return false
    end
    local staminaRegen = {}
    local event = staminaRegen[self:getId()]

    if zone == ZONE_PROTECTION then
        if self:getStamina() < 2520 then
            if not event then
                local delay = 2
                if self:getStamina() > 2400 and self:getStamina() <= 2520 then
                    delay = 8
                end

                staminaRegen[self:getId()] = addEvent(addStamina, delay * 60 * 1000, self:getId(), 1, delay * 60 * 1000)  
            end
        end
    else
        if zone ~= ZONE_PROTECTION then
            self:sendTextMessage(MESSAGE_STATUS_SMALL, "You are no longer refilling stamina, since you left a regeneration zone.")
            stopEvent(event)
            stopEvent(staminaRegen[self:getId()])
            staminaRegen[self:getId()] = nil
            return false
        end
    end
end

When I left the protection zone I got the message in the game: You are no longer refilling stamina, since you left a regeneration zone.

However, he continues to gain stamina.

How to solve? Thank you and I'm waiting!
 
Last edited:
Solution
You can also add support to stop the event in the case that addStamina somehow gets executed if the player is no longer in a protection zone.
Lua:
function addStamina(id, amountStamina, delay)
    local staminaRegen = {}
    local event = staminaRegen[id]

    local player = Player(id)
    if not player or not Tile(player:getPosition()):hasFlag(TILESTATE_PROTECTIONZONE) then
        stopEvent(event)
        staminaRegen[id] = nil
        return false
    end

    local actualStamina = player:getStamina()

    if actualStamina > 2400 and actualStamina < 2520 then
        delay = 12 * 60 * 1000 -- Stamina verde 12 mins
    elseif actualStamina == 2520 then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You are no longer refilling...
Since there are no errors try using debug.print("TEXT") to try and isolate what is working and what is not working to help narrow down the issue.


Code:
function Player:onChangeZone(zone)
    if not self:isPremium() then
        return false
    end
    local staminaRegen = {}
    local event = staminaRegen[self:getId()]

    if zone == ZONE_PROTECTION then
        if self:getStamina() < 2520 then
            if not event then
                local delay = 2
                if self:getStamina() > 2400 and self:getStamina() <= 2520 then
                    delay = 8
                end

                staminaRegen[self:getId()] = addEvent(addStamina, delay * 60 * 1000, self:getId(), 1, delay * 60 * 1000) 
            end
        end
    else
        if zone ~= ZONE_PROTECTION then
            self:sendTextMessage(MESSAGE_STATUS_SMALL, "You are no longer refilling stamina, since you left a regeneration zone.")
            debug.print("Message Sent")
            stopEvent(event)
            debug.print("Event stopped")
            stopEvent(staminaRegen[self:getId()])
            staminaRegen[self:getId()] = nil
            return false
        end
    end
end
 
You can also add support to stop the event in the case that addStamina somehow gets executed if the player is no longer in a protection zone.
Lua:
function addStamina(id, amountStamina, delay)
    local staminaRegen = {}
    local event = staminaRegen[id]

    local player = Player(id)
    if not player or not Tile(player:getPosition()):hasFlag(TILESTATE_PROTECTIONZONE) then
        stopEvent(event)
        staminaRegen[id] = nil
        return false
    end

    local actualStamina = player:getStamina()

    if actualStamina > 2400 and actualStamina < 2520 then
        delay = 12 * 60 * 1000 -- Stamina verde 12 mins
    elseif actualStamina == 2520 then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You are no longer refilling stamina, because your stamina is already full.")
        stopEvent(event)
        staminaRegen[id] = nil
        return false
    end  
  

    player:setStamina(actualStamina + 1)
    player:sendTextMessage(MESSAGE_STATUS_SMALL, "One minute of stamina has been refilled.")

    stopEvent(event)
    staminaRegen[id] = addEvent(addStamina, delay, id, amountStamina, delay)
    return true
end
 
Solution
Lua Script Error: [Event Interface]
data/events/scripts/player.lua:player@onChangeZone
data/events/scripts/player.lua:138: attempt to call field 'print' (a nil value)
stack traceback:
[C]: in function 'print'
data/events/scripts/player.lua:138: in function <data/events/scripts/player.lua:117>
Post automatically merged:

Since there are no errors try using debug.print("TEXT") to try and isolate what is working and what is not working to help narrow down the issue.


Code:
function Player:onChangeZone(zone)
    if not self:isPremium() then
        return false
    end
    local staminaRegen = {}
    local event = staminaRegen[self:getId()]

    if zone == ZONE_PROTECTION then
        if self:getStamina() < 2520 then
            if not event then
                local delay = 2
                if self:getStamina() > 2400 and self:getStamina() <= 2520 then
                    delay = 8
                end

                staminaRegen[self:getId()] = addEvent(addStamina, delay * 60 * 1000, self:getId(), 1, delay * 60 * 1000)
            end
        end
    else
        if zone ~= ZONE_PROTECTION then
            self:sendTextMessage(MESSAGE_STATUS_SMALL, "You are no longer refilling stamina, since you left a regeneration zone.")
            debug.print("Message Sent")
            stopEvent(event)
            debug.print("Event stopped")
            stopEvent(staminaRegen[self:getId()])
            staminaRegen[self:getId()] = nil
            return false
        end
    end
end

Ok thank!
Apparently it worked! Now I just wanted to make some changes that could help me?

I want that when the Player has red / orange stamina to recover: 1 minute of stamina every 3 minutes;

And when it is green, recover 1 minute of stamina every 6 minutes!

Thank you if you can help me thank you.

Another question, what does this line mean? I was confused since there is a delay here, and in the player.lua too!

if actualStamina> 2400 and actualStamina <2520 then
delay = 12 * 60 * 1000 - Green Stamina 12 mins
Post automatically merged:

I identified an error too, instead of adding 1 minute it adds 2 minutes of stamina, one at a time!

15:03 One minute of stamina has been refilled.

15:03 One minute of stamina has been refilled.

What will be the error in the script?
Post automatically merged:

From what I saw, there are hours that work well, and there are hours that add 2 minutes TOGETHER! it seems that only the first time I enter the protect zone the error happens, when I left and I enter again the error ends!

15:11 One minute of stamina has been refilled.
15:13 One minute of stamina has been refilled.
15:15 One minute of stamina has been refilled.

However, when he left the protect zone, he was stopping gaining stamina, that was already fixed!
 
Last edited:
Back
Top