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

Help stamina training online

robertorb12

New Member
Joined
Jan 15, 2020
Messages
65
Reaction score
3
Hello, i want to implement that when attacking a monster (in this case training monk) give 1 min of stamina every 3 minutes hitting him

Can somebody help me? đź‘Ť
 
Solution
Here's the code if anyone want/needs it.
register to the creature only, not login.lua

data/monsters/training_monk.xml [under immunities]
XML:
<script>
    <event name="onStatsChange_staminaTraining"/>
</script>
data/creaturescripts/creaturescripts.xml
XML:
<event type="statschange" name="onStatsChange_staminaTraining" event="script" value="onStatsChange_staminaTraining.lua"/>
data/creaturescripts/scripts/onStatsChange_staminaTraining.lua
Lua:
local creatures = {}
local config = {
    timer = 180, -- time in seconds, before you gain stamina while hitting monster_name
    monster_name = "training monk",
    stamina_gain = 1, -- amount to gain after time elapse.
    threshold = 1000 -- how tight a timer before script...
How can i add the script to this
Lua:
local mType = Game.createMonsterType("Training Machine")
local monster = {}
monster.description = "Training Machine"
monster.experience = 0
monster.outfit = {
    lookType = 1142
}

monster.health = 1000000
monster.maxHealth = monster.health
monster.race = "energy"
monster.corpse = 0
monster.speed = 0
monster.maxSummons = 0

monster.changeTarget = {
    interval = 1*1000,
    chance = 0
}

monster.flags = {
    summonable = false,
    attackable = true,
    hostile = true,
    convinceable = false,
    illusionable = false,
    canPushItems = true,
    canPushCreatures = true,
    targetDistance = 1,
    staticAttackChance = 100,
}

monster.summons = {
}

monster.voices = {
    interval = 5000,
    chance = 10,
    {text = "I hope you are enjoying your sparring Sir or Ma'am!", yell = false},
    {text = "Threat level rising!", yell = false},
    {text = "Engaging in hostile interaction!", yell = false},
    {text = "Rrrtttarrrttarrrtta", yell = false},
    {text = "Please feel free to hit me Sir or Ma'am!", yell = false},
    {text = "klonk klonk klonk", yell = false},
    {text = "Self-diagnosis running.", yell = false},
    {text = "Battle simulation proceeding.", yell = false},
    {text = "Repairs initiated!", yell = false}
}

monster.loot = {
}

monster.attacks = {
    {name = "melee", attack = 130, effect = CONST_ME_DRAWBLOOD, interval = 2*1000, minDamage = -1, maxDamage = -2}
}

monster.defenses = {
    defense = 1,
    armor = 1,
    {name = "combat", type = COMBAT_HEALING, chance = 15, interval = 2*1000, minDamage = 10000, maxDamage = 50000, effect = CONST_ME_MAGIC_BLUE}
}

monster.elements = {
}

monster.immunities = {
}

--[[
mType.onThink = function(monster, interval)
    print("I'm thinking")
end

mType.onAppear = function(monster, creature)
    if monster:getId() == creature:getId() then
        print(monster:getId(), creature:getId())
    end
end

mType.onDisappear = function(monster, creature)
    if monster:getId() == creature:getId() then
        print(monster:getId(), creature:getId())
    end
end

mType.onMove = function(monster, creature, fromPosition, toPosition)
    if monster:getId() == creature:getId() then
        print(monster:getId(), creature:getId(), fromPosition, toPosition)
    end
end

mType.onSay = function(monster, creature, type, message)
    print(monster:getId(), creature:getId(), type, message)
end
]]

mType:register(monster)

When using a RevScript monster, you need to use the onAppear function, make the creature find itself, then register the event.
Lua:
mType.onAppear = function(monster, creature)
    if monster:getId() == creature:getId() then
        monster:registerEvent("onHealthChange_Training_Dummy")
    end
end
 
Does that work with otservbr-global-x64 1.3 ?

this one does
 
Here's the code if anyone want/needs it.
register to the creature only, not login.lua

data/monsters/training_monk.xml [under immunities]
XML:
<script>
    <event name="onStatsChange_staminaTraining"/>
</script>
data/creaturescripts/creaturescripts.xml
XML:
<event type="statschange" name="onStatsChange_staminaTraining" event="script" value="onStatsChange_staminaTraining.lua"/>
data/creaturescripts/scripts/onStatsChange_staminaTraining.lua
Lua:
local creatures = {}
local config = {
    timer = 180, -- time in seconds, before you gain stamina while hitting monster_name
    monster_name = "training monk",
    stamina_gain = 1, -- amount to gain after time elapse.
    threshold = 1000 -- how tight a timer before script cancels counting of timer. (2000 + (threshold of 1000) = need to hit blood every 3 seconds)
}


function onStatsChange(cid, attacker, type, combat, value)
    if type ~= STATSCHANGE_HEALTHLOSS then
        return true
    end

    if not isPlayer(attacker) then
        return true
    end
    if not isMonster(cid) then
        return true
    end
    if getCreatureName(cid):lower() ~= config.monster_name:lower() then
        return true
    end
 
    local current_time = os.mtime()
    if not creatures[cid] then
        creatures[cid] = {current_time, current_time} -- {start_time, last_hit_time}
        return true
    end

    -- check if too much time has elasped between last blood hit
    local time_difference = current_time - creatures[cid][2] -- between last hit and current time
    if time_difference > 2000 + config.threshold then
        creatures[cid] = {current_time, current_time}
        return true
    end
    creatures[cid][2] = current_time
 
    -- check if stamina timer has elasped
    time_difference = current_time - creatures[cid][1] -- time between first attack and current time.
    if time_difference < 1000 * config.timer then
        return true
    end
 
    -- give stamina and adjust timers for future stamina gain (2520 is max stamina)
    creatures[cid][1] = creatures[cid][1] + (1000 * config.timer)
 
 
    local stamina_to_add = getPlayerStamina(attacker) + config.stamina_gain
    if stamina_to_add >= 2520 then
        stamina_to_add = 2520 - getPlayerStamina(attacker)
    end
    if stamina_to_add <= 0 then -- stamina is already full
        return true
    end
    
    doPlayerSetStamina(attacker, stamina_to_add)
    doPlayerAddExperience(attacker, 1) -- neccesary to update client with new stamina information.
    addEvent(doPlayerAddExperience, 0, attacker, -1)
    return true
end
HEY BRO Does it work for otbr tfs 1.3? can you do it in revscriptsys?
 
Here's the code if anyone want/needs it.
register to the creature only, not login.lua

data/monsters/training_monk.xml [under immunities]
XML:
<script>
    <event name="onStatsChange_staminaTraining"/>
</script>
data/creaturescripts/creaturescripts.xml
XML:
<event type="statschange" name="onStatsChange_staminaTraining" event="script" value="onStatsChange_staminaTraining.lua"/>
data/creaturescripts/scripts/onStatsChange_staminaTraining.lua
Lua:
local creatures = {}
local config = {
    timer = 180, -- time in seconds, before you gain stamina while hitting monster_name
    monster_name = "training monk",
    stamina_gain = 1, -- amount to gain after time elapse.
    threshold = 1000 -- how tight a timer before script cancels counting of timer. (2000 + (threshold of 1000) = need to hit blood every 3 seconds)
}


function onStatsChange(cid, attacker, type, combat, value)
    if type ~= STATSCHANGE_HEALTHLOSS then
        return true
    end

    if not isPlayer(attacker) then
        return true
    end
    if not isMonster(cid) then
        return true
    end
    if getCreatureName(cid):lower() ~= config.monster_name:lower() then
        return true
    end
 
    local current_time = os.mtime()
    if not creatures[cid] then
        creatures[cid] = {current_time, current_time} -- {start_time, last_hit_time}
        return true
    end

    -- check if too much time has elasped between last blood hit
    local time_difference = current_time - creatures[cid][2] -- between last hit and current time
    if time_difference > 2000 + config.threshold then
        creatures[cid] = {current_time, current_time}
        return true
    end
    creatures[cid][2] = current_time
 
    -- check if stamina timer has elasped
    time_difference = current_time - creatures[cid][1] -- time between first attack and current time.
    if time_difference < 1000 * config.timer then
        return true
    end
 
    -- give stamina and adjust timers for future stamina gain (2520 is max stamina)
    creatures[cid][1] = creatures[cid][1] + (1000 * config.timer)
 
 
    local stamina_to_add = getPlayerStamina(attacker) + config.stamina_gain
    if stamina_to_add >= 2520 then
        stamina_to_add = 2520 - getPlayerStamina(attacker)
    end
    if stamina_to_add <= 0 then -- stamina is already full
        return true
    end
    
    doPlayerSetStamina(attacker, stamina_to_add)
    doPlayerAddExperience(attacker, 1) -- neccesary to update client with new stamina information.
    addEvent(doPlayerAddExperience, 0, attacker, -1)
    return true
end
Hello
Sorry for interruption.
I am trying to use this script but there is a problem.
It works fine until it comes to 41:59, the next step goes from 41:59 to 0:01.
No idea what the problem is here.
Thanks in advance.
 
Hello
Sorry for interruption.
I am trying to use this script but there is a problem.
It works fine until it comes to 41:59, the next step goes from 41:59 to 0:01.
No idea what the problem is here.
Thanks in advance.
change
Lua:
local stamina_to_add = getPlayerStamina(attacker) + config.stamina_gain
if stamina_to_add >= 2520 then
    stamina_to_add = 2520 - getPlayerStamina(attacker)
end
if stamina_to_add <= 0 then -- stamina is already full
    return true
end
to
Lua:
local currentStamina = getPlayerStamina(attacker)
local maxStamina = 2520
if currentStamina == maxStamina then
    return true
end

local stamina_to_add = currentStamina + config.stamina_gain
if stamina_to_add > maxStamina then
    stamina_to_add = maxStamina
end
 
change
Lua:
local stamina_to_add = getPlayerStamina(attacker) + config.stamina_gain
if stamina_to_add >= 2520 then
    stamina_to_add = 2520 - getPlayerStamina(attacker)
end
if stamina_to_add <= 0 then -- stamina is already full
    return true
end
to
Lua:
local currentStamina = getPlayerStamina(attacker)
local maxStamina = 2520
if currentStamina == maxStamina then
    return true
end

local stamina_to_add = currentStamina + config.stamina_gain
if stamina_to_add > maxStamina then
    stamina_to_add = maxStamina
end
It works fine now, Thanks.
 
@Xikini
Sorry for mention but if i want to add an option for green stamina and another for red stamina like in config? what can be done here?
 
@Xikini
Sorry for mention but if i want to add an option for green stamina and another for red stamina like in config? what can be done here?
2520 / 60 = 42 hours
So figure out where green stamina starts/ends, and where red stamina starts/ends.. then make an if statement.

Example (with numbers pulled out my.. cuz I have no idea what green/red stamina is)
Lua:
local currentStamina = getPlayerStamina(attacker)
local maxStamina = 2520
if currentStamina == maxStamina then
    return true
end

local stamina_to_add = config.stamina_gain

if currentStamina < 1400 then -- if 'red stamina' add extra
    stamina_to_add = stamina_to_add + 20
elseif currentStamina > 2220 then -- if 'green stamina', reduce by 1/3. (with a minimum of 1, since that's the lowest amount of time that can be given)
    stamina_to_add = math.max(math.floor(stamina_to_add / 3), 1)
end

local newStamina = currentStamina + stamina_to_add

if newStamina > maxStamina then
    newStamina = maxStamina
end
 
2520 / 60 = 42 hours
So figure out where green stamina starts/ends, and where red stamina starts/ends.. then make an if statement.

Example (with numbers pulled out my.. cuz I have no idea what green/red stamina is)
Lua:
local currentStamina = getPlayerStamina(attacker)
local maxStamina = 2520
if currentStamina == maxStamina then
    return true
end

local stamina_to_add = config.stamina_gain

if currentStamina < 1400 then -- if 'red stamina' add extra
    stamina_to_add = stamina_to_add + 20
elseif currentStamina > 2220 then -- if 'green stamina', reduce by 1/3. (with a minimum of 1, since that's the lowest amount of time that can be given)
    stamina_to_add = math.max(math.floor(stamina_to_add / 3), 1)
end

local newStamina = currentStamina + stamina_to_add

if newStamina > maxStamina then
    newStamina = maxStamina
end
Oh sorry my bad, i meant green stamina from 40h to 42h and less than 40h is red stamina.
Every 3 mins you gain 1 minute in green stamina and every 2 mins you gain 1 minute in red stamina.
Depends on what is your stamina is.
I think the change should be in the timer?
 
Oh sorry my bad, i meant green stamina from 40h to 42h and less than 40h is red stamina.
Every 3 mins you gain 1 minute in green stamina and every 2 mins you gain 1 minute in red stamina.
Depends on what is your stamina is.
I think the change should be in the timer?

Good luck. I'm not editing this script again. lol

Lua:
local creatures = {}
local config = {
    timer = 120, -- time in seconds, before you gain stamina while hitting monster_name
    greenStaminaThreshhold = 2400, -- 60*40h
    greenStaminaAdditionalTime = 60, -- 120 + 60
    monster_name = "training monk",
    stamina_gain = 1, -- amount to gain after time elapse.
    threshold = 1000 -- how tight a timer before script cancels counting of timer. (2000 + (threshold of 1000) = need to hit blood every 3 seconds)
}


function onStatsChange(cid, attacker, type, combat, value)
    if type ~= STATSCHANGE_HEALTHLOSS then
        return true
    end

    if not isPlayer(attacker) then
        return true
    end
    if not isMonster(cid) then
        return true
    end
    if getCreatureName(cid):lower() ~= config.monster_name:lower() then
        return true
    end
 
    local current_time = os.mtime()
    if not creatures[cid] then
        creatures[cid] = {current_time, current_time} -- {start_time, last_hit_time}
        return true
    end

    -- check if too much time has elasped between last blood hit
    local time_difference = current_time - creatures[cid][2] -- between last hit and current time
    if time_difference > 2000 + config.threshold then
        creatures[cid] = {current_time, current_time}
        return true
    end
    creatures[cid][2] = current_time
   
    local currentStamina = getPlayerStamina(attacker)
 
    -- check if stamina timer has elasped
    local checkTime = config.timer + (currentStamina > config.greenStaminaThreshhold and greenStaminaAdditionalTime or 0)
    time_difference = current_time - creatures[cid][1] -- time between first attack and current time.
    if time_difference < 1000 * checkTime then
        return true
    end
 
    -- give stamina and adjust timers for future stamina gain (2520 is max stamina)
    creatures[cid][1] = creatures[cid][1] + (1000 * checkTime)
 
 
    local maxStamina = 2520
    if currentStamina == maxStamina then
        return true
    end
   
    local newStamina = math.min(currentStamina + config.stamina_gain, maxStamina)
    
    doPlayerSetStamina(attacker, newStamina)
    doPlayerAddExperience(attacker, 1) -- neccesary to update client with new stamina information.
    addEvent(doPlayerAddExperience, 0, attacker, -1)
    return true
end
 
Good luck. I'm not editing this script again. lol

Lua:
local creatures = {}
local config = {
    timer = 120, -- time in seconds, before you gain stamina while hitting monster_name
    greenStaminaThreshhold = 2400, -- 60*40h
    greenStaminaAdditionalTime = 60, -- 120 + 60
    monster_name = "training monk",
    stamina_gain = 1, -- amount to gain after time elapse.
    threshold = 1000 -- how tight a timer before script cancels counting of timer. (2000 + (threshold of 1000) = need to hit blood every 3 seconds)
}


function onStatsChange(cid, attacker, type, combat, value)
    if type ~= STATSCHANGE_HEALTHLOSS then
        return true
    end

    if not isPlayer(attacker) then
        return true
    end
    if not isMonster(cid) then
        return true
    end
    if getCreatureName(cid):lower() ~= config.monster_name:lower() then
        return true
    end
 
    local current_time = os.mtime()
    if not creatures[cid] then
        creatures[cid] = {current_time, current_time} -- {start_time, last_hit_time}
        return true
    end

    -- check if too much time has elasped between last blood hit
    local time_difference = current_time - creatures[cid][2] -- between last hit and current time
    if time_difference > 2000 + config.threshold then
        creatures[cid] = {current_time, current_time}
        return true
    end
    creatures[cid][2] = current_time
  
    local currentStamina = getPlayerStamina(attacker)
 
    -- check if stamina timer has elasped
    local checkTime = config.timer + (currentStamina > config.greenStaminaThreshhold and greenStaminaAdditionalTime or 0)
    time_difference = current_time - creatures[cid][1] -- time between first attack and current time.
    if time_difference < 1000 * checkTime then
        return true
    end
 
    -- give stamina and adjust timers for future stamina gain (2520 is max stamina)
    creatures[cid][1] = creatures[cid][1] + (1000 * checkTime)
 
 
    local maxStamina = 2520
    if currentStamina == maxStamina then
        return true
    end
  
    local newStamina = math.min(currentStamina + config.stamina_gain, maxStamina)
   
    doPlayerSetStamina(attacker, newStamina)
    doPlayerAddExperience(attacker, 1) -- neccesary to update client with new stamina information.
    addEvent(doPlayerAddExperience, 0, attacker, -1)
    return true
end
Thanks man 🙏 much appreciated :)
 
Here's the code if anyone want/needs it.
register to the creature only, not login.lua

data/monsters/training_monk.xml [under immunities]
XML:
<script>
    <event name="onStatsChange_staminaTraining"/>
</script>
data/creaturescripts/creaturescripts.xml
XML:
<event type="statschange" name="onStatsChange_staminaTraining" event="script" value="onStatsChange_staminaTraining.lua"/>
data/creaturescripts/scripts/onStatsChange_staminaTraining.lua
Lua:
local creatures = {}
local config = {
    timer = 180, -- time in seconds, before you gain stamina while hitting monster_name
    monster_name = "training monk",
    stamina_gain = 1, -- amount to gain after time elapse.
    threshold = 1000 -- how tight a timer before script cancels counting of timer. (2000 + (threshold of 1000) = need to hit blood every 3 seconds)
}


function onStatsChange(cid, attacker, type, combat, value)
    if type ~= STATSCHANGE_HEALTHLOSS then
        return true
    end

    if not isPlayer(attacker) then
        return true
    end
    if not isMonster(cid) then
        return true
    end
    if getCreatureName(cid):lower() ~= config.monster_name:lower() then
        return true
    end
 
    local current_time = os.mtime()
    if not creatures[cid] then
        creatures[cid] = {current_time, current_time} -- {start_time, last_hit_time}
        return true
    end

    -- check if too much time has elasped between last blood hit
    local time_difference = current_time - creatures[cid][2] -- between last hit and current time
    if time_difference > 2000 + config.threshold then
        creatures[cid] = {current_time, current_time}
        return true
    end
    creatures[cid][2] = current_time
 
    -- check if stamina timer has elasped
    time_difference = current_time - creatures[cid][1] -- time between first attack and current time.
    if time_difference < 1000 * config.timer then
        return true
    end
 
    -- give stamina and adjust timers for future stamina gain (2520 is max stamina)
    creatures[cid][1] = creatures[cid][1] + (1000 * config.timer)
 
 
    local stamina_to_add = getPlayerStamina(attacker) + config.stamina_gain
    if stamina_to_add >= 2520 then
        stamina_to_add = 2520 - getPlayerStamina(attacker)
    end
    if stamina_to_add <= 0 then -- stamina is already full
        return true
    end
    
    doPlayerSetStamina(attacker, stamina_to_add)
    doPlayerAddExperience(attacker, 1) -- neccesary to update client with new stamina information.
    addEvent(doPlayerAddExperience, 0, attacker, -1)
    return true
end
Hi

I am using TFS 1.5 and getting following error while loading - Any idea how to fix this?

[Error - CreatureEvent::configureEvent] Invalid type for creature event: onStatsChange_staminaTraining
[Warning - BaseEvents::loadFromXml] Failed to configure event
 
Hi

I am using TFS 1.5 and getting following error while loading - Any idea how to fix this?

[Error - CreatureEvent::configureEvent] Invalid type for creature event: onStatsChange_staminaTraining
[Warning - BaseEvents::loadFromXml] Failed to configure event
there is no onStatsChange event on tfs 1.5, you will need to adapt it to onHealthChange event
 
there is no onStatsChange event on tfs 1.5, you will need to adapt it to onHealthChange event
Would you mind sharing how that is done? I tried with the simplest thing my brain could work out which is simply changing the wording from onStatsChange to onHealthChange but simply switching that word out didn't do the trick.
(Tfs 1.5 client 8.6)
Thanks in advance!
 
Back
Top