• 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 Timer on Teleportscroll

mpa

Member
Joined
Oct 8, 2008
Messages
319
Reaction score
13
Location
Sweden
This timer on the teleportscroll isn't working. When I use it i only get the cancelmessage "You can only use this scroll once every hour."

What's wrong with the timer?
PHP:
function onUse(cid, item)
    local compareTime = 60 * 60 * 1000
    local scroll = 1959
    local currentValue = getPlayerStorageValue(cid, 9897)
    
    if currentValue == -1 then
        currentValue = os.time()
    end

    if hasCondition(cid, CONDITION_INFIGHT) == TRUE and item.itemid == scroll then
        doPlayerSendCancel(cid, "You may not use this scroll while in-fight!")
        return FALSE
    end

    if (os.time() - currentValue) >= compareTime and hasCondition(cid, CONDITION_INFIGHT) == FALSE and item.itemid == scroll then
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid,19,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
        doPlayerSendCancel(cid, "You can only use this scroll once every hour.")
end
return TRUE
end
 
Remove

PHP:
    if currentValue == -1 then
        currentValue = os.time()
    end

And it will work.

So you will get

PHP:
  function onUse(cid, item)
    local compareTime = 60 * 60 * 1000
    local scroll = 1959
    local currentValue = getPlayerStorageValue(cid, 9897)
  

    if hasCondition(cid, CONDITION_INFIGHT) == TRUE and item.itemid == scroll then
        doPlayerSendCancel(cid, "You may not use this scroll while in-fight!")
        return FALSE
    end

    if (os.time() - currentValue) >= compareTime and hasCondition(cid, CONDITION_INFIGHT) == FALSE and item.itemid == scroll then
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid,19,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
        doPlayerSendCancel(cid, "You can only use this scroll once every hour.")
end
return TRUE
end
 
Remove

PHP:
    if currentValue == -1 then
        currentValue = os.time()
    end

And it will work.

So you will get

PHP:
  function onUse(cid, item)
    local compareTime = 60 * 60 * 1000
    local scroll = 1959
    local currentValue = getPlayerStorageValue(cid, 9897)
  

    if hasCondition(cid, CONDITION_INFIGHT) == TRUE and item.itemid == scroll then
        doPlayerSendCancel(cid, "You may not use this scroll while in-fight!")
        return FALSE
    end

    if (os.time() - currentValue) >= compareTime and hasCondition(cid, CONDITION_INFIGHT) == FALSE and item.itemid == scroll then
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getCreaturePosition(cid), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid,19,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
        doPlayerSendCancel(cid, "You can only use this scroll once every hour.")
end
return TRUE
end

Is possible to make timer in doPlayerSendCancel ?
Code:
doPlayerSendCancel(cid, "You still have to wait " ..(os.time() + compareTime).. " minutes to teleport to your home town again!")
don't work
 
PHP:
doPlayerSendCancel(cid, "You still have to wait " ..(((os.time() -currentValue)+compareTime)/60).. " minutes to teleport to your home town again!")

RAP++??

P.S.

PHP:
local compareTime = 60 * 60
instead, no
PHP:
* 1000
... It's not mili-seconds, but seconds.
 
Optimized the script a bit.

PHP:
local timeBetweenUsage = 1 * 60 * 60 -- Hour * Min * Sec

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local lastUsage = getPlayerStorageValue(cid, 9897)
	lastUsage = os.time() - lastUsage -- Seconds since last usage
	
	doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
	
    if (hasCondition(cid, CONDITION_INFIGHT) == TRUE) then
        doPlayerSendTextMessage(cid, 23, "You may not use this scroll while in-fight!")
    elseif (lastUsage >= timeBetweenUsage) then
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getTownTemplePosition(getPlayerTown(cid)), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid, 23,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
		doPlayerSendTextMessage(cid, 23, "You still have to wait " ..((lastUsage+timeBetweenUsage)/60).. " minutes to teleport to your home town again!")  
	end
	return TRUE
end
 
Optimized the script a bit.

