• Congratulations to the OTLand February Mapping Competition Winner! The grand prize of Otland Premium goes to eiserne festung.
  • 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+ Castle event

jel

Member
Joined
Mar 22, 2014
Messages
183
Reaction score
8
I have this error in the castle's lib.

Lua:
CASTELO_STOR = 37894
EVENTO_CASTELO_STOR = CASTELO_STOR + 1
castleOwner = 0
CENTRE_POS = {x = 17435, y = 17221, z = 7} --pos

EVENT_CLOSED = 0
EVENT_WAIT = 1
EVENT_STARTED = 2

DAY = "Monday" --day
TIME_EVENT = 5 --mins

MONSTER_NAME = "Castle Defender"
TIME_TO_REVIVE_MONSTER = 2 --mins
POS_MONSTER_CREATE = {x = 17435, y = 17217, z = 7} --pos

guildsOnEvent =
{

}

local function getIndex(arr, val)
    local ret = -1
    for i = 1, #arr do
        if (arr[i] == val) then
            ret = i
            break
        end
    end

    return ret
end

--to enter to event
function Player:registerToCastle()
    if (not self:isCreature()) then return false end
    local guild = self:getGuild()

    if (guild) then
        if (self:isCastleEventMember()) then
            return true
        end
        local guildId = guild:getId()
        if not guildsOnEvent[guildId] then
            guildsOnEvent[guildId] = {}
        end
        table.insert(guildsOnEvent[guildId], self:getId())
    end
end

function Player:kickFromCastle()
    if (not self:isCreature()) then return false end
    local guild = self:getGuild()

    if (guild) then
        if (not self:isCastleEventMember()) then
            return true
        end
        local guildId = guild:getId()
        if (isInArray(guildsOnEvent[guildId], self:getId())) then
            local mIndex = getIndex(guildsOnEvent[guildId], self:getId())
            table.remove(guildsOnEvent[guildId], mIndex)
        end
    end
end

function cleanGuildsFromCastle()
    guildsOnEvent = {}
end

-- to check who is castle owner
function getGuildCastleOwner()
    return castleOwner
end

function setGuildCastleOwner(guild)
    -- body
    if (not guild) then return false end
    setGlobalStorageValueDB(CASTELO_STOR, guild:getId())
    Game.broadcastMessage(string.format("A guild %s conquistou o castle-48h!", guild:getName()))
    castleOwner = guild:getId()
end

function Player:isGuildCastleOwnerMember()
    if (not self:isCreature()) then return false end

    if (self:getGuild() and self:getGuild():getId() == getGuildCastleOwner()) then
        return true
    end
    return false
end

--players

function Player:isCastleEventMember()
    if (not self) then return false end

    local guild = self:getGuild()

    if (guild) then
        local guildId = guild:getId()
        if (guildsOnevent[guildId] and isInArray(guildsOnevent[guildId], self:getId())) then
            return true
        end
    end

    return false
end
function getCastleOwners()
    local owners = {}

    if (castleOwner > 0 ) then
        owners = guildsOnevent[castleOwner]
    end
    return owners
end

function getCastleEventMembers()
    local members = {}

    for i,x in pairs(guildsOnEvent) do
        for j,p in pairs(x) do
            if (p and not p:isGuildCastleOwnerMember()) then
                table.insert(members, p)
            end
        end
    end

    return members
end

function teleportCastleEventMembers(owners, members)
    --owners, members
    if (owners) then

        for i, x in pairs (getCastleOwners()) do
            x:teleportTo(CENTRE_POS)
        end
    end

    if (members) then
        for i, x in pairs (getCastleEventMembers()) do
            if (x:isCreature()) then
                x:teleportTo(x:getTown():getTemplePosition())
            end
        end
    end
end

-- control event
function startCastleEvent()
    Game.setStorageValue(EVENTO_CASTELO_STOR, EVENT_STARTED)
    createCastleEventMonster(false)
    addEvent(closeCastleEvent, TIME_EVENT * 60 * 1000)
