• 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 Anyway to shorten this?

fishie

fishies-war
Joined
Oct 27, 2016
Messages
78
Reaction score
2
Ive got a working script that creates a teleport when a monster dies, but instead of creating multiple scripts to make multiple monsters create the tp can this script be edited to make multiple monsters make multiple tps?

local function remove(pos)
local item = Tile(pos):getItemById(1387)
if item then
item:remove()
end
end

function onKill(creature, target)
if target:isMonster() and target:getName():lower() == 'dipthrah' then
local pos = target:getPosition()
local teleport = Game.createItem(1387, -1, pos)
Teleport(teleport.uid):setDestination(Position(71, 127, 9))
addEvent(remove, 30*1000, pos)
end
return true
end
 
Code:
local cfg = {
    [1] = {name = 'dipthrah', tpDestination = {71, 127, 9}, removeAfter = 30 * 1000},
    [2] = {name = 'rat', tpDestination = {71, 127, 9}, removeAfter = 30 * 1000}
}

local function remove(pos)
    local item = Tile(pos):getItemById(1387)
    if item then
        item:remove()
    end
end

function onKill(creature, target)
    for i = 1, #cfg do
        if target:isMonster() and target:getName():lower() == cfg[i].name:lower() then
            local pos = target:getPosition()
            local teleport = Game.createItem(1387, -1, pos)
            Teleport(teleport.uid):setDestination(Position(cfg[i].tpDestination[1], cfg[i].tpDestination[2], cfg[i].tpDestination[3]))
            addEvent(remove, cfg[i].removeAfter, pos)
            break
        end
    end
    return true
end
 
Code:
local cfg = {
    [1] = {name = 'dipthrah', tpDestination = {71, 127, 9}, removeAfter = 30 * 1000},
    [2] = {name = 'rat', tpDestination = {71, 127, 9}, removeAfter = 30 * 1000}
}

local function remove(pos)
    local item = Tile(pos):getItemById(1387)
    if item then
        item:remove()
    end
end

function onKill(creature, target)
    for i = 1, #cfg do
        if target:isMonster() and target:getName():lower() == cfg[i].name:lower() then
            local pos = target:getPosition()
            local teleport = Game.createItem(1387, -1, pos)
            Teleport(teleport.uid):setDestination(Position(cfg[i].tpDestination[1], cfg[i].tpDestination[2], cfg[i].tpDestination[3]))
            addEvent(remove, cfg[i].removeAfter, pos)
            break
        end
    end
    return true
end
or you can just access the table index directly through monster name
Code:
local cfg = {
    ['dipthrah'] = {tpDestination = Position(71, 127, 9), removeAfter = 30 * 1000},
    ['rat'] = {tpDestination = Position(71, 127, 9), removeAfter = 30 * 1000}
}

local function remove(pos)
    local item = Tile(pos):getItemById(1387)
    if item then
        item:remove()
    end
end

function onKill(creature, target)
    local tmp = cfg[target:getName():lower()]
    if tmp and target:isMonster() then
        local pos = target:getPosition()
        local teleport = Game.createItem(1387, -1, pos)
        Teleport(teleport.uid):setDestination(tmp.tpDestination)
        addEvent(remove, tmp.removeAfter, pos)
    end
    return true
end
 
Back
Top