• 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+ Ghazbaran onStartUp

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,470
Solutions
27
Reaction score
844
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi! I have this script that is supposed to respawn Ghazbaran the Fridays, but it is giving an error onStartUp. I use TFS 1.5 downgraded by Nekiro, any ideas of which part of the script is causing the error? Thanks in advance, here's the script and the console log :)

Lua:
local config = {
    ['Friday'] = Position(861, 890, 12)
}

local spawnByDay = true

function onStartup()
    if spawnByDay then
        local npc = Game.createMonster('Ghazbaran', config[os.date('%A')], false, true)
        if npc then
            npc:setMasterPos(config[os.date('%A')])
        end
    else
        local npc
        for k, position in pairs(config) do
            npc = Game.createMonster('Ghazbaran', position, false, true)
            if npc then
                npc:setMasterPos(position)
            end
        end
    end

    return true
end

startup.png

Regards!
 
Solution
Lua:
local spawnByDay = true

function onStartup()
    if spawnByDay then
        if os.date('%A') == 'Friday' then
            Game.createMonster("Ghazbaran", Position(861, 890, 12))
        end
    end  

    return true
end
Lua:
local spawnByDay = true

function onStartup()
    if spawnByDay then
        if os.date('%A') == 'Friday' then
            Game.createMonster("Ghazbaran", Position(861, 890, 12))
        end
    end  

    return true
end
 
Solution
you have this error because you trying to access some value that does not exist, you only have friday and as this script check by day, if there is no day it means it is a null value so you trying to use a null value while creating the monster

tldr:

Lua:
local config = {
    ['Friday'] = {
        position = Position(861, 890, 12),
        monsterName = "Ghazbaran",
    }
}
function onStartup()
    local spawn = config[os.date('%A')]
    if spawn then
        local monster = Game.createMonster(spawn.monsterName, spawn.position, false, true)
        if monster then
            monster:setMasterPos(spawn.position)
        end
    end
    return true
end
 
you have this error because you trying to access some value that does not exist, you only have friday and as this script check by day, if there is no day it means it is a null value so you trying to use a null value while creating the monster

tldr:

Lua:
local config = {
    ['Friday'] = {
        position = Position(861, 890, 12),
        monsterName = "Ghazbaran",
    }
}
function onStartup()
    local spawn = config[os.date('%A')]
    if spawn then
        local monster = Game.createMonster(spawn.monsterName, spawn.position, false, true)
        if monster then
            monster:setMasterPos(spawn.position)
        end
    end
    return true
end

Thanks!!! Added to server, i'll post tomorrow if all works fine. In 3~ hours the server will start automatically and i'll see if everything goes well :)

Lua:
local spawnByDay = true

function onStartup()
    if spawnByDay then
        if os.date('%A') == 'Friday' then
            Game.createMonster("Ghazbaran", Position(861, 890, 12))
        end
    end

    return true
end

This seems to be a correct solution for this too! Thanks a lot I appreciate your help! :D
 
is there even a setMasterPos? because it should be ok
as I said, there is no setmasterpos for monster so the nil error is because of that
 
is there even a setMasterPos? because it should be ok
as I said, there is no setmasterpos for monster so the nil error is because of that

Yeah I found it weird too. I took the script from orts2 datapack but I guess it wasn't what I was looking for. It's my mistake, I gave it a turn and saw this it was based on Rashid daily respawn, so I guess the syntax of the script was wrong for what I wanted to achieve.

Anyways, the other script worked well, no errors so far :)
Thanks again for the help @StreamSide it's very appreciated! :D
 
There isn't a setMasterPosition in TFS for monsters, I actually made it myself because of this problem.

luascript.h
under
Code:
static int luaMonsterRename(lua_State* L);

add
C++:
static int luaMonsterSetMasterPosition(lua_State* L);

luascript.cpp

under
Code:
registerMethod("Monster", "rename", LuaScriptInterface::luaMonsterRename);

add
C++:
registerMethod("Monster", "setMasterPosition", LuaScriptInterface::luaMonsterSetMasterPosition);

find
Code:
int LuaScriptInterface::luaMonsterRename(lua_State* L)

under that whole method add
Lua:
int LuaScriptInterface::luaMonsterSetMasterPosition(lua_State* L)
{
    // monster:setMasterPosition(pos)
    Monster* monster = getUserdata<Monster>(L, 1);
    if (!monster) {
        lua_pushnil(L);
        return 1;
    }

    Position position;
    if (isTable(L, 2)) {
        position = getPosition(L, 2);
    }
    else {
        position.x = getNumber<uint16_t>(L, 2);
        position.y = getNumber<uint16_t>(L, 3);
        position.z = getNumber<uint16_t>(L, 4);
    }

    Tile* tile = g_game.map.getTile(position);
    if (!tile) {
        lua_pushnil(L);
        return 1;
    }

    monster->setMasterPos(position);
    pushBoolean(L, true);
    return 1;
}

Then you can set the master pos for the monster so he doesn't run around all over.
 
Back
Top