• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Respawn onKill (Conditional)

kito2

www.masteria.net
Joined
Mar 9, 2009
Messages
3,764
Solutions
1
Reaction score
227
Location
Chile, Santiago
Hello,

I have a few days trying to simulate the following: If a certain condition is true, then monsters will respawn (after the game starts), but if the condition is not true, it wont start.

I have done this part so far, but I am missing now the "event" where those monsters are respawned at the specific position again.

For example les says if the condition is true, then 6 rats are respawned at different positions. If one of those rats dies, it should be respawned as normal as it was.

Any ideas?

Here is the code for the starting respawn:

Code:
local spawns = {
    -- Spawns
    [1]  = {position = Position(33514, 32471, 7), monster = 'Sight of Surrender'},
    [2]  = {position = Position(33585, 32459, 7), monster = 'Sight of Surrender'},
    [3]  = {position = Position(33625, 32490, 7), monster = 'Sight of Surrender'},
    [4]  = {position = Position(33670, 32485, 7), monster = 'Sight of Surrender'},
    [5]  = {position = Position(33659, 32540, 7), monster = 'Sight of Surrender'},
    [6]  = {position = Position(33607, 32536, 7), monster = 'Sight of Surrender'}

}

function onStartup()
   
    if (getGlobalStorageValueDB(ROSHAMUUL_HORN_WALL) < 200) then
        Game.createMonster('Sight of Surrender', Position(33514, 32471, 7), false, true)
        Game.createMonster('Sight of Surrender', Position(33585, 32459, 7), false, true)
        Game.createMonster('Sight of Surrender', Position(33625, 32490, 7), false, true)
        Game.createMonster('Sight of Surrender', Position(33670, 32485, 7), false, true)
        Game.createMonster('Sight of Surrender', Position(33659, 32540, 7), false, true)
        Game.createMonster('Sight of Surrender', Position(33607, 32536, 7), false, true)
        print("Sight of Surrender summoned!")
       
        return true
    else
        setGlobalStorageValueDB(ROSHAMUUL_HORN_WALL,0)
        print("Sight of Surrender NOT summoned!")
    end
end
 
You need 2 things:
An onDeath event to trigger an addEvent that will respawn the monster
A global table to store the spawnId of spawned monsters and the positions.

And in your code, use a for to spawn them.
 
You need 2 things:
An onDeath event to trigger an addEvent that will respawn the monster
A global table to store the spawnId of spawned monsters and the positions.

And in your code, use a for to spawn them.

Got it.

Created SightOfSurrender = {} as a table and storaged each respawn as SightOfSurrender = Game.createMonster(spawn.monster, spawn.position, false, true) in a for condition.

But onDeath have this code:

Code:
    if target:getName() == "Sight of Surrender" then
        for i = 1, #SightOfSurrender do
            if SightOfSurrender[i] then
                local spawn = spawns[i]
                SightOfSurrender[i] = Game.createMonster(spawn.monster, spawn.position, false, true)
                --print("Summoneado SoS" .. i)
            end
        end
    end

And it sumons all the monsters again... How to check specific spawnId?

Note: Haven't added an event yet with timer.
 
You are doing it wrong you don't need this for loop, at spawn you will do:
local monster = Game.createMonster...
if monster then
SightSurrender[monster:getId()] = spawnId (aka "i" in your loop when spawning first time)
else
-- shit happened and monster didn't spawn maybe try respawning again after a while, print something or ignore.
end

Then on the onDeath you get the spawn id by referencing this table with the current monster id

On cellphone so it may have typos

Edit: You are using onKill, change to onDeath, it's the correct event for this task.

You can register it on the monster the moment you spawn.
 
Last edited:
I get your idea, but when summon again the monster, it will have another Id I think, so won't be able to respawn after this "new" creature dies.
 
I get your idea, but when summon again the monster, it will have another Id I think, so won't be able to respawn after this "new" creature dies.
I know it will have a different id, and you will set the reference again and erase the previous.
 
There is not a way to save on a Table monsters created with the function "Game.createMonster" and when prepareDeath check if is that monster, then respawn again when it was spawned before?

It would solve everything.
 
