• 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 How to make something last x seconds

reisbro

Member
Joined
May 15, 2017
Messages
94
Solutions
1
Reaction score
7
So I am testing this simple script because I have a future project in mind that will use a similar code. Player uses a talkaction and receives 50 health. The action has 5 seconds cooldown as you can see below. This script is working fine btw, I tested it and found no issues.

Lua:
local ex_storage = 9919
local ex_time = 5

function onSay(cid, words, param)
if exhaustion.get(cid, ex_storage) then
        local remaining = exhaustion.get(cid, ex_storage)
        doPlayerSendCancel(cid, 'You have to wait ' .. remaining .. ' more seconds.')
        return true
    end
    exhaustion.set(cid, ex_storage, ex_time)
    doCreatureAddHealth(cid, 50)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_HEARTS)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Rawr!")
return true
end

However, I want to make the player get damaged by 10 hit points after like 2 seconds of using the talkaction. How can I make something happen to the player after X seconds of using a talkaction?
 
Lua:
local ex_storage = 9919
local ex_time = 5
local damage = -10
function onSay(cid, words, param)
    if exhaustion.get(cid, ex_storage) then
        local remaining = exhaustion.get(cid, ex_storage)
        if remaining % 2 == 0 then
            doCreatureAddHealth(cid, damage)
        end
        doPlayerSendCancel(cid, 'You have to wait ' .. remaining .. ' more seconds.')
        return true
    end
    exhaustion.set(cid, ex_storage, ex_time)
    doCreatureAddHealth(cid, 50)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_HEARTS)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Rawr!")
    return true
end

This means if the value of remaining modulo 2, meaning remaining / 2 and if there is no remainder then the expression evaluates to true
Lua:
remaining % 2 == 0
 
Last edited:
Lua:
local ex_storage = 9919
local ex_time = 5
local damage = -10
function onSay(cid, words, param)
    if exhaustion.get(cid, ex_storage) then
        local remaining = exhaustion.get(cid, ex_storage)
        if remaining % 2 == 0 then
            doCreatureAddHealth(cid, damage)
        end
        doPlayerSendCancel(cid, 'You have to wait ' .. remaining .. ' more seconds.')
        return true
    end
    exhaustion.set(cid, ex_storage, ex_time)
    doCreatureAddHealth(cid, 50)
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_HEARTS)
    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Rawr!")
    return true
end

This means if the value of remaining modulo 2, meaning remaining / 2 and if there is no remainder then the expression evaluates to true
Lua:
remaining % 2 == 0

Won't this apply the damage only if the player uses the command again? Or does it work like a timer?
 
Back
Top