• 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 1.X+ TFS1.3 Spawn npc (on startup) first day of the month

undead mage

Active Member
Joined
Mar 25, 2012
Messages
364
Solutions
2
Reaction score
47
Location
Amsterdam
Hello! I was wondering if it's possible to spawn a certain npc for example every first day of the month at startup?
 
Solution
Sure:
Lua:
local globalevent_spawnNPC_onDay = GlobalEvent("globalevent_spawnNPC_onDay")

-- NPC list and which day(s) of month they should spawn
spawnNpcList = {
    ["Banker"] = {
        ["pos"] = {x=1024, y=1024, z=7},
        ["dayList"] = {1}
    },
    ["Sam"] = {
        ["pos"] = {x=1024, y=1024, z=7},
        ["dayList"] = {14, 28}
    }
}
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
    local today = tonumber(os.date("%d", os.time()))
    local spawnedNPC = 0
    local shouldHaveSpawned = 0
    for npcName, npcInfo in pairs(spawnNpcList) do
        for _, day in ipairs(npcInfo["dayList"]) do
            -- If today is in the configured dayList of this NPC
            if today == day then...
RevScriptSys, place file in data/scripts/ folder, and restart server.
Lua:
local globalevent_spawnNPC_firstDay = GlobalEvent("globalevent_spawnNPC_firstDay")

function globalevent_spawnNPC_firstDay.onStartup()
	local day = tonumber(os.date("%d", os.time()))
    -- This is not the first day of any month
    if day > 1 then 
        return false 
    end

    -- Where do we want to place this NPC?
    local pos = Position(1024, 1024, 7)
    local npc = Game.createNpc("Banker", pos)
    if npc then 
        npc:setMasterPos(pos)
        pos:sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end

    print("Error: Failed to spawn NPC [".. npc:getName() .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].");
	return false
end

globalevent_spawnNPC_firstDay:register()
 
RevScriptSys, place file in data/scripts/ folder, and restart server.
Lua:
local globalevent_spawnNPC_firstDay = GlobalEvent("globalevent_spawnNPC_firstDay")

function globalevent_spawnNPC_firstDay.onStartup()
    local day = tonumber(os.date("%d", os.time()))
    -- This is not the first day of any month
    if day > 1 then
        return false
    end

    -- Where do we want to place this NPC?
    local pos = Position(1024, 1024, 7)
    local npc = Game.createNpc("Banker", pos)
    if npc then
        npc:setMasterPos(pos)
        pos:sendMagicEffect(CONST_ME_MAGIC_RED)
        return true
    end

    print("Error: Failed to spawn NPC [".. npc:getName() .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].");
    return false
end

globalevent_spawnNPC_firstDay:register()
Thank you! I had one more question would it be possible to make it that multiple npcs/positions/days could be given? For example now it's banker on every first day of the month. But would it be possible to make it that I can add another npc, Sam for example to spawn every 15th of the month?
 
Sure:
Lua:
local globalevent_spawnNPC_onDay = GlobalEvent("globalevent_spawnNPC_onDay")

-- NPC list and which day(s) of month they should spawn
spawnNpcList = {
    ["Banker"] = {
        ["pos"] = {x=1024, y=1024, z=7},
        ["dayList"] = {1}
    },
    ["Sam"] = {
        ["pos"] = {x=1024, y=1024, z=7},
        ["dayList"] = {14, 28}
    }
}
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
    local today = tonumber(os.date("%d", os.time()))
    local spawnedNPC = 0
    local shouldHaveSpawned = 0
    for npcName, npcInfo in pairs(spawnNpcList) do
        for _, day in ipairs(npcInfo["dayList"]) do
            -- If today is in the configured dayList of this NPC
            if today == day then
                shouldHaveSpawned = shouldHaveSpawned + 1
                -- Where do we want to place this NPC?
                local pos = Position(npcInfo.pos.x, npcInfo.pos.y, npcInfo.pos.z)
                local npc = Game.createNpc(npcName, pos)
                if npc then
                    npc:setMasterPos(pos)
                    pos:sendMagicEffect(CONST_ME_MAGIC_RED)
                    spawnedNPC = spawnedNPC + 1
                else
                    print("Error: Failed to spawn NPC [".. npcName .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].")
                end
            end
        end
    end
    print("Spawned [".. spawnedNPC .."] out of [".. shouldHaveSpawned .."] day configured NPC's on startup.")
    return true
