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

Npc Rashid

xshaggyx

New Member
Joined
Feb 20, 2016
Messages
42
Reaction score
2
I have a question
About npc rashid I can not make it change according to the day I can help
 
Solution
It's not pretty, but it's simple and works. 0.3.7 scripting style/code.
Code:
function onStartUp()
    if os.date('%A') == "Sunday" then
        doCreateNpc("Rashid", {x = 11111, y = 11111, z = 11})
    elseif os.date('%A') == "Monday" then
        doCreateNpc("Rashid", {x = 22222, y = 22222, z = 22})
    elseif os.date('%A') == "Tuesday" then
        doCreateNpc("Rashid", {x = 33333, y = 33333, z = 33})
    elseif os.date('%A') == "Wednesday" then
        doCreateNpc("Rashid", {x = 44444, y = 44444, z = 44})
    elseif os.date('%A') == "Thursday" then
        doCreateNpc("Rashid", {x = 55555, y = 55555, z = 55})
    elseif os.date('%A') == "Friday" then
        doCreateNpc("Rashid", {x = 66666, y = 66666, z = 66})
    elseif os.date('%A')...
It's not pretty, but it's simple and works. 0.3.7 scripting style/code.
Code:
function onStartUp()
    if os.date('%A') == "Sunday" then
        doCreateNpc("Rashid", {x = 11111, y = 11111, z = 11})
    elseif os.date('%A') == "Monday" then
        doCreateNpc("Rashid", {x = 22222, y = 22222, z = 22})
    elseif os.date('%A') == "Tuesday" then
        doCreateNpc("Rashid", {x = 33333, y = 33333, z = 33})
    elseif os.date('%A') == "Wednesday" then
        doCreateNpc("Rashid", {x = 44444, y = 44444, z = 44})
    elseif os.date('%A') == "Thursday" then
        doCreateNpc("Rashid", {x = 55555, y = 55555, z = 55})
    elseif os.date('%A') == "Friday" then
        doCreateNpc("Rashid", {x = 66666, y = 66666, z = 66})
    elseif os.date('%A') == "Saturday" then
        doCreateNpc("Rashid", {x = 77777, y = 77777, z = 77})
    end
    return true
end
 
Solution
Updated version to include multiple npc's, since this thread was linked to semi-recently. xD

Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    }
}

function onStartUp()
    local currentDay = os.date('%A')
    for v, k in pairs(days) do
        if currentDay == v then
            for i = 1, #k do
                doCreateNpc(k[i].npc, k[i].pos)
            end
            break
        end
    end
    return true
end
 
Updated version to include multiple npc's, since this thread was linked to semi-recently. xD

Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}}
    }
}

function onStartUp()
    local currentDay = os.date('%A')
    for v, k in pairs(days) do
        if currentDay == v then
            for i = 1, #k do
                doCreateNpc(k[i].npc, k[i].pos)
            end
            break
        end
    end
    return true
end

Xini all working fine, but this work only when server restart.
How change script to Create NPC Rashid on Time 23:55 on first place, second place etc ? Thanks for your reply.
 
Xini all working fine, but this work only when server restart.
How change script to Create NPC Rashid on Time 23:55 on first place, second place etc ? Thanks for your reply.
Every 5 minutes he changes position?

Or every time it changes to a new day he changes position?
Post automatically merged:

Well w/e, just gonna assume you meant the latter

