• 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 [TFS 1.4.2] Make something happen when killing a monster

bravespiritz

Member
Joined
Aug 10, 2023
Messages
33
Reaction score
5
I have a Arena. in the same arena there will spawn different monsters when you kill a monster.

example when I kill a troll another will appear. is it possible to do?

Skärmbild 2023-08-13 105453.png
 
Solution
Yes so it rotates, but after rotworm it has a delay on 20sec
Put all of your desires in the first post.
Scattering them along like this is annoying.
Lua:
local delay = 20
local monsterIndex = 1
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat",
    "cave rat",
    "troll",
    "rotworm"
}

local function createCreatureFromExternalTable(index)
    local monster = Game.createMonster(monsterList[index], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
end

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    monsterIndex = monsterIndex < #monsterList and...
Something like this should work.
Lua:
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat", "cave rat", "troll",
    "demon", "rotworm"
}

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    local rand = math.random(#monsterList)
    local monster = Game.createMonster(monsterList[rand], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

creatureevent:register()

local globalevent = GlobalEvent("SpawnRandomMonsterOnStartup")

function globalevent.onStartup()
    local rand = math.random(#monsterList)
    local monster = Game.createMonster(monsterList[rand], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

globalevent:register()
 
Last edited:
Do not forget to leave information about the engine you are using:

For example:
TFS 1.x+ / TFS 0.4 / Canary / ect...

This is very important so that we can fast help you.
TFS 1.4.2
Post automatically merged:

Something like this should work.
Lua:
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat", "cave rat", "troll",
    "demon", "rotworm"
}

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    local rand = math.random(#monsterList)
    local monster = Game.createMonster(monsterList[rand], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

creatureevent:register()
in what file am I writing this?
 
it works thanks, but If I dont want it to be random. If I want in this order. how would I go about changing it
1. rat",
2. "cave rat",
3. "troll",
4."rotworm"
So that it rotates? 1-2-3-4, 1-2-3-4?

Like this I guess.
Lua:
local monsterIndex = 1
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat",
    "cave rat",
    "troll",
    "rotworm"
}

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    monsterIndex = monsterIndex < #monsterList and monsterIndex + 1 or 1
    local monster = Game.createMonster(monsterList[monsterIndex], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

creatureevent:register()

local globalevent = GlobalEvent("SpawnRandomMonsterOnStartup")

function globalevent.onStartup()
    local monster = Game.createMonster(monsterList[monsterIndex], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

globalevent:register()
 
So that it rotates? 1-2-3-4, 1-2-3-4?

Like this I guess.
Lua:
local monsterIndex = 1
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat",
    "cave rat",
    "troll",
    "rotworm"
}

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    monsterIndex = monsterIndex < #monsterList and monsterIndex + 1 or 1
    local monster = Game.createMonster(monsterList[monsterIndex], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

creatureevent:register()

local globalevent = GlobalEvent("SpawnRandomMonsterOnStartup")

function globalevent.onStartup()
    local monster = Game.createMonster(monsterList[monsterIndex], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

globalevent:register()
Yes so it rotates, but after rotworm it has a delay on 20sec
 
Yes so it rotates, but after rotworm it has a delay on 20sec
Put all of your desires in the first post.
Scattering them along like this is annoying.
Lua:
local delay = 20
local monsterIndex = 1
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat",
    "cave rat",
    "troll",
    "rotworm"
}

local function createCreatureFromExternalTable(index)
    local monster = Game.createMonster(monsterList[index], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
end

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    monsterIndex = monsterIndex < #monsterList and monsterIndex + 1 or 1
    addEvent(createCreatureFromExternalTable, (monsterIndex == 1 and delay or 0) * 1000)
    return true
end

creatureevent:register()

local globalevent = GlobalEvent("SpawnRandomMonsterOnStartup")

function globalevent.onStartup()
    local monster = Game.createMonster(monsterList[monsterIndex], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

globalevent:register()
 
Solution
This works thanks. Thank you for helping me. and sorry for not explaining this at start :)
Put all of your desires in the first post.
Scattering them along like this is annoying.
Lua:
local delay = 20
local monsterIndex = 1
local spawnPosition = Position(1000, 1000, 7)
local monsterList = {
    "rat",
    "cave rat",
    "troll",
    "rotworm"
}

local function createCreatureFromExternalTable(index)
    local monster = Game.createMonster(monsterList[index], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
end

local creatureevent = CreatureEvent("SpawnRandomMonsterOnDeath")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    monsterIndex = monsterIndex < #monsterList and monsterIndex + 1 or 1
    addEvent(createCreatureFromExternalTable, (monsterIndex == 1 and delay or 0) * 1000)
    return true
end

creatureevent:register()

local globalevent = GlobalEvent("SpawnRandomMonsterOnStartup")

function globalevent.onStartup()
    local monster = Game.createMonster(monsterList[monsterIndex], spawnPosition)
    monster:registerEvent("SpawnRandomMonsterOnDeath")
    return true
end

globalevent:register()
 
Back
Top