end

globalevent_spawnNPC_onDay:register()
 
Last edited:
Solution
Sure:
Lua:
local globalevent_spawnNPC_onDay = GlobalEvent("globalevent_spawnNPC_onDay")

-- NPC list and which day(s) of month they should spawn
spawnNpcList = {
    ["Banker"] = {
        ["pos"] = {x=1024, y=1024, z=7},
        ["dayList"] = {1}
    },
    ["Sam"] = {
        ["pos"] = {x=1024, y=1024, z=7},
        ["dayList"] = {14, 28}
    }
}
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
    local today = tonumber(os.date("%d", os.time()))
    local spawnedNPC = 0
    local shouldHaveSpawned = 0
    for npcName, npcInfo in ipairs(spawnNpcList) do
        for _, day in ipairs(npcInfo["dayList"]) do
            -- If today is in the configured dayList of this NPC
            if today == day then
                shouldHaveSpawned = shouldHaveSpawned + 1
                -- Where do we want to place this NPC?
                local pos = Position(npcInfo.pos.x, npcInfo.pos.y, npcInfo.pos.z)
                local npc = Game.createNpc(npcName, pos)
                if npc then
                    npc:setMasterPos(pos)
                    pos:sendMagicEffect(CONST_ME_MAGIC_RED)
                    spawnedNPC = spawnedNPC + 1
                else
                    print("Error: Failed to spawn NPC [".. npcName .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].")
                end
            end
        end
    end
    print("Spawned [".. spawnedNPC .."] out of [".. shouldHaveSpawned .."] day configured NPC's on startup.")
    return true
end

globalevent_spawnNPC_onDay:register()
Hey thank you! But it doesn't seem to be working. I have configured the position (triple checked it) and at dayList I have used 27 (today). But it doesn't spawn and the console also says 0 out of 0 day npcs spawned
 
Hey thank you! But it doesn't seem to be working. I have configured the position (triple checked it) and at dayList I have used 27 (today). But it doesn't spawn and the console also says 0 out of 0 day npcs spawned

Add print statements inside the function so you can debug what's going on.
 
So added the following prints
Lua:
-- on startup
function globalevent_spawnNPC_onDay.onStartup()
    print("Test1")
    local today = tonumber(os.date("%d", os.time()))
    local spawnedNPC = 0
    local shouldHaveSpawned = 0
    for npcName, npcInfo in ipairs(spawnNpcList) do
        print("Test2")
        for _, day in ipairs(npcInfo["dayList"]) do
            print("Test3")
            -- If today is in the configured dayList of this NPC
            if today == day then
                print("Test4")
                shouldHaveSpawned = shouldHaveSpawned + 1
                -- Where do we want to place this NPC?
                local pos = Position(npcInfo.pos.x, npcInfo.pos.y, npcInfo.pos.z)
                local npc = Game.createNpc(npcName, pos)
                if npc then
                    print("Test5")
                    npc:setMasterPos(pos)
                    pos:sendMagicEffect(CONST_ME_MAGIC_RED)
                    spawnedNPC = spawnedNPC + 1
                else
                    print("Test6")
                    print("Error: Failed to spawn NPC [".. npcName .."] as position [".. pos.x ..", ".. pos.y ..", ".. pos.z .."].")
                end
            end
        end
    end
    print("Test7")
    print("Spawned [".. spawnedNPC .."] out of [".. shouldHaveSpawned .."] day configured NPC's on startup.")
    return true
end

Output:
output.jpg
 
Do I also need to change
for _, day in ipairs(npcInfo["dayList"]) do
for
for _, day in pairs(npcInfo["dayList"]) do
?

Nope, here is difference explained between pairs() and ipairs():
.
 
Back
Top