NPC's are created upon start-up, based on day.
If deSpawnOnNextDay = true those npcs are removed from the server whenever it moves to the next day. (up to 5 minute delay)
if deSpawnOnNextDay = false they are not removed until next server shutdown. (note: if server is online for 8+ days, you'll get double spawned npcs.)
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        if isCreature(k.creature) then
            doRemoveCreature(k.creature)
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local creature = doCreateNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {creature = creature}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    npcSpawnChecker()
    return true
end
 
Last edited:
Every 5 minutes he changes position?

Or every time it changes to a new day he changes position?
Post automatically merged:

Well w/e, just gonna assume you meant the latter

NPC's are created upon start-up, based on day.
If deSpawnOnNextDay = true those npcs are removed from the server whenever it moves to the next day. (up to 5 minute delay)
if deSpawnOnNextDay = false they are not removed until next server shutdown. (note: if server is online for 8+ days, you'll get double spawned npcs.)
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local creature = Creature(k.creatureId)
        if creature then
            creature:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local creature = doCreateNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {creatureId = creature:getId()}
                    end
                end
                addEvent(npcSpawnChecker, delayBetweenChecks)
                break
            end
        end
    end
end

function onStartUp()
    npcSpawnChecker()
    return true
end

Xikini , not every 5 minutes.
Function on start-up = create NPC only when do restart server.
If I don't restart server, NPC stay on position (etc. MONDAY) - two days not restart stay on monday position.
Sorry for my f**king ENG.

Maybe use function TIME, and removecreature (position prev) and createcreature (postionon next) ?
 
Xikini , not every 5 minutes.
Function on start-up = create NPC only when do restart server.
If I don't restart server, NPC stay on position (etc. MONDAY) - two days not restart stay on monday position.
Sorry for my f**king ENG.

Maybe use function TIME, and removecreature (position prev) and createcreature (postionon next) ?
The original script already does this..

they spawn on start-up, and no other npc's will spawn regardless of how long the server has been up.

Server stays up for 5 days, and it'll stay monday npc
 
The original script already does this..

they spawn on start-up, and no other npc's will spawn regardless of how long the server has been up.

Server stays up for 5 days, and it'll stay monday npc

Ok.
I need a script where I won't do a restart of the NPC server and so it will be in an additional place.
This script work only startup.

Maybe change this to functiononTime and use time: "23:59" ?
function onStartUp()
npcSpawnChecker()
return true
end
 
Every 5 minutes he changes position?

Or every time it changes to a new day he changes position?
Post automatically merged:

Well w/e, just gonna assume you meant the latter

NPC's are created upon start-up, based on day.
If deSpawnOnNextDay = true those npcs are removed from the server whenever it moves to the next day. (up to 5 minute delay)
if deSpawnOnNextDay = false they are not removed until next server shutdown. (note: if server is online for 8+ days, you'll get double spawned npcs.)
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        if isCreature(k.creature) then
            doRemoveCreature(k.creature)
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local creature = doCreateNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {creature = creature}
                    end
                end
                addEvent(npcSpawnChecker, delayBetweenChecks)
                break
            end
        end
    end
end

function onStartUp()
    npcSpawnChecker()
    return true
end
Sorry for probably asking a dumb question but do I put this in globalevents as a new script or do I add it to a existing script?
Once again sorry, I'm relatively new to this :p
 
Sorry for probably asking a dumb question but do I put this in globalevents as a new script or do I add it to a existing script?
Once again sorry, I'm relatively new to this :p
yes xP
as a new script

data/globalevents/globalevents.xml
XML:
<globalevent name="xxxxxxxxx" type="start" event="script" value="xxxxxxxxx.lua"/>
and script goes into data/globalevents/scripts/xxxxxxxxx.lua
 
yes xP
as a new script

data/globalevents/globalevents.xml
XML:
<globalevent name="xxxxxxxxx" type="start" event="script" value="xxxxxxxxx.lua"/>
and script goes into data/globalevents/scripts/xxxxxxxxx.lua
Thank you sir and this should work for tfs 1.2 too right? =)
 
Thank you sir and this should work for tfs 1.2 too right? =)
No, the above is for tfs 0.x servers.

tfs 1.0+ is below

XML:
<globalevent type="startup" name="XxxxxXxxxx" script="xxxxxxx.lua" />
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local npc = Npc(k.npcId)
        if isNpc(npc) then
            npc:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local npc = Game.createNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {npcId = npc:getId()}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    npcSpawnChecker()
    return true
end
 
No, the above is for tfs 0.x servers.

tfs 1.0+ is below

XML:
<globalevent type="startup" name="XxxxxXxxxx" script="xxxxxxx.lua" />
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local npc = Npc(k.npcId)
        if isNpc(npc) then
            npc:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local npc = Game.createNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {npcId = npc:getId()}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    npcSpawnChecker()
    return true
end
Thanks but I am getting following error:
[Warning - Event::checkScript] Event onStartup not found. scripts/npctravel.lua

Tried adding
Lua:
function onStartup()
to the script and error went away but rashid is not spawning
 
Thanks but I am getting following error:
[Warning - Event::checkScript] Event onStartup not found. scripts/npctravel.lua

Tried adding
Lua:
function onStartup()
to the script and error went away but rashid is not spawning
Are you sure you copied the entire script?
Because onStartUp is at the bottom.
 
Are you sure you copied the entire script?
Because onStartUp is at the bottom.
Yes that's what I found odd.
I have only changed x,y,z.

in globalevents.xml
XML:
<globalevent type="startup" name="TravellingNPC" script="npctravel.lua" />

and the script:
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local npc = Npc(k.npcId)
        if isNpc(npc) then
            npc:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local npc = Game.createNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {npcId = npc:getId()}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    npcSpawnChecker()
    return true
end
 
Yes that's what I found odd.
I have only changed x,y,z.

in globalevents.xml
XML:
<globalevent type="startup" name="TravellingNPC" script="npctravel.lua" />

and the script:
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 32348, y = 32832, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local npc = Npc(k.npcId)
        if isNpc(npc) then
            npc:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local npc = Game.createNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {npcId = npc:getId()}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    npcSpawnChecker()
    return true
end
Try this.

Tell me what prints to console
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true},
        --{npc = "Npc 2",  pos = Position(1000, 1000, 7), deSpawnOnNextDay = false},
        --{npc = "Npc 3",  pos = Position(1000, 1000, 7), deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local npc = Npc(k.npcId)
        if isNpc(npc) then
            npc:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    print("Checking current day..")
    local currentDay = os.date('%A')
    print(currentDay, previousDay)
    if currentDay ~= previousDay then
        print("New day found.. spawning npcs..")
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    print("Spawning Npc named " .. k[i].npc .. "")
                    local npc = Game.createNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        print("adding to npc table for removal, as deSpawnOnNextDay = true")
                        npcs[#npcs + 1] = {npcId = npc:getId()}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartup()
    print("TravellingNPC script is functional.")
    npcSpawnChecker()
    return true
end
 
Last edited:
Try this.

Tell me what prints to console
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true},
        --{npc = "Npc 2",  pos = Position(1000, 1000, 7), deSpawnOnNextDay = false},
        --{npc = "Npc 3",  pos = Position(1000, 1000, 7), deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = Position(32348, 32832, 7), deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        local npc = Npc(k.npcId)
        if isNpc(npc) then
            npc:remove()
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    print("Checking current day..")
    local currentDay = os.date('%A')
    print(currentDay, previousDay)
    if currentDay ~= previousDay then
        print("New day found.. spawning npcs..")
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    print("Spawning Npc named " .. k[i].npc .. "")
                    local npc = Game.createNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        print("adding to npc table for removal, as deSpawnOnNextDay = true")
                        npcs[#npcs + 1] = {npcId = npc:getId()}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    print("TravellingNPC script is functional.")
    npcSpawnChecker()
    return true
end
Code:
>> Loading config
>> Establishing database connection... MySQL 6.1.2
>> Running database manager
> Optimizing table players_online... [success]
> Optimizing table tile_store... [success]
>> Loading groups
>> Loading vocations
>> Loading items
>> Loading script systems
[Warning - Event::checkScript] Event onStartup not found. scripts/npctravel.lua
>> Loading monsters
>> Loading outfits
>> Checking world type... PVP
>> Loading map
> Map size: 33568x33111.
> Map loading time: 4.967 seconds.
> Loaded house items in: 0.017 s
>> Initializing gamestate
>> Loaded all modules, server starting up...
>> Forgotten Server Online!

^ same as before =(
 
Code:
>> Loading config
>> Establishing database connection... MySQL 6.1.2
>> Running database manager
> Optimizing table players_online... [success]
> Optimizing table tile_store... [success]
>> Loading groups
>> Loading vocations
>> Loading items
>> Loading script systems
[Warning - Event::checkScript] Event onStartup not found. scripts/npctravel.lua
>> Loading monsters
>> Loading outfits
>> Checking world type... PVP
>> Loading map
> Map size: 33568x33111.
> Map loading time: 4.967 seconds.
> Loaded house items in: 0.017 s
>> Initializing gamestate
>> Loaded all modules, server starting up...
>> Forgotten Server Online!

^ same as before =(
copy again, I edited post. xP
 
Every 5 minutes he changes position?

Or every time it changes to a new day he changes position?
Post automatically merged:

Well w/e, just gonna assume you meant the latter

NPC's are created upon start-up, based on day.
If deSpawnOnNextDay = true those npcs are removed from the server whenever it moves to the next day. (up to 5 minute delay)
if deSpawnOnNextDay = false they are not removed until next server shutdown. (note: if server is online for 8+ days, you'll get double spawned npcs.)
Lua:
local days = {
    ["Sunday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true},
        {npc = "Npc 2",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false},
        {npc = "Npc 3",  pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = false}
    },
    ["Monday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Tuesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Wednesday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Thursday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Friday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    },
    ["Saturday"] = {
        {npc = "Rashid", pos = {x = 1000, y = 1000, z = 7}, deSpawnOnNextDay = true}
    }
}

local delayBetweenChecks = 300000 -- 1000 * 60 * 5 (5 minutes)
local previousDay = "Day"
local npcs = {}

local function removeNpcs()
    for v, k in pairs(npcs) do
        if isCreature(k.creature) then
            doRemoveCreature(k.creature)
        end
    end
    npcs = {}
end

local function npcSpawnChecker()
    local currentDay = os.date('%A')
    if currentDay ~= previousDay then
        previousDay = currentDay
        removeNpcs()
        for v, k in pairs(days) do
            if currentDay == v then
                for i = 1, #k do
                    local creature = doCreateNpc(k[i].npc, k[i].pos)
                    if k[i].deSpawnOnNextDay then
                        npcs[#npcs + 1] = {creature = creature}
                    end
                end
                break
            end
        end
    end
    addEvent(npcSpawnChecker, delayBetweenChecks)
end

function onStartUp()
    npcSpawnChecker()
    return true
end

Don't Work ... @EDIT - All working fine THX!!!!!

1655750419940.png
 
Last edited:
Back
Top