PHP:
local timeBetweenUsage = 1 * 60 * 60 -- Hour * Min * Sec

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local lastUsage = getPlayerStorageValue(cid, 9897)
	lastUsage = os.time() - lastUsage -- Seconds since last usage
	
	doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
	
    if (hasCondition(cid, CONDITION_INFIGHT) == TRUE) then
        doPlayerSendTextMessage(cid, 23, "You may not use this scroll while in-fight!")
    elseif (lastUsage >= timeBetweenUsage) then
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getTownTemplePosition(getPlayerTown(cid)), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid, 23,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
		doPlayerSendTextMessage(cid, 23, "You still have to wait " ..((lastUsage+timeBetweenUsage)/60).. " minutes to teleport to your home town again!")  
	end
	return TRUE
end

Don't works.
12:03 You still have to wait 60.733333333333 minutes to teleport to your home town again!

Any idea ?
 
Try this one, all credits goes to Rizz obviously. ; )
PHP:
local timeBetweenUsage = 1 * 60 * 60 -- Hour * Min * Sec

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local lastUsage = getPlayerStorageValue(cid, 9897)
    lastUsage = os.time() - lastUsage -- Seconds since last usage
    
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
    
    if (hasCondition(cid, CONDITION_INFIGHT) == TRUE) then
        doPlayerSendTextMessage(cid, 23, "You may not use this scroll while in-fight!")
    elseif (lastUsage >= timeBetweenUsage) then
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getTownTemplePosition(getPlayerTown(cid)), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid, 23,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
        doPlayerSendTextMessage(cid, 23, "You still have to wait " ..math.ceil(((lastUsage+timeBetweenUsage)/60)).. " minutes to teleport to your home town again!")  
    end
    return TRUE
end
 
Try this one, all credits goes to Rizz obviously. ; )
PHP:
local timeBetweenUsage = 1 * 60 * 60 -- Hour * Min * Sec

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local lastUsage = getPlayerStorageValue(cid, 9897)
    lastUsage = os.time() - lastUsage -- Seconds since last usage
    
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
    
    if (hasCondition(cid, CONDITION_INFIGHT) == TRUE) then
        doPlayerSendTextMessage(cid, 23, "You may not use this scroll while in-fight!")
    elseif (lastUsage >= timeBetweenUsage) then
        doTeleportThing(cid, getTownTemplePosition(getPlayerTown(cid)), FALSE)
        doSendMagicEffect(getTownTemplePosition(getPlayerTown(cid)), CONST_ME_TELEPORT)
        doPlayerSendTextMessage(cid, 23,"You have been teleported to your home town!")
        setPlayerStorageValue(cid, 9897, os.time())
    else
        doPlayerSendTextMessage(cid, 23, "You still have to wait " ..math.ceil(((lastUsage+timeBetweenUsage)/60)).. " minutes to teleport to your home town again!")  
    end
    return TRUE
end

13:14 You still have to wait 60 minutes to teleport to your home town again!
13:14 You still have to wait 61 minutes to teleport to your home town again!
13:16 You still have to wait 62 minutes to teleport to your home town again!

Add time ; p

I change
Code:
        doPlayerSendTextMessage(cid, 23, "You still have to wait " ..math.ceil(((lastUsage+timeBetweenUsage)/60)).. " minutes to teleport to your home town again!")
To:
Code:
        doPlayerSendTextMessage(cid, 23, "You still have to wait " ..math.ceil(((lastUsage-timeBetweenUsage)/60)).. " minutes to teleport to your home town again!")

and:
13:16 You still have to wait -58 minutes to teleport to your home town again!
13:17 You still have to wait -57 minutes to teleport to your home town again!
13:18 You still have to wait -56 minutes to teleport to your home town again!

But is -56, need 56
 
Ah sorry
PHP:
doPlayerSendTextMessage(cid, 23, "You still have to wait " ..math.ceil(((timeBetweenUsage-lastUsage)/60)).. " minutes to teleport to your home town again!")


Edit;

Instead of
PHP:
lastUsage = os.time() - lastUsage -- Seconds since last usage
use
PHP:
lastUsage = os.difftime(os.time(), lastUsage)
Looks more professional :-D
 
Last edited:
Back
Top