• 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 Script Error: [Test Interface]

bravespiritz

Member
Joined
Aug 10, 2023
Messages
33
Reaction score
5
This is the error I get, I dont really know how to fix it im using TFS 1.4.2. it is supposed to be a quest that starts when a player enters a portal,

Lua Script Error: [Test Interface]
data/actions/scripts/questPortal.lua
data/actions/scripts/questPortal.lua:69: attempt to index a nil value
stack traceback:
[C]: in function '__index'
data/actions/scripts/questPortal.lua:69: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/questPortal.lua


Lua:
local requiredKills = 15
local completedPlayers = {} -- To track completed players
local spawnPosition = Position(999, 974, 7) -- This is the quest start area
local portalPosition = Position(1000, 988, 6) -- This is the portal's position

local function resetMonsters()
    for _, creature in ipairs(spawnPosition:getCreatures()) do
        creature:remove()
    end
end

local function spawnMonstersForQuest()
    for _ = 1, 2 do
        local rand = math.random(#monsterList)
        local monster = Game.createMonster(monsterList[rand], spawnPosition)
        monster:registerEvent("SpawnRandomMonstersOnDeath")
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item:getId() == 1387 and item:getUniqueId() == 1000 and not completedPlayers[player:getName()] then
        print("Player used magic forcefield portal with ID 1387")

        -- Check if the player's position matches the portal's position
        if player:getPosition() == portalPosition then
            print("Player's position matches the portal's position")

            player:teleportTo(Position(999, 974, 7)) -- Teleport to a waiting area
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest started! You have 5 minutes to kill 15 monsters.")

            -- Spawn monsters for the quest
            spawnMonstersForQuest()

            local kills = 0
            local timer = 5 * 60 -- 5 minutes in seconds
            local questTimer

            questTimer = function()
                if kills >= requiredKills then
                    completedPlayers[player:getName()] = true
                    player:teleportTo(Position(1000, 1000, 7)) -- Teleport to completion area
                    local rewardItems = {"2457", "2509", "2465", "2478", "2511"}
                    local rewardItem = rewardItems[math.random(#rewardItems)]
                    player:addItem(rewardItem, 1)
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations! You've completed the quest and received a reward.")
                else
                    if timer > 0 then
                        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Quest progress: " .. kills .. "/" .. requiredKills .. " monsters. Time left: " .. timer .. " seconds.")
                        timer = timer - 1
                        addEvent(questTimer, 1000)
                    else
                        player:teleportTo(Position(1000, 1000, 7)) -- Teleport to failure area
                        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Quest failed! You ran out of time.")
                    end
                end
            end

            addEvent(questTimer, 1000)

        else
            print("Player is not at the correct portal position")
        end
    end
    return true
end

-- Register the onUse function
for _, uniqueId in ipairs({1000}) do
    local portalItem = Tile(portalPosition):getItemById(1387, uniqueId)
    if portalItem then
        portalItem:setActionId(100) -- Replace AID_EXAMPLE with your desired action ID
        portalItem:setAttribute(ITEM_ATTRIBUTE_ACTIONID, 100)
        portalItem:setAttribute(ITEM_ATTRIBUTE_UNIQUEID, uniqueId)
        portalItem:setAttribute(ITEM_ATTRIBUTE_TEXT, "Use")
    end
end

-- Global event to spawn the portal
local portalSpawnPosition = Position(1000, 988, 6) -- This is where the portal should appear
local portalUniqueId = 1000 -- Replace with the desired unique ID
local portalEvent = GlobalEvent("SpawnQuestPortal")

function portalEvent.onStartup()
    local portalItem = Game.createItem(1387, 1, portalSpawnPosition)
    portalItem:setUniqueId(portalUniqueId)
end

portalEvent:register()
 
Solution
everything works but, the killing message is spamming in the quest area. line 48 in the script.


Lua:
local tileActionId = 45001 -- put this on the tile underneath the portal
local spawnableCreatures = {"rat", "cave rat", "spider", "troll"}
local room = {
    from = Position(994, 973, 7), -- top left corner
    to = Position(1006, 985, 7)  -- bottom right corner
}
local monsters = {}
local killedMonsters = 0
local playerInRoom = 0

local requiredMonster = 15
local questTimer = 300

local teleportOut = Position(1000, 1000, 7)
local teleportIn = Position(1000, 981, 7)

local rewardsItems = {2457, 2509, 2460, 2465, 2478, 2511}

local function spawnMonstersInRoom(amount)
    local spawnPosition
    local randomMonster
    for i = 1...
Could you ad more coordianates to represent other shapes on the room?
No, unfortunately.

Tibia is made out of tiles on an x / y plane.
So basically square & rectangle is the only shape.

This was the error after print test
"
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
...erver-1.4.2\data\scripts\creaturescripts\questPortal.lua:55: attempt to compare number with nil
stack traceback:
[C]: in function '__lt'
...erver-1.4.2\data\scripts\creaturescripts\questPortal.lua:55: in function <...erver-1.4.2\data\scripts\creaturescripts\questPortal.lua:35>"

which is
local function monitorRoom(playerId, timer) (35)
if timer > questTimer then (55)

Ah, we never updated the the addEvent

4 lines down from that

change
Lua:
addEvent(monitorRoom, 1000, timer + 1)
to
Lua:
addEvent(monitorRoom, 1000, playerId, timer + 1)
 
that works thanks,

"
-- remove all creatures that were previously spawned.
for i = 1, #monsters do
local monster = Creature(monsters.monsterId)
if monster then
monster:remove()
end
end

"

this is not synced with the quest. when I enter the portal/tile again and spawn in the room. the room is not reset all the monsters are still there
 
that works thanks,

"
-- remove all creatures that were previously spawned.
for i = 1, #monsters do
local monster = Creature(monsters.monsterId)
if monster then
monster:remove()
end
end

"

this is not synced with the quest. when I enter the portal/tile again and spawn in the room. the room is not reset all the monsters are still there

change
Lua:
local monster = Creature(monsters.monsterId)
to
Lua:
local monster = Creature(monsters[i].monsterId)
 
No, unfortunately.

Tibia is made out of tiles on an x / y plane.
So basically square & rectangle is the only shape.



Ah, we never updated the the addEvent

4 lines down from that

change
Lua:
addEvent(monitorRoom, 1000, timer + 1)
to
Lua:
addEvent(monitorRoom, 1000, playerId, timer + 1)
what does this hashtag do example in the script "
#spawnableCreatures"
 
what does this hashtag do example in the script "
#spawnableCreatures"

There's a couple of usages for the # operator.

But it's primarily used in a sequence-like table to return the length of that sequence
Lua:
local array = {10, 20, 30, 40}
print(#array)  -- Outputs: 4

If it's not a list of objects like strings or arrays that can be counted, it will fail.
Lua:
local map = {["a"] = 10, ["b"] = 20, ["c"] = 30}
print(#map)  -- Outputs: 0, since it's not a sequence.

Here's some more examples of things it could be used for.
Lua:
local str = "Hello"
print(#str)  -- Outputs: 5

local arrayOfStrings= {"rat", "cave rat", "troll", "rotworm", "cyclops"}
print(#arrayOfStrings)  -- Outputs: 5

local arrayOfArrays = {
    {},
    {},
    {}
}
print(#arrayOfArrays) -- Outputs: 3
Post automatically merged:

what does this hashtag do example in the script "
#spawnableCreatures"
change
Lua:
local monster = Creature(monsters.monsterId)
to
Lua:
local monster = Creature(monsters[i].monsterId)
Any additional issues?
or is it working after this change?
 
Last edited:
yes it is working thank you so much, there is no issues.

im justtrying to figure out.

when we land into the quest area can we add a teleport effect? because right now it seems the character just comes in form thin air :)

also tracking kills in the default text box.


and a text message telling me that I have completed the quest.


Thank you for your help much appreciated. I will work on this next three things i mentioned
 
in the function moveevent.onStepIn

add under
Lua:
player:teleportTo(teleportIn)
Lua:
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest started! You have " .. questTimer .. " seconds to kill " .. requiredMonster .. " monsters.")
--
in the function monitorRoom

add above
Lua:
if killedMonsters >= requiredMonster then
Lua:
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have killed " ..  killedMonsters .. "/" .. requiredMonster .. " monsters.")

add under
Lua:
-- quest success
Lua:
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest finished! A reward has been sent to your characters inventory.")

add under
Lua:
-- quest fail
Lua:
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest failed. You did not kill enough monsters in the time allotted. (" ..  killedMonsters .. "/" .. requiredMonster .. ")")
 
everything works but, the killing message is spamming in the quest area. line 48 in the script.


Lua:
local tileActionId = 45001 -- put this on the tile underneath the portal
local spawnableCreatures = {"rat", "cave rat", "spider", "troll"}
local room = {
    from = Position(994, 973, 7), -- top left corner
    to = Position(1006, 985, 7)  -- bottom right corner
}
local monsters = {}
local killedMonsters = 0
local playerInRoom = 0

local requiredMonster = 15
local questTimer = 300

local teleportOut = Position(1000, 1000, 7)
local teleportIn = Position(1000, 981, 7)

local rewardsItems = {2457, 2509, 2460, 2465, 2478, 2511}

local function spawnMonstersInRoom(amount)
    local spawnPosition
    local randomMonster
    for i = 1, amount do
        randomMonster = spawnableCreatures[math.random(#spawnableCreatures)]
        spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
        local monster = Game.createMonster(randomMonster, spawnPosition)
        while not monster do
            spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
            monster = Game.createMonster(randomMonster, spawnPosition)
        end
        monsters[#monsters + 1] = {monsterId = monster:getId()}
        monster:registerEvent("onDeathSpawnTwoMore")
    end
end

local function monitorRoom(playerId, timer)
    local player = Player(playerId)
    if not player then
        -- player is no longer online.
        
        return
    end
    if not player:getPosition():isInRange(room.from, room.to) then
        -- player is no longer in the room
        
        return
    end

    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have killed " ..  killedMonsters .. "/" .. requiredMonster .. " monsters.")

    if killedMonsters >= requiredMonster then
        
        -- quest success

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest finished! A reward has been sent to your characters inventory.")

        player:teleportTo(teleportOut)
        local rand = math.random(#rewardsItems)
        player:addItem(rewardsItems[rand], 1, true)
        return true
    end
    if timer > questTimer then
        
        -- quest fail

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest failed. You did not kill enough monsters in the time allotted. (" ..  killedMonsters .. "/" .. requiredMonster .. ")")
        
        player:teleportTo(teleportOut)
        return true
    end
    -- continue monitoring event as not finished yet
    
    addEvent(monitorRoom, 1000, playerId, timer + 1)
end

local moveevent = MoveEvent()

function moveevent.onStepIn(creature, item, position, fromPosition)
    
    -- check if player stepping on the tile
    local player = Player(creature:getId())
    if not player then
        return true
    end
    
    -- check if another player is currently in the room
    local _playerInRoom = Player(playerInRoom)
    if _playerInRoom and _playerInRoom:getPosition():isInRange(room.from, room.to) then
        player:teleportTo(fromPosition, true)
        return true
    end
    
    -- remove all creatures that were previously spawned.
    for i = 1, #monsters do
        local monster = Creature(monsters[i].monsterId)
        if monster then
            monster:remove()
        end
    end   

    -- get player into the room and start monitoring room
    killedMonsters = 0
    playerInRoom = player:getId()
    player:teleportTo(teleportIn)

    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest started! You have " .. questTimer .. " seconds to kill " .. requiredMonster .. " monsters.")

    monitorRoom(playerInRoom, 0)
    
    -- spawn new creatures into the room
    addEvent(spawnMonstersInRoom, 2000, #spawnableCreatures)
    return true
end

moveevent:aid(tileActionId)
moveevent:register()



local creatureevent = CreatureEvent("onDeathSpawnTwoMore")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    killedMonsters = killedMonsters + 1
    spawnMonstersInRoom(2)
    return true
end

creatureevent:register()
 
everything works but, the killing message is spamming in the quest area. line 48 in the script.


Lua:
local tileActionId = 45001 -- put this on the tile underneath the portal
local spawnableCreatures = {"rat", "cave rat", "spider", "troll"}
local room = {
    from = Position(994, 973, 7), -- top left corner
    to = Position(1006, 985, 7)  -- bottom right corner
}
local monsters = {}
local killedMonsters = 0
local playerInRoom = 0

local requiredMonster = 15
local questTimer = 300

local teleportOut = Position(1000, 1000, 7)
local teleportIn = Position(1000, 981, 7)

local rewardsItems = {2457, 2509, 2460, 2465, 2478, 2511}

local function spawnMonstersInRoom(amount)
    local spawnPosition
    local randomMonster
    for i = 1, amount do
        randomMonster = spawnableCreatures[math.random(#spawnableCreatures)]
        spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
        local monster = Game.createMonster(randomMonster, spawnPosition)
        while not monster do
            spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
            monster = Game.createMonster(randomMonster, spawnPosition)
        end
        monsters[#monsters + 1] = {monsterId = monster:getId()}
        monster:registerEvent("onDeathSpawnTwoMore")
    end
end

local function monitorRoom(playerId, timer)
    local player = Player(playerId)
    if not player then
        -- player is no longer online.
       
        return
    end
    if not player:getPosition():isInRange(room.from, room.to) then
        -- player is no longer in the room
       
        return
    end

    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have killed " ..  killedMonsters .. "/" .. requiredMonster .. " monsters.")

    if killedMonsters >= requiredMonster then
       
        -- quest success

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest finished! A reward has been sent to your characters inventory.")

        player:teleportTo(teleportOut)
        local rand = math.random(#rewardsItems)
        player:addItem(rewardsItems[rand], 1, true)
        return true
    end
    if timer > questTimer then
       
        -- quest fail

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest failed. You did not kill enough monsters in the time allotted. (" ..  killedMonsters .. "/" .. requiredMonster .. ")")
       
        player:teleportTo(teleportOut)
        return true
    end
    -- continue monitoring event as not finished yet
   
    addEvent(monitorRoom, 1000, playerId, timer + 1)
end

local moveevent = MoveEvent()

function moveevent.onStepIn(creature, item, position, fromPosition)
   
    -- check if player stepping on the tile
    local player = Player(creature:getId())
    if not player then
        return true
    end
   
    -- check if another player is currently in the room
    local _playerInRoom = Player(playerInRoom)
    if _playerInRoom and _playerInRoom:getPosition():isInRange(room.from, room.to) then
        player:teleportTo(fromPosition, true)
        return true
    end
   
    -- remove all creatures that were previously spawned.
    for i = 1, #monsters do
        local monster = Creature(monsters[i].monsterId)
        if monster then
            monster:remove()
        end
    end  

    -- get player into the room and start monitoring room
    killedMonsters = 0
    playerInRoom = player:getId()
    player:teleportTo(teleportIn)

    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest started! You have " .. questTimer .. " seconds to kill " .. requiredMonster .. " monsters.")

    monitorRoom(playerInRoom, 0)
   
    -- spawn new creatures into the room
    addEvent(spawnMonstersInRoom, 2000, #spawnableCreatures)
    return true
end

moveevent:aid(tileActionId)
moveevent:register()



local creatureevent = CreatureEvent("onDeathSpawnTwoMore")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    killedMonsters = killedMonsters + 1
    spawnMonstersInRoom(2)
    return true
end

creatureevent:register()
No problem.
We'll add a small check to ensure it only updates you when your kill count has changed.

Lua:
local tileActionId = 45001 -- put this on the tile underneath the portal
local spawnableCreatures = {"rat", "cave rat", "spider", "troll"}
local room = {
    from = Position(994, 973, 7), -- top left corner
    to = Position(1006, 985, 7)  -- bottom right corner
}
local monsters = {}
local killedMonsters = 0
local playerInRoom = 0

local requiredMonster = 15
local questTimer = 300

local teleportOut = Position(1000, 1000, 7)
local teleportIn = Position(1000, 981, 7)

local rewardsItems = {2457, 2509, 2460, 2465, 2478, 2511}

local function spawnMonstersInRoom(amount)
    local spawnPosition
    local randomMonster
    for i = 1, amount do
        randomMonster = spawnableCreatures[math.random(#spawnableCreatures)]
        spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
        local monster = Game.createMonster(randomMonster, spawnPosition)
        while not monster do
            spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
            monster = Game.createMonster(randomMonster, spawnPosition)
        end
        monsters[#monsters + 1] = {monsterId = monster:getId()}
        monster:registerEvent("onDeathSpawnTwoMore")
    end
end

local function monitorRoom(playerId, timer, amountKilled)
    local player = Player(playerId)
    if not player then
        -- player is no longer online.
        return
    end
    if not player:getPosition():isInRange(room.from, room.to) then
        -- player is no longer in the room
        return
    end
    
    if amountKilled ~= killedMonsters then
        amountKilled = killedMonsters
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have killed " ..  killedMonsters .. "/" .. requiredMonster .. " monsters.")
    end
    
    if killedMonsters >= requiredMonster then
        -- quest success
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest finished! A reward has been sent to your characters inventory.")
        player:teleportTo(teleportOut)
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        local rand = math.random(#rewardsItems)
        player:addItem(rewardsItems[rand], 1, true)
        return true
    end
    if timer > questTimer then
        -- quest fail
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest failed. You did not kill enough monsters in the time allotted. (" ..  killedMonsters .. "/" .. requiredMonster .. ")")
        player:teleportTo(teleportOut)
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        return true
    end
    -- continue monitoring event as not finished yet
    addEvent(monitorRoom, 1000, playerId, timer + 1, amountKilled)
end


local moveevent = MoveEvent()

function moveevent.onStepIn(creature, item, position, fromPosition)
    
    -- check if player stepping on the tile
    local player = Player(creature:getId())
    if not player then
        return true
    end
    
    -- check if another player is currently in the room
    local _playerInRoom = Player(playerInRoom)
    if _playerInRoom and _playerInRoom:getPosition():isInRange(room.from, room.to) then
        player:teleportTo(fromPosition, true)
        return true
    end
    
    -- remove all creatures that were previously spawned.
    for i = 1, #monsters do
        local monster = Creature(monsters[i].monsterId)
        if monster then
            monster:remove()
        end
    end   
    
    -- get player into the room and start monitoring room
    killedMonsters = 0
    playerInRoom = player:getId()
    
    player:teleportTo(teleportIn)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest started! You have " .. questTimer .. " seconds to kill " .. requiredMonster .. " monsters.")
    
    monitorRoom(playerInRoom, 0, 0)
    
    -- spawn new creatures into the room
    addEvent(spawnMonstersInRoom, 2000, #spawnableCreatures)
    return true
end

moveevent:aid(tileActionId)
moveevent:register()



local creatureevent = CreatureEvent("onDeathSpawnTwoMore")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    killedMonsters = killedMonsters + 1
    spawnMonstersInRoom(2)
    return true
end

creatureevent:register()
 
Solution
No problem.
We'll add a small check to ensure it only updates you when your kill count has changed.

Lua:
local tileActionId = 45001 -- put this on the tile underneath the portal
local spawnableCreatures = {"rat", "cave rat", "spider", "troll"}
local room = {
    from = Position(994, 973, 7), -- top left corner
    to = Position(1006, 985, 7)  -- bottom right corner
}
local monsters = {}
local killedMonsters = 0
local playerInRoom = 0

local requiredMonster = 15
local questTimer = 300

local teleportOut = Position(1000, 1000, 7)
local teleportIn = Position(1000, 981, 7)

local rewardsItems = {2457, 2509, 2460, 2465, 2478, 2511}

local function spawnMonstersInRoom(amount)
    local spawnPosition
    local randomMonster
    for i = 1, amount do
        randomMonster = spawnableCreatures[math.random(#spawnableCreatures)]
        spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
        local monster = Game.createMonster(randomMonster, spawnPosition)
        while not monster do
            spawnPosition = Position(math.random(room.from.x, room.to.x), math.random(room.from.y, room.to.y), math.random(room.from.z, room.to.z))
            monster = Game.createMonster(randomMonster, spawnPosition)
        end
        monsters[#monsters + 1] = {monsterId = monster:getId()}
        monster:registerEvent("onDeathSpawnTwoMore")
    end
end

local function monitorRoom(playerId, timer, amountKilled)
    local player = Player(playerId)
    if not player then
        -- player is no longer online.
        return
    end
    if not player:getPosition():isInRange(room.from, room.to) then
        -- player is no longer in the room
        return
    end
   
    if amountKilled ~= killedMonsters then
        amountKilled = killedMonsters
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have killed " ..  killedMonsters .. "/" .. requiredMonster .. " monsters.")
    end
   
    if killedMonsters >= requiredMonster then
        -- quest success
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest finished! A reward has been sent to your characters inventory.")
        player:teleportTo(teleportOut)
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        local rand = math.random(#rewardsItems)
        player:addItem(rewardsItems[rand], 1, true)
        return true
    end
    if timer > questTimer then
        -- quest fail
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest failed. You did not kill enough monsters in the time allotted. (" ..  killedMonsters .. "/" .. requiredMonster .. ")")
        player:teleportTo(teleportOut)
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        return true
    end
    -- continue monitoring event as not finished yet
    addEvent(monitorRoom, 1000, playerId, timer + 1, amountKilled)
end


local moveevent = MoveEvent()

function moveevent.onStepIn(creature, item, position, fromPosition)
   
    -- check if player stepping on the tile
    local player = Player(creature:getId())
    if not player then
        return true
    end
   
    -- check if another player is currently in the room
    local _playerInRoom = Player(playerInRoom)
    if _playerInRoom and _playerInRoom:getPosition():isInRange(room.from, room.to) then
        player:teleportTo(fromPosition, true)
        return true
    end
   
    -- remove all creatures that were previously spawned.
    for i = 1, #monsters do
        local monster = Creature(monsters[i].monsterId)
        if monster then
            monster:remove()
        end
    end  
   
    -- get player into the room and start monitoring room
    killedMonsters = 0
    playerInRoom = player:getId()
   
    player:teleportTo(teleportIn)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Quest started! You have " .. questTimer .. " seconds to kill " .. requiredMonster .. " monsters.")
   
    monitorRoom(playerInRoom, 0, 0)
   
    -- spawn new creatures into the room
    addEvent(spawnMonstersInRoom, 2000, #spawnableCreatures)
    return true
end

moveevent:aid(tileActionId)
moveevent:register()



local creatureevent = CreatureEvent("onDeathSpawnTwoMore")

function creatureevent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    killedMonsters = killedMonsters + 1
    spawnMonstersInRoom(2)
    return true
end

creatureevent:register()
this works great. thanks for your help. I think we can end this thread now :)

learned a lot from this :)
 
Back
Top