• 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!

TFS 0.X [Spawn monster upon stepping on tile] But with chance and possible only once per 1 hour

Tibia Leaves

New Member
Joined
Jun 1, 2019
Messages
63
Reaction score
2
I want this code to be only possible to run once every hour how can we achieve that ? this is currently in movements.

so t
Lua:
---<movevent event="StepIn" actionid="47401" script="test.lua"/>
-- action id 47401 for StepIn Tile" can even be fucking grass but pressure pads are cool"
local position1 = {x="32103",y="32172",z="8"}
local position2 = {x="32103",y="32171",z="8"}
local monstertospawn = "rat"
--32103 32172 8
--32103 32171 8
--perfect
-- can u write these into x yz
-- local variables mean that it is value associated with variable name that can be used locally within THIS script global variables can be written into /data/lib as separate file!    I
-- if you want to understand stop removing comments

--function onStepIn(tile, itemid, whereistile, wherewasplayerlaststandingbefore)
--addevent(timefunction , 1000 ms, seconds)


function onStepIn(cid, item, position, fromPosition, player)
addEvent(onThink, 1000, seconds)
end
--function timefunction handler
--this is a tes script
function onThink()-- npcHandler:onThink() -- we are not handling anything via NPC so i guess it is not needed we will see
chance = math.random(1,10)
-- math random number generating variable chance within FUNCTION "warning this is not local variable so chance can only be used within this function not whole script!"
    if chance == 1 then
    --1st option this script does not have to have option for each generater number simply haivng 1 chance and 1,100 generation will make it 1 to 100 chance to spawn upon step.
 doSummonCreature("rat", position1)
doSummonCreature("rat", position2)

-- docreate monster seems to be useless
    elseif chance == 2 then
    --2nd option
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)
    elseif chance == 3 then
    --3rd option
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)
    elseif chance == 4 then
    --4th option
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)
    end
end
 
So you want to move it to globalevents right? Or you want make something like 'block' that it will be executed only when player will move on tile but minimum 1 hour exh?
 
I want this code to be only possible to run once every hour how can we achieve that ? this is currently in movements.

so t
Lua:
---<movevent event="StepIn" actionid="47401" script="test.lua"/>
-- action id 47401 for StepIn Tile" can even be fucking grass but pressure pads are cool"
local position1 = {x="32103",y="32172",z="8"}
local position2 = {x="32103",y="32171",z="8"}
local monstertospawn = "rat"
--32103 32172 8
--32103 32171 8
--perfect
-- can u write these into x yz
-- local variables mean that it is value associated with variable name that can be used locally within THIS script global variables can be written into /data/lib as separate file!    I
-- if you want to understand stop removing comments

--function onStepIn(tile, itemid, whereistile, wherewasplayerlaststandingbefore)
--addevent(timefunction , 1000 ms, seconds)


function onStepIn(cid, item, position, fromPosition, player)
addEvent(onThink, 1000, seconds)
end
--function timefunction handler
--this is a tes script
function onThink()-- npcHandler:onThink() -- we are not handling anything via NPC so i guess it is not needed we will see
chance = math.random(1,10)
-- math random number generating variable chance within FUNCTION "warning this is not local variable so chance can only be used within this function not whole script!"
    if chance == 1 then
    --1st option this script does not have to have option for each generater number simply haivng 1 chance and 1,100 generation will make it 1 to 100 chance to spawn upon step.
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)

-- docreate monster seems to be useless
    elseif chance == 2 then
    --2nd option
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)
    elseif chance == 3 then
    --3rd option
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)
    elseif chance == 4 then
    --4th option
doSummonCreature("rat", position1)
doSummonCreature("rat", position2)
    end
end

If globalevent u can write something like this, but remember about protect your systems. So first you need to check if you can really spawn monster on tile, cuz if someone is on tile server will send you an error.

Lua:
--  <globalevent name="monsterSpawner" interval="3600" event="script" value="monsterSpawner.lua"/>

