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

Solved Script question TFS 1.2

Aeronx

Intermediate OT User
Joined
Dec 17, 2015
Messages
738
Solutions
9
Reaction score
123
Hello ladies and gentlemen! I've got a little script that tp summons to master:

Code:
local distFromMaster = 6

function onThink(interval)
    for _, player in ipairs(Game.getPlayers()) do
        local playerPos = player:getPosition()
        if not Tile(playerPos):hasFlag(TILESTATE_PROTECTIONZONE) then
            local summons = player:getSummons()
            if #summons ~= 0 then
                for i = 1, #summons do
                    local summon = summons[i]
                    local summonPos = summon:getPosition()
                    if summonPos.z ~= playerPos.z or summonPos:getDistance(playerPos) > distFromMaster then
                        summon:teleportTo(playerPos)
                                            end
                end
            end
        end
    end
    return true
end

But now, i have a summon that i dont want to be teleported to master, how can i make "if summon == fire elemental" no telportTo..!

Thanks everybody!
 
Why do you have 5 addEvents to the same function with different times?
They seem to be the same structured function, basically just a copy and paste of the other.

Functions in general should be generated as generic or non-specific code.

Regardless if it works or not, this here is a poor programming practice to develop.
Code:
        addEvent(function(cid)
       
    local master = getCreatureMaster(cid)
                if master then
                    local party = getPartyMembers(master)
                    local masterPosition = getCreaturePosition(master)
                    local totempos = getCreaturePosition(cid)
                    if party == nil or type(party) ~= "table" or #party <= 1 then
                        doCreatureAddHealth(master, c.health)
                        doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                        doSendMagicEffect(masterPosition, 12)
                        return true
                    end
                    local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
                        for _, member in pairs(party) do
                            if isInArray(players, member) then
                                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                                doCreatureAddHealth(member, c.health)
                                doSendMagicEffect(getCreaturePosition(member), 12)
                            
                            end
                        end
                        end
        end, 1000, creature:getId())

        addEvent(function(cid)
     local master = getCreatureMaster(cid)
                if master then
                    local party = getPartyMembers(master)
                    local masterPosition = getCreaturePosition(master)
                    local totempos = getCreaturePosition(cid)
                    if party == nil or type(party) ~= "table" or #party <= 1 then
                        doCreatureAddHealth(master, c.health)
                        doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                        doSendMagicEffect(masterPosition, 12)
                        return true
                    end
                    local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
                        for _, member in pairs(party) do
                            if isInArray(players, member) then
                                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                                doCreatureAddHealth(member, c.health)
                                doSendMagicEffect(getCreaturePosition(member), 12)
                            
                            end
                        end
                        end
        end, 3000, creature:getId())

        addEvent(function(cid)
         local master = getCreatureMaster(cid)
                if master then
                    local party = getPartyMembers(master)
                    local masterPosition = getCreaturePosition(master)
                    local totempos = getCreaturePosition(cid)
                    if party == nil or type(party) ~= "table" or #party <= 1 then
                        doCreatureAddHealth(master, c.health)
                        doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                        doSendMagicEffect(masterPosition, 12)
                        return true
                    end
                    local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
                        for _, member in pairs(party) do
                            if isInArray(players, member) then
                                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                                doCreatureAddHealth(member, c.health)
                                doSendMagicEffect(getCreaturePosition(member), 12)
                            
                            end
                        end
                        end
        end, 5000, creature:getId())

        addEvent(function(cid)
          local master = getCreatureMaster(cid)
                if master then
                    local party = getPartyMembers(master)
                    local masterPosition = getCreaturePosition(master)
                    local totempos = getCreaturePosition(cid)
                    if party == nil or type(party) ~= "table" or #party <= 1 then
                        doCreatureAddHealth(master, c.health)
                        doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                        doSendMagicEffect(masterPosition, 12)
                        return true
                    end
                    local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
                        for _, member in pairs(party) do
                            if isInArray(players, member) then
                                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                                doCreatureAddHealth(member, c.health)
                                doSendMagicEffect(getCreaturePosition(member), 12)
                            
                            end
                        end
                        end
        end, 7000, creature:getId())

        addEvent(function(cid)
                local master = getCreatureMaster(cid)
                if master then
                    local party = getPartyMembers(master)
                    local masterPosition = getCreaturePosition(master)
                    local totempos = getCreaturePosition(cid)
                    if party == nil or type(party) ~= "table" or #party <= 1 then
                        doCreatureAddHealth(master, c.health)
                        doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                        doSendMagicEffect(masterPosition, 12)
                        return true
                    end
                    local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
                        for _, member in pairs(party) do
                            if isInArray(players, member) then
                                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                                doCreatureAddHealth(member, c.health)
                                doSendMagicEffect(getCreaturePosition(member), 12)
                            
                            end
                        end
                        end
        end, 9000, creature:getId())
 
Now it should be easier to trouble-shoot.
Code:
local c = {
    health = 1000,
    rangex = 3,
    rangey = 3,
    multifloor = false
}
local eventsFired = {}
local hitArea = createCombatArea(AREA_CIRCLE3X3)
function isInArray(array, value)
    if type(array) == 'table' then
        for _, v in pairs(array) do
            if v == value then
                return true
            end
        end
    end
    return false
end

function genericFunction(cid)
    local master = getCreatureMaster(cid)
    if master then
        local party = getPartyMembers(master)
        local masterPosition = getCreaturePosition(master)
        local totempos = getCreaturePosition(cid)
        if party == nil or type(party) ~= "table" or #party <= 1 then
            doCreatureAddHealth(master, c.health)
            doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
            doSendMagicEffect(masterPosition, 12)
            return true
        end
        local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
        for _, member in pairs(party) do
            if isInArray(players, member) then
                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                doCreatureAddHealth(member, c.health)
                doSendMagicEffect(getCreaturePosition(member), 12)

            end
        end
    end
