• 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+ spell can crash server?

T

Tibia Demon

Guest
i made spell and i need know if this will crash server or its good
and if anyone can show tips to write it in better way
i explain spell in tags if need more explain tell me and i explain again anypart
Lua:
local time = 30

local config = {
    ItemPos1 = Position(1014, 958, 7),
    ItemPos2 = Position(1019, 961, 7),
    ItemPos3 = Position(1022, 956, 7),
    ItemCheck1 = 1729,
    ItemCheck2 = 1730,
    ItemCheck3 = 1731,
    ItemCreate = 1728,
    AID = 5008,
    StorageCheck = PlayerStorageKeys.magictreestorage
}

-- create item i added decay in items.xml after time
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_TELEPORT)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_ENERGY)
combat:setParameter(COMBAT_PARAM_CREATEITEM, config.ItemCreate)
combat:setArea(createCombatArea(AREA_BEAM8))

local combatEffect = Combat()
combatEffect:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_POFF)
combatEffect:setArea(createCombatArea(AREA_BEAM8))

-- add timer to first front item
local function countDown(cid, position, time)
    local player = Player(cid)
    if not player or time == 0 then
        return
    end

    player:say(time, TALKTYPE_MONSTER_SAY, false, 0, position)
    addEvent(countDown, 1000, cid, position, time - 1)
end

-- show effect after 1 second one time only
local function EffectDelay(creatureId, variant)
    local creature = Creature(creatureId)
    if not creature then
        return
    end
    combatEffect:execute(creature, variant)
end

function onCastSpell(creature, variant)
    local player = Player(creature)
    local playersDir = player:getDirection()
    local position = variant:getPosition()
    local tile = Tile(position)
    if not tile then
        return false
    end
    -- check if tile i am looking at has actionid
    if not (tile:getGround():getActionId() == config.AID) then
        return false
    end
    -- check if the three items in pos
    local PosCheck1 = Tile(config.ItemPos1):getItemById(config.ItemCheck1)
    local PosCheck2 = Tile(config.ItemPos2):getItemById(config.ItemCheck2)
    local PosCheck3 = Tile(config.ItemPos3):getItemById(config.ItemCheck3)
    if not PosCheck1 or not PosCheck2 or not PosCheck3 then
        return false
    end
    -- check if player looking east
    if playersDir ~= DIRECTION_EAST then
        return false
    end
    -- check if player dont have spell timed storage
    if player:getStorageValue(config.StorageCheck) > os.time() then
        return false
    end
    if tile then
        -- do spell and add 30 second timed storage
        if tile:getGround():getActionId() == config.AID then
            player:setStorageValue(config.StorageCheck, os.time() + 30)
            position:sendMagicEffect(CONST_ME_TELEPORT)
            countDown(creature.uid, Variant.getPosition(variant), 30)
            combat:execute(creature, variant)
            addEvent(EffectDelay, 1000, creature:getId(), variant)
        end
    end
    return true
end
 
I am not a very good scripter.. but I typically spin up test server and test new spells, if it crashes, then I fix why it crash :)

Goodluck :D Best to learn from errors, so I just keep tweaking things until it works correctly
 
thank you. it doesnt crash but i want to be sure because addevent crashed with other spell before so maybe i forget something
and i want to see if i need all this checks or can write in better way
like this check
Lua:
-- check if the three items in pos
local PosCheck1 = Tile(config.ItemPos1):getItemById(config.ItemCheck1)
local PosCheck2 = Tile(config.ItemPos2):getItemById(config.ItemCheck2)
local PosCheck3 = Tile(config.ItemPos3):getItemById(config.ItemCheck3)
if not PosCheck1 or not PosCheck2 or not PosCheck3 then
    return false
end
 
thank you. it doesnt crash but i want to be sure because addevent crashed with other spell before so maybe i forget something
and i want to see if i need all this checks or can write in better way
like this check
Lua:
-- check if the three items in pos
local PosCheck1 = Tile(config.ItemPos1):getItemById(config.ItemCheck1)
local PosCheck2 = Tile(config.ItemPos2):getItemById(config.ItemCheck2)
local PosCheck3 = Tile(config.ItemPos3):getItemById(config.ItemCheck3)
if not PosCheck1 or not PosCheck2 or not PosCheck3 then
    return false
end
Usually the Addevent can crash the server, if in the function it triggers you need a player or something that doesn't exist anymore.
Lets say you have a spell with addevent for 5 seconds later, that needs the casting player to execute the function.

if the player is logged out at the time the addevent triggers, and you don't have a check in the function to see if the player is nil, it will crash
 
Back
Top