Svira
Active Member
- Joined
- Jan 27, 2008
- Messages
- 269
- Solutions
- 11
- Reaction score
- 37
Hello dear community, I am creating a code to increase HP and MP for 1 hour, but if the player uses the item and then remains logged out, a crash occurs after some time? how to prevent it?
CODE:
CODE:
Lua:
local BOOST_DURATION = 3600 -- w sekundach
local STORAGE_VALUE = 224411
local BOOST_EVENT_NAME = "profession_boost"
function onUse(cid, item, fromPosition, itemEx, toPosition)
local player = Player(cid)
if not player then
return false
end
if player:getStorageValue(STORAGE_VALUE) >= os.time() then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You already have a profession-specific boost!')
return true
end
local oldHealth = player:getHealth()
local oldMana = player:getMana()
local hpBoost = Condition(CONDITION_ATTRIBUTES)
hpBoost:setParameter(CONDITION_PARAM_SUBID, 31)
hpBoost:setParameter(CONDITION_PARAM_TICKS, BOOST_DURATION * 1000)
hpBoost:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTSPERCENT, 200)
hpBoost:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
player:addCondition(hpBoost)
local manaBoost = Condition(CONDITION_ATTRIBUTES)
manaBoost:setParameter(CONDITION_PARAM_SUBID, 32)
manaBoost:setParameter(CONDITION_PARAM_TICKS, BOOST_DURATION * 1000)
manaBoost:setParameter(CONDITION_PARAM_STAT_MAXMANAPOINTSPERCENT, 200)
manaBoost:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
player:addCondition(manaBoost)
player:setStorageValue(STORAGE_VALUE, os.time() + BOOST_DURATION)
Item(item.uid):remove(1)
-- Wysłanie informacji o przyznanym boost
local newHealth = player:getMaxHealth()
local newMana = player:getMaxMana()
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have received a profession-specific boost for the next hour! Your maximum hit points and mana points have been increased by 200%. Your health was ' .. oldHealth .. ' and now is ' .. newHealth .. '. Your mana was ' .. oldMana .. ' and now is ' .. newMana .. '. Enjoy your adventures!')
local stopEventFunc
stopEventFunc = function()
if player:getStorageValue(STORAGE_VALUE) >= os.time() then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Your profession-specific boost has expired!')
player:removeCondition(CONDITION_ATTRIBUTES, CONDITION_PARAM_SUBID, 31)
player:removeCondition(CONDITION_ATTRIBUTES, CONDITION_PARAM_SUBID, 32)
player:setStorageValue(STORAGE_VALUE, 0)
stopEvent(BOOST_EVENT_NAME)
end
end
addEvent(stopEventFunc, BOOST_DURATION * 1000, BOOST_EVENT_NAME)
return true
end
function onLogout(player)
if not player then
return true
end
local playerId = player:getId()
local storageValue = player:getStorageValue(STORAGE_VALUE)
if storageValue > 0 then
local remainingTime = storageValue - os.time()
player:setStorageValue(STORAGE_VALUE, remainingTime)
stopEvent(BOOST_EVENT_NAME)
end
return true
end
function onLogin(player)
local playerId = player:getId()
local storageValue = player:getStorageValue(STORAGE_VALUE)
if storageValue > 0 then
local remainingTime = storageValue - os.time()
if remainingTime > 0 then
local hpBoost = Condition(CONDITION_ATTRIBUTES)
hpBoost:setParameter(CONDITION_PARAM_SUBID, 31)
hpBoost:setParameter(CONDITION_PARAM_TICKS, remainingTime * 1000)
hpBoost:setParameter(CONDITION_PARAM_STAT_MAXHITPOINTSPERCENT, 200)
hpBoost:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
player:addCondition(hpBoost)
local manaBoost = Condition(CONDITION_ATTRIBUTES)
manaBoost:setParameter(CONDITION_PARAM_SUBID, 32)
manaBoost:setParameter(CONDITION_PARAM_TICKS, remainingTime * 1000)
manaBoost:setParameter(CONDITION_PARAM_STAT_MAXMANAPOINTSPERCENT, 200)
manaBoost:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
player:addCondition(manaBoost)
addEvent(function()
if player:getStorageValue(STORAGE_VALUE) >= os.time() then
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Your profession-specific boost has expired!')
player:removeCondition(CONDITION_ATTRIBUTES, CONDITION_PARAM_SUBID, 31)
player:removeCondition(CONDITION_ATTRIBUTES, CONDITION_PARAM_SUBID, 32)
player:setStorageValue(STORAGE_VALUE, 0)
end
end, remainingTime * 1000)
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Your profession-specific boost has been resumed for the remaining time!')
else
player:setStorageValue(STORAGE_VALUE, 0)
end
end
return true
end