• 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 delay time before teleport (onKill)

PuszekLDZ

https://tibia74.eu
Joined
Jan 2, 2020
Messages
428
Solutions
2
Reaction score
109
Location
Lodz
Twitch
puszekgamer
I`m trying to make a simple script, to teleport me to destination after kill a monster.

I got teleport part worked, but it wont wait 60 sec as I need :(

got something like that:

LUA:
local destination = Position(32337, 32087, 7)
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
    local player = Player(creature)
    if not player then
        creature:teleportTo(fromPosition, true)
        return false
    end
    if (getCreatureName(target) == "rat") then
            player:teleportTo(destination, os.time() + 60)
        end
return true
end
anybody could help me?
 
Solution
LUA:
local destination = Position(32337, 32087, 7)
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
    local player = Player(creature)
    if not player then
        creature:teleportTo(fromPosition, true)
        return false
    end
    if (getCreatureName(target) == "rat") then
            addEvent(function(player_id)
               local p = Creature(player_id)
               if not p then
                  return
               end
               p:teleportTo(destination)
            end, 60000, player:getId())
   end
return true
end
LUA:
local destination = Position(32337, 32087, 7)
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
    local player = Player(creature)
    if not player then
        creature:teleportTo(fromPosition, true)
        return false
    end
    if (getCreatureName(target) == "rat") then
            addEvent(function(player_id)
               local p = Creature(player_id)
               if not p then
                  return
               end
               p:teleportTo(destination)
            end, 60000, player:getId())
   end
return true
end
 
Solution
LUA:
local destination = Position(32337, 32087, 7)
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
    local player = Player(creature)
    if not player then
        creature:teleportTo(fromPosition, true)
        return false
    end
    if (getCreatureName(target) == "rat") then
            addEvent(function(player_id)
               local p = Creature(player_id)
               if not p then
                  return
               end
               p:teleportTo(destination)
            end, 60000, player:getId())
   end
return true
end
[Warning - Event::checkScript] Can not load script: scripts/teleport.lua
data/creaturescripts/scripts/teleport.lua:9: unexpected symbol near ')'
 
LUA:
local destination = Position(32337, 32087, 7)
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
    local player = Player(creature)
    if not player then
        creature:teleportTo(fromPosition, true)
        return false
    end
    if (getCreatureName(target) == "rat") then
            addEvent(function(player_id)
               local p = Creature(player_id)
               if not p then
                  return
               end
               p:teleportTo(destination)
            end, 60000, player:getId())
   end
return true
end
can remake such for revscript please ? :)) (for 1,2tfs++)
 
onKill event has only 2 parameters:
Code:
function onKill(creature, target)
not 7:
Code:
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
so part using fromPosition is wrong, it won't do anything. Anyway, it's not a problem, because this script (onKill) will execute only for Players, not Monsters.
It can be a problem, if player kill that monster using summon and it won't execute onKill for player (IDK if it executes onKill for player who owns summon).

Often scripts like that use onDeath event added to monster and read creature:getDamageMap() to teleport all players that dealt damage to killed monster.
OR spawn teleport item with destination position on position of killed monster, that is removed after 60 seconds.

can remake such for revscript please ? :)) (for 1,2tfs++)
TFS 1.x version [not tested]:
LUA:
-- config
local monsterName = 'Rat'
local destination = Position(32337, 32087, 7)
local delayInSeconds = 60

local eventKillName = 'onKillRat'
local eventLoginName = 'onLoginOnKillRat'

-- script
monsterName = monsterName:lower()

local creatureEventKill = CreatureEvent(eventKillName)
function creatureEventKill.onKill(creature, target)
    -- creature is for sure of type Player, as we add this onKill event only to players in onLogin

    -- ignore players with same name as monster
    if not Monster(target) then
        return true
    end

    -- get monsterType name (.xml), not 'name', as it can be modified for given monster on some engines (ex. Rat [lvl 3])
    if target:type():name():lower() == monsterName then
        addEvent(function(playerId)
            local player = Player(playerId)
            if not player then
                return
            end
			player:teleportTo(destination)
        end, delayInSeconds * 1000, creature:getId())
    end

    return true
end

creatureEventKill:register()

local creatureEventLogin = CreatureEvent(eventLoginName)
function creatureEventLogin.onLogin(player)
    player:registerEvent(eventKillName)

    return true
end

creatureEventLogin:register()
 
onKill event has only 2 parameters:
Code:
function onKill(creature, target)
not 7:
Code:
function onKill(creature, target, lastHit, player, item, fromPosition, toPosition)
so part using fromPosition is wrong, it won't do anything. Anyway, it's not a problem, because this script (onKill) will execute only for Players, not Monsters.
It can be a problem, if player kill that monster using summon and it won't execute onKill for player (IDK if it executes onKill for player who owns summon).

Often scripts like that use onDeath event added to monster and read creature:getDamageMap() to teleport all players that dealt damage to killed monster.
OR spawn teleport item with destination position on position of killed monster, that is removed after 60 seconds.


TFS 1.x version [not tested]:
LUA:
-- config
local monsterName = 'Rat'
local destination = Position(32337, 32087, 7)
local delayInSeconds = 60

local eventKillName = 'onKillRat'
local eventLoginName = 'onLoginOnKillRat'

-- script
monsterName = monsterName:lower()

local creatureEventKill = CreatureEvent(eventKillName)
function creatureEventKill.onKill(creature, target)
    -- creature is for sure of type Player, as we add this onKill event only to players in onLogin

    -- ignore players with same name as monster
    if not Monster(target) then
        return true
    end

    -- get monsterType name (.xml), not 'name', as it can be modified for given monster on some engines (ex. Rat [lvl 3])
    if target:type():name():lower() == monsterName then
        addEvent(function(playerId)
            local player = Player(playerId)
            if not player then
                return
            end
            player:teleportTo(destination)
        end, delayInSeconds * 1000, creature:getId())
    end

    return true
end

creatureEventKill:register()

local creatureEventLogin = CreatureEvent(eventLoginName)
function creatureEventLogin.onLogin(player)
    player:registerEvent(eventKillName)

    return true
end

creatureEventLogin:register()
found bug,
LUA:
if target:type():name():lower() == monsterName then
this will looking for "rat" while we have in config "Rat"

quick fix:
LUA:
if target:type():name():lower() == monsterName:lower() then

or it doesnt matter for monsterType?
 
found bug,
LUA:
if target:type():name():lower() == monsterName then
this will looking for "rat" while we have in config "Rat"

quick fix:
LUA:
if target:type():name():lower() == monsterName:lower() then

or it doesnt matter for monsterType?
I lowercased it outside onKill script to execute it once, not every 'onKill':
LUA:
-- script
monsterName = monsterName:lower()
(...)
This will execute every kill:
LUA:
if target:type():name():lower() == monsterName:lower() then
My code processes config of script on server startup - when .lua files are loaded. Just one per server restart/Lua /reload.
onKill event will be called 100k-1kk times per 24 hours on popular server, so it saves a looot of CPU.

That's how every TFS 1.x script should look like:
1. config - things that user may configure wrong
2. process config on server start (when .lua loads) - you can detect errors in config and process some variables ex. :lower(), you can generate some extra Lua tables with ex. "reverse lookup" for some scripts; not everything can be detected, as Lua loads before .otbm and you can't detect map errors [wrong positions] without onStartup GlobalEvent
3. declare Lua events that OTS will execute over and over again (onKill, onDeath, on Login)
 

Similar threads

  • Question Question
Replies
0
Views
173
Back
Top