end

function getCastleEventStatus()
    local st = Game.getStorageValue(EVENTO_CASTELO_STOR)
    local ret
    if (st <= 0) then
        ret = EVENT_CLOSED
    elseif (st == 1) then
        ret = EVENT_WAIT
    elseif (st == 2) then
        ret = EVENT_STARTED
    end

    return ret
end

function closeCastleEvent()
    cleanGuildsFromCastle()
    Game.setStorageValue(EVENTO_CASTELO_STOR, EVENT_CLOSED)
end

function createCastleEventMonster(teleportPlayers)
    if (getCastleEventStatus() ~= EVENT_STARTED) then
        teleportCastleEventMembers(true, true)
        return false
    end
    if (teleportPlayers) then
        teleportCastleEventMembers(true, true)
        Game.broadcastMessage(string.format("Castle Defender will respawn in %d minutes", TIME_TO_REVIVE_MONSTER))
        addEvent(Game.createMonster, TIME_TO_REVIVE_MONSTER * 60 * 1000, MONSTER_NAME, POS_MONSTER_CREATE)
    else
        Game.createMonster(MONSTER_NAME, POS_MONSTER_CREATE)
    end
end

--[[
--creatureevent
dofile("data/lib/events/castelo.lua")
function onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
    if (lasthitkiller:isPlayer()) then
        local guild = lasthitkiller:getGuild()
        if (guild) then
            setGuildCastleOwner(guild)
        else
            Game.broadcastMessage(string.format("%s matou o king mais ele n e membro do uma guild.", lasthitkiller:getName()))
        end
        --time left??
        createCastleEventMonster(true)
    end
    return true
end


--movement (--pending: enter during event execution (30 mins))
dofile("data/lib/events/castelo.lua")
function onStepIn(creature, item, pos, fromPosition)
    if (creature:isPlayer()) then
        if (getCastleEventStatus() <= EVENT_CLOSED) then
            if (castleOwner > 0) then
                local guild = creature:getGuild()
                if (guild) then
                    if (guild:getId() ~= castleOwner) then
                        creature:teleportTo(fromPosition)
                    end
                else
                    creature:teleportTo(fromPosition)
                end
            else
                creature:teleportTo(fromPosition)
            end
        end
    end
    return true
end


--globalevent
dofile("data/lib/events/castelo.lua")
function onTime()

    local day = os.date("%A")
    if (day == DAY) then
        startCastleEvent()
    end
    return true
end

dofile("data/lib/events/castelo.lua")
function onStartup()
    castleOwner = getGlobalStorageValueDB(CASTELO_STOR)
    return true
end


    getEventTimeLeft
    -- get all info required from players
    --onStartup: set guildOwner value
]]--

erro:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/castle48.lua:eek:nDeath
data/lib/castle48.lua:113: attempt to index global 'guildsOnevent' (a nil value)
stack traceback:
[C]: in function '__index'
data/lib/castle48.lua:113: in function 'getCastleOwners'
data/lib/castle48.lua:136: in function 'teleportCastleEventMembers'
data/lib/castle48.lua:182: in function 'createCastleEventMonster'
data/creaturescripts/scripts/castle48.lua:11: in function <data/creaturescripts/scripts/castle48.lua:2>
 
OP
J

jel

Member
Joined
Mar 22, 2014
Messages
183
Reaction score
8
replace guildsOnevent with guildsOnEvent
now gave this error, solution?
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/castle48.lua:eek:nDeath
data/lib/castle48.lua:136: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
[C]: at 0x004f1450
[C]: in function 'pairs'
data/lib/castle48.lua:136: in function 'teleportCastleEventMembers'
data/lib/castle48.lua:182: in function 'createCastleEventMonster'
data/creaturescripts/scripts/castle48.lua:11: in function <data/creaturescripts/scripts/castle48.lua:2>
 
Top