How could you do it, cause I am a bit confused.

- Globalevents startup spawn 6 sight of surrender of specific places regarding a condition (as first post shows).
- If any of those sight of surrender is killed, should be respawned in 2 minutes on the same place it was spawned before.

That should be all :P
 
Ok, I prefer to wait since I couldn't make it.

Thanks.
Code:
SightOfSurrender = {
    [1]  = {position = Position(33514, 32471, 7), monster = 'Sight of Surrender'},
    [2]  = {position = Position(33585, 32459, 7), monster = 'Sight of Surrender'},
    [3]  = {position = Position(33625, 32490, 7), monster = 'Sight of Surrender'},
    [4]  = {position = Position(33670, 32485, 7), monster = 'Sight of Surrender'},
    [5]  = {position = Position(33659, 32540, 7), monster = 'Sight of Surrender'},
    [6]  = {position = Position(33607, 32536, 7), monster = 'Sight of Surrender'}

}

SightOfSurrenderReference = {}

function spawnSight(id)
    local spawn = SightOfSurrender[id]
    if not spawn then
        return false
    end
   
    local monster = Game.createMonster(spawn.monster, spawn.position, false, true)
    if monster then
        monster:registerEvent("SightDeath")
        SightOfSurrenderReference[monster:getId()] = id
    else
        -- Monster not spawned? Something is wrong, chose a way to deal with this.
    end
end

function firstSightSpawn()
    for id, _ in pairs(SightOfSurrender) do
        spawnSight(id)
    end
end

function onStartup()
 
    if (getGlobalStorageValueDB(ROSHAMUUL_HORN_WALL) < 200) then
        firstSightSpawn()
        print("Sight of Surrender summoned!")
        return true
    else
        setGlobalStorageValueDB(ROSHAMUUL_HORN_WALL,0)
        print("Sight of Surrender NOT summoned!")
    end
end

function onDeath(creature, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local spawnId = SightOfSurrenderReference[creature:getId()]
    if spawnId then
        addEvent(spawnSight, 60 * 1000, spawnId)
        SightOfSurrenderReference[creature:getId()] = nil
    end
end

Organize it as you prefer.
 
Code:
SightOfSurrender = {
    [1]  = {position = Position(33514, 32471, 7), monster = 'Sight of Surrender'},
    [2]  = {position = Position(33585, 32459, 7), monster = 'Sight of Surrender'},
    [3]  = {position = Position(33625, 32490, 7), monster = 'Sight of Surrender'},
    [4]  = {position = Position(33670, 32485, 7), monster = 'Sight of Surrender'},
    [5]  = {position = Position(33659, 32540, 7), monster = 'Sight of Surrender'},
    [6]  = {position = Position(33607, 32536, 7), monster = 'Sight of Surrender'}

}

SightOfSurrenderReference = {}

function spawnSight(id)
    local spawn = SightOfSurrender[id]
    if not spawn then
        return false
    end
  
    local monster = Game.createMonster(spawn.monster, spawn.position, false, true)
    if monster then
        monster:registerEvent("SightDeath")
        SightOfSurrenderReference[monster:getId()] = id
    else
        -- Monster not spawned? Something is wrong, chose a way to deal with this.
    end
end

function firstSightSpawn()
    for id, _ in pairs(SightOfSurrender) do
        spawnSight(id)
    end
end

function onStartup()
 
    if (getGlobalStorageValueDB(ROSHAMUUL_HORN_WALL) < 200) then
        firstSightSpawn()
        print("Sight of Surrender summoned!")
        return true
    else
        setGlobalStorageValueDB(ROSHAMUUL_HORN_WALL,0)
        print("Sight of Surrender NOT summoned!")
    end
end

function onDeath(creature, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local spawnId = SightOfSurrenderReference[creature:getId()]
    if spawnId then
        addEvent(spawnSight, 60 * 1000, spawnId)
        SightOfSurrenderReference[creature:getId()] = nil
    end
end

Organize it as you prefer.

Thanks. Got your idea but I see a problem. When spawned the 7th, 8th Sight of Surrender is going to fail about getting it's position, right?
 
Back
Top