• 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+ About summons code

GOD Half

Member
Joined
May 9, 2010
Messages
179
Reaction score
15
Hi again, I have developed a code to summon a Pet, but I don't understand much about how the "master" or "getCreatureMaster" system works ... I think I'm using it the wrong way. If anyone can explain to me the logic of this system, please. There have been some errors because of this, in the example below my Pet/Summon should say "WEEEEEEEEEEEEEE!", but if already have another one with the same name on the screen, the speaker is the other and not mine. If someone helps me, I did too the "summonName", which in this case would be to identify only this summon, since I intend to do others summons and if not identify who this is, the others will disappear together if I remove the item from the slot. Thank's

FVw3igJhRnCRIwso_sSn5Q.jpeg


Lua:
local configId = {
    [1] = {26382}
}

function doRemoveCreatureSummons(cid, name)
    if isCreature(cid) and type(name) == "string" then
    local c = getCreatureSummons(cid)
        if #c > 0 then
            for i, v in ipairs(c) do
                doRemoveCreature(v)
            end
        end
    end
return true
end

function onThink(interval, lastExecution, thinkInterval)
    for _, player in ipairs(Game.getPlayers()) do
        local nomePlayer = player:getName()
        local positionPlayer = player:getPosition()
        local summonPlayer = player:getSummons()
        local master = getCreatureMaster(nomePlayer)
        if master then
            for _, i in pairs(configId) do
                if type(i) == 'table' then
                    NomePet = getItemName(i[1])
                    if (#summonPlayer > 0) then
                        for s = 1, #summonPlayer do
                            summonName = summonPlayer[s]:getName()
                            summonPos = summonPlayer[s]:getPosition()
                        end
                    else
                        summonPos = nil
                    end
                    if isInArray(i, getPlayerSlotItem(nomePlayer, CONST_SLOT_AMMO).itemid) then
                        if (player:getStorageValue(storage00005) ~= 1) and ((summonName ~= NomePet) or (#summonPlayer == 0)) then
                        player:setStorageValue(storage00005, 1)
                        player:setStorageValue(storage00006, 0)
                            local PetName = doForceSummonCreature(NomePet, positionPlayer)
                            doConvinceCreature(player, PetName)
                            doCreatureSay(summonName, "WEEEEEEEEEEEE!", TALKTYPE_ORANGE_1)
                        end
                    else
                        if (summonName == NomePet) and (player:getStorageValue(storage00006) ~= 1) then
                        player:setStorageValue(storage00005, 0)
                        player:setStorageValue(storage00006, 1)
                            doSendMagicEffect(summonPos, CONST_ME_POFF)
                            doRemoveCreatureSummons(player, NomePet)
                        end
                    end
                end
            end
        end
    end
return true
end
 
The master is the owner of the creature, If you not have master so the function return nil
"The function getCreatureMaster(cid) you have to send him a parameter CID, not a name."

Example:
Code:
local master = self:getMaster()
or
local master = getCreatureMaster(self:getId())

Here the code compat.lua
Code:
function getCreatureMaster(cid)
    local c = Creature(cid)
    if c ~= nil then
        local master = c:getMaster()
        return master ~= nil and master:getId() or c:getId()
    end
    return false
end
in this case if the CID not is any creature return false and if CID not have master so return ID of self,,,, if CID have master so return the master ID
 
The master is the owner of the creature, If you not have master so the function return nil
"The function getCreatureMaster(cid) you have to send him a parameter CID, not a name."

Example:
Code:
local master = self:getMaster()
or
local master = getCreatureMaster(self:getId())

Here the code compat.lua
Code:
function getCreatureMaster(cid)
    local c = Creature(cid)
    if c ~= nil then
        local master = c:getMaster()
        return master ~= nil and master:getId() or c:getId()
    end
    return false
end
in this case if the CID not is any creature return false and if CID not have master so return ID of self,,,, if CID have master so return the master ID

Hi Sarah Wesker, thanks for the explanation, but I trying this inside an onThink(), and I don't know if that's why, but I'm trying a bug:
Lua:
Lua Script Error: [GlobalEvent Interface]
data/globalevents/scripts/check_pet.lua:onThink
data/globalevents/scripts/check_pet.lua:24: attempt to index global 'self' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/globalevents/scripts/check_pet.lua:24: in function <data/globalevents/scripts/check_pet.lua:17>
[Error - GlobalEvents::think] Failed to execute event: checkPet
 
I tried, tried, tried and tried and solved. Thank's!
Lua:
local configId = {
    [1] = {26382}
}

function doRemoveCreatureSummons(cid, name)
    if isCreature(cid) and type(name) == "string" then
    local c = getCreatureSummons(cid)
        if #c > 0 then
            for i, v in ipairs(c) do
                doRemoveCreature(v)
            end
        end
    end
return true
end

function onThink(interval, lastExecution, thinkInterval)
    for _, player in ipairs(Game.getPlayers()) do
        local nomePlayer = player:getName()
        local positionPlayer = player:getPosition()
        local summonPlayer = player:getSummons()
        local master = getCreatureMaster(nomePlayer)
        if master then
            for _, i in pairs(configId) do
                if type(i) == 'table' then
                    NomePet = getItemName(i[1])
                    summonName = "no summon summoned"
                    if (#summonPlayer > 0) then
                        for s = 1, #summonPlayer do
                            summonName = summonPlayer[s]:getName()
                            summonPos = summonPlayer[s]:getPosition()
                        end
                    else
                        summonPos = nil
                    end
                    if isInArray(i, getPlayerSlotItem(nomePlayer, CONST_SLOT_AMMO).itemid) then
                        if (player:getStorageValue(storage00005) ~= 1) and ((summonName ~= NomePet) or (#summonPlayer == 0)) then
                        player:setStorageValue(storage00005, 1)
                        player:setStorageValue(storage00006, 0)
                            local PetName = doForceSummonCreature(NomePet, positionPlayer)
                            doCreatureSay(PetName, "WEEEEEEEEEEEE!", TALKTYPE_ORANGE_1)
                            doConvinceCreature(player, PetName)
                        end
                    else
                        if (summonName == NomePet) and (#summonPlayer ~= 0) and (player:getStorageValue(storage00006) ~= 1) then
                        player:setStorageValue(storage00005, 0)
                        player:setStorageValue(storage00006, 1)
                            doSendMagicEffect(summonPos, CONST_ME_POFF)
                            doRemoveCreatureSummons(player, NomePet)
                        end
                    end
                end
            end
        end
    end
return true
end
 
Back
Top