end

local times = {1, 3, 5, 7, 9}

function onThink(creature)

    if not eventsFired[creature:getId()] then
        addEvent(function(cid)
            local creature = Creature(cid)
            if creature then
            creature:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONAREA)
                creature:remove()
            end
        end, 10000, creature:getId())
      -- local damage = onTargetCreature(creature, target)
        for i = 1, #times do
            addEvent(genericFunction, times[i] * 1000, creature:getId())
        end
   
        eventsFired[creature:getId()] = true
    end
    return true
end
 
Well, yeah, it looks way nicer now.. I dont know anything about programming, so i can only look for the enums and try to make it work, i know how to read it, but not how to write it, thats why my code looks so messy! :p
Now that i've seen how you did it, i know how to do it aswell, thats how i learn haha.

Now I've tested on your code:
Code:
local c = {
    health = 1000,
    rangex = 3,
    rangey = 3,
    multifloor = false
}
local eventsFired = {}
local hitArea = createCombatArea(AREA_CIRCLE3X3)
function isInArray(array, value)
    if type(array) == 'table' then
        for _, v in pairs(array) do
            if v == value then
                return true
            end
        end
    end
    return false
end

function genericFunction(cid)
    local master = getCreatureMaster(cid)
    if master then
        local distFromMaster = 3
        local party = getPartyMembers(master)
        local masterPosition = getCreaturePosition(master)
        local totempos = getCreaturePosition(cid)
        if party == nil or type(party) ~= "table" or #party <= 1 then
        if totempos.z ~= masterPosition.z or totempos:getDistance(masterPosition) > distFromMaster then
            doCreatureAddHealth(master, c.health)
            doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
            doSendMagicEffect(masterPosition, 12)
            return true
            end
        end
        local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
        for _, member in pairs(party) do
            if isInArray(players, member) then
                doAreaCombatHealth(creature, COMBAT_FIREDAMAGE, creature:getPosition(), hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
                doCreatureAddHealth(member, c.health)
                doSendMagicEffect(getCreaturePosition(member), 12)

            end
        end
    end
end

local times = {1, 3, 5, 7, 9}

function onThink(creature)

    if not eventsFired[creature:getId()] then
        addEvent(function(cid)
            local creature = Creature(cid)
            if creature then
            creature:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONAREA)
                creature:remove()
            end
        end, 10000, creature:getId())
        for i = 1, #times do
            addEvent(genericFunction, times[i] * 1000, creature:getId())
        end

        eventsFired[creature:getId()] = true
    end
    return true
end

getting this error:

Code:
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/creaturescripts/scripts/custom/healing totem.lua:37: bad argument #1 to 'pairs' (table expected, got boolean)
stack traceback:
        [C]: ?
        [C]: in function 'pairs'
        data/creaturescripts/scripts/custom/healing totem.lua:37: in function <data/creaturescripts/scripts/custom/healing totem.lua:20>

It's the same error i had on my version :p
 
Last edited:
im assuming
for _, member in pairs(party) do
is line 37
which means party is NOT a table, but rathern boolean (true/false)
try adding
print(party, type(party))
after local party = ... and say what it prints

however, i think the problem lies on that party is nil, but this line
if totempos.z ~= masterPosition.z or totempos:getDistance(masterPosition) > distFromMaster then
are both false, and since return true lies INSIDE that after party == nil etc, it goes to loop even with invalid party..
try moving return true on line 32 to between ends on line 33/34
 
This is the final version. Bug less, working perfect as intended.

Code:
local c = {
    rangex = 2,
    rangey = 2,
    multifloor = false
}

local eventsFired = {}

local hitArea = createCombatArea(AREA_SQUARE2X2)

function isInArray(array, value)
    if type(array) == 'table' then
        for _, v in pairs(array) do
            if v == value then
                return true
            end
        end
    end
    return false
end

function genericFunction(cid)
    local master = getCreatureMaster(cid)
    if master then
        local distFromMaster = 3
        local party = getPartyMembers(master)
        local masterPosition = getCreaturePosition(master)
        local totempos = getCreaturePosition(cid)
        local min = ((getPlayerLevel(master) * 0.3) + (getPlayerMagLevel(master) * 4.6) + 125)
        local max = ((getPlayerLevel(master) * 0.3) + (getPlayerMagLevel(master) * 9.6) + 125)
        local health = math.random(min,max)
        doAreaCombatHealth(master, COMBAT_FIREDAMAGE, totempos, hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
        if party == nil or type(party) ~= "table" or #party <= 1 then
        if totempos.z ~= masterPosition.z or totempos:getDistance(masterPosition) < distFromMaster then
            doCreatureAddHealth(master, health)
            doSendMagicEffect(masterPosition, 12)
            end
            return true
        end
        local players = getSpectators(totempos, c.rangex, c.rangey, c.multifloor)
        for _, member in pairs(party) do
        doAreaCombatHealth(member, COMBAT_FIREDAMAGE, totempos, hitArea, 0, 0, CONST_ME_MAGIC_BLUE)
            if isInArray(players, member) then
                doCreatureAddHealth(member, health)
                doSendMagicEffect(getCreaturePosition(member), 12)
            end
        end
    end
end

local times = {1, 3, 5, 7, 9}

function onThink(creature)

    if not eventsFired[creature:getId()] then
        addEvent(function(cid)
            local creature = Creature(cid)
            if creature then
            creature:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONAREA)
                creature:remove()
            end
        end, 10000, creature:getId())
        for i = 1, #times do
            addEvent(genericFunction, times[i] * 1000, creature:getId())
        end
  
        eventsFired[creature:getId()] = true
    end
    return true
end
 
Back
Top