function onThink(interval, lastExecution)

    local config = {

        {

            pos = {x=2345,y=2433,z=7},

            monsters = {"Wolf", "Rat"},

            count = 3

        },

        {

            pos = {x=5555,y=6666,z=7},

            monsters = {"Dragon", "Demon"},

            count = 2

        },

    }

    local spawn = config[math.random(1, #config)]

    for i = 1, spawn.count do

        doCreateMonster(spawn.monsters[math.random(1, #spawn.monsters)], spawn.pos)

    end

    return true

end
 
If globalevent u can write something like this, but remember about protect your systems. So first you need to check if you can really spawn monster on tile, cuz if someone is on tile server will send you an error.

Lua:
--  <globalevent name="monsterSpawner" interval="3600" event="script" value="monsterSpawner.lua"/>

function onThink(interval, lastExecution)

    local config = {

        {

            pos = {x=2345,y=2433,z=7},

            monsters = {"Wolf", "Rat"},

            count = 3

        },

        {

            pos = {x=5555,y=6666,z=7},

            monsters = {"Dragon", "Demon"},

            count = 2

        },

    }

    local spawn = config[math.random(1, #config)]

    for i = 1, spawn.count do

        doCreateMonster(spawn.monsters[math.random(1, #spawn.monsters)], spawn.pos)

    end

    return true

end
actually not if tile is taken it spawns on next closest tile but yes we want to add exhaustion to this script without having to move it to globals if possible
ps. we use math random as actual random chance to trigger this
 
According to your description the script should rather look something like this
Lua:
local monsters = {
    {pos = { x = "32103", y = "32172", z = "8"}, name = "rat"},
    {pos = { x = "32103", y = "32171", z = "8"}, name = "rat"}
}
local storage = 12111

function onStepIn(cid, item, position, fromPosition, player)
    if getStorage(storage) > os.time() then
        doPlayerSendCancel((getStorage(storage) - os.time()) / 60 .." minutes of cooldown remain until more monsters can be spawned")
        return
    else
        for k,v in pairs(monsters) do
            doCreateMonster(v.name, v.pos)
        end
        doSetStorage(storage, os.time() + 60 * 60)
    end
end

edit: fixed minor bugs
 
Last edited:
According to your description the script should rather look something like this
Lua:
local monsters = {
    pos = { x = "32103", y = "32172", z = "8"}, name = "rat"},
    pos = { x = "32103", y = "32171", z = "8"}, name = "rat"}
}
local storage = 12111

function onStepIn(cid, item, position, fromPosition, player)
    if getStorage(storage) > os.time() then
        doPlayerSendCancel((getStorage(storage)-os.time())/60.." minutes of cooldown remain until more monsters can be spawned")
        return
    else
        for k,v in pairs(monsters) do
            doCreateMonster(v.name, v.pos)
        end
        doSetStorage(storage, os.time + 60 * 60)
    end
end
thank you we will test it but yes this is exactly what we wanted! :D can you please elaborate just fr me how does for k,v and inpairs work?
also we tried DocreateMonster before and it was giving error about '' name
 
There was a typo, I updated the script
k,v in pairs works like this
Lua:
local table = {
    ["anything"] = 500,
    [55] = 9
}
-- k,v is only short for key,value you can actually write whatever you want they're just variables
for key,value in pairs(table) do
    print("key: "..key.."; value: "..value)
end
-- prints
-- anything; 500
-- 55; 9
-- it won't necessarily be in the order of the original table
 
There was a typo, I updated the script
k,v in pairs works like this
Lua:
local table = {
    ["anything"] = 500,
    [55] = 9
}
-- k,v is only short for key,value you can actually write whatever you want they're just variables
for key,value in pairs(table) do
    print("key: "..key.."; value: "..value)
end
-- prints
-- anything; 500
-- 55; 9
-- it won't necessarily be in the order of the original table
ok why dots ..key.. and ..value and ; is to separate or acts as =

oh and also how do we go about adding a chance to spawn it and if not then not adding the storage yet or should i experiment iwth math random myself? :D if you believe it is easy then dont tell me i will figure but if it is more complex than you think i think ( xD) then please share wisdom
 
That's how you write values of variables to a string in lua.
Dots are not needed at the beginning or end of a string though, only where the string should continue.
 
actually not if tile is taken it spawns on next closest tile but yes we want to add exhaustion to this script without having to move it to globals if possible
ps. we use math random as actual random chance to trigger this

Yes, it will scan 8 closest tiles if something is on center, but if it can't find free tile in these 9 tiles it will return error, im not sure how it works in new engines, probably better but on the older like 0.3.6/0.4 it will print error.
 
According to your description the script should rather look something like this
Lua:
local monsters = {
    {pos = { x = "32103", y = "32172", z = "8"}, name = "rat"},
    {pos = { x = "32103", y = "32171", z = "8"}, name = "rat"}
}
local storage = 12111

function onStepIn(cid, item, position, fromPosition, player)
    if getStorage(storage) > os.time() then
        doPlayerSendCancel((getStorage(storage)-os.time())/60.." minutes of cooldown remain until more monsters can be spawned")
        return
    else
        for k,v in pairs(monsters) do
            doCreateMonster(v.name, v.pos)
        end
        doSetStorage(storage, os.time + 60 * 60)
    end
end
they keep respawning and line 9 gives error about malformed number 60 i tried adding . before 60 or removing the send cancel completely and they just spawn non stop this is tfs 0.4
 
---<movevent event="StepIn" actionid="47401" script="test.lua"/>
--data/movements/movements.xml^ actionid must match desired tile action id
local desiredposition = {x="32103",y="32172",z="8"}
-- desiredposition change that in dosummoncreature if you want customxyz
local monstertospawn = "rat"
local seconds = 0

local config =
{
makeExhaustion = 10, --make exhaust? put the exhaust time in seconds or false if it won't make exhaustion
exhaustionStorage = 51615,
}

function onStepIn(cid, item, position, fromPosition, player)
if getPlayerLevel(cid) > 1 then
-- npcHandler:eek:nThink() -- we are not handling anything via NPC so i guess it is not needed we will see
if (exhaustion.get (cid, config.exhaustionStorage)) then
return doPlayerSendCancel (cid, "Wait "..exhaustion.get (cid, config.exhaustionStorage).. " seconds to do it again.")
end

if (config.makeExhaustion ~= false and tonumber(config.makeExhaustion)) then
-- math.random in 1 out of 2 chances 50% or in 1,100 1% chance
chance = math.random(1,2)
if chance == 1 then
exhaustion.set (cid, config.exhaustionStorage, config.makeExhaustion)
doSummonCreature(monstertospawn, desiredposition)
doSummonCreature(monstertospawn, position)
doPlayerSendTextMessage(cid,21,"You have been ambushed!")
stopEvent(event)
else
return doPlayerSendCancel (cid, "IT didnt happen this time maybe try again")
-- return false once its debugged
end
end
end
end


Credits: To everyone in the thread!! It works flawlessly.
 
Back
Top