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

Lua If Empty Slot then do (something)

athenso

Average Coder
Joined
May 31, 2011
Messages
155
Solutions
3
Reaction score
23
Script works, until you remove the quiver(s). So yes it does "work" but I want it to work 100%. How do I check if the CONST_SLOT_AMMO is empty? So I can throw up a cancel message saying something like "You need a quiver equipped to cast this spell"?

Lua:
function onCastSpell(creature, variant)
local quiver = creature:getItemById(13260)
local bluequiver = creature:getItemById(13262)
local redquiver = creature:getItemById(13259)

        if creature:getSlotItem(CONST_SLOT_AMMO):getId() == 13260  then
            return quiver:addItem(2544, 15)
        elseif creature:getSlotItem(CONST_SLOT_AMMO):getId() == 13262 then
            return bluequiver:addItem(2544, 20)
        elseif creature:getSlotItem(CONST_SLOT_AMMO):getId() == 13259 then
            return redquiver:addItem(2544, 30)
        end
end
 
Solution
Lua:
local quiver = {
    [13260] = {item = 2544, amount = 15},
    [13262] = {item = 2544, amount = 20},
    [13259] = {item = 2544, amount = 30},
}

function onCastSpell(creature, variant, isHotkey)
    local ammoSlot = creature:getSlotItem(CONST_SLOT_AMMO)
    if not ammoSlot then
        creature:sendCancelMessage("You need a quiver equipped to cast this spell.")
        return false
    end

    for k, v in pairs(quiver) do
        if ammoSlot:getId() == k then
            ammoSlot:addItem(v["item"], v["amount"])
            creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
        end
    end

    return combat:execute(creature, variant)
end

incase you want an option
Works, but throws a combat error. Combat is not defined...
I quickly edited your script to make it work like you want, I didn't test it, so tell me if you find any problems...
you can edit lines 11 and 19 to make whatever you want

Lua:
local quivers = {
  [13260] = {amount=15},
  [13262] = {amount=20},
  [13259] = {amount=30}
}

function onCastSpell(creature, variant)
  local eqpdQuiver = creature:getSlotItem(CONST_SLOT_AMMO)

  if not eqpdQuiver then
    -- player arrow slot is empty
    return false
  end

  if quivers[eqpdQuiver:getId()].amount ~= nil then
    eqpdQuiver:addItem(2544, quivers[eqpdQuiver:getId()].amount)
    return true
  else
    -- player item on arrow slot isn't on the quivers list
    return false
  end
end
 
Lua:
local quiver = {
    [13260] = {item = 2544, amount = 15},
    [13262] = {item = 2544, amount = 20},
    [13259] = {item = 2544, amount = 30},
}

local combat = Combat()

function onCastSpell(creature, variant, isHotkey)
    local ammoSlot = creature:getSlotItem(CONST_SLOT_AMMO)
    if not ammoSlot then
        creature:sendCancelMessage("You need a quiver equipped to cast this spell.")
        return false
    end

    for k, v in pairs(quiver) do
        if ammoSlot:getId() == k then
            ammoSlot:addItem(v["item"], v["amount"])
            creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
        end
    end

    return combat:execute(creature, variant)
end

incase you want an option
 
Last edited:
Lua:
local quiver = {
    [13260] = {item = 2544, amount = 15},
    [13262] = {item = 2544, amount = 20},
    [13259] = {item = 2544, amount = 30},
}

function onCastSpell(creature, variant, isHotkey)
    local ammoSlot = creature:getSlotItem(CONST_SLOT_AMMO)
    if not ammoSlot then
        creature:sendCancelMessage("You need a quiver equipped to cast this spell.")
        return false
    end

    for k, v in pairs(quiver) do
        if ammoSlot:getId() == k then
            ammoSlot:addItem(v["item"], v["amount"])
            creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
        end
    end

    return combat:execute(creature, variant)
end

incase you want an option
Works, but throws a combat error. Combat is not defined
Lua:
Lua Script Error: [Spell Interface]
data/spells/scripts/conjuring/conjure_arrow.lua:onCastSpell
data/spells/scripts/conjuring/conjure_arrow.lua:21: attempt to index global 'combat' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/spells/scripts/conjuring/conjure_arrow.lua:21: in function <data/spells/scripts/conjuring/conjure_arrow.lua:7>

Really was not expecting anyone to do the script lol. I enjoyed the struggle to get it to where I had it. Appreciate it though.
Post automatically merged:

Lua:
local quiver = {
    [13260] = {item = 2544, amount = 15},
    [13262] = {item = 2544, amount = 20},
    [13259] = {item = 2544, amount = 30},
}

local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)

function onCastSpell(creature, variant, isHotkey)
    local ammoSlot = creature:getSlotItem(CONST_SLOT_AMMO)
    if not ammoSlot then
        creature:sendCancelMessage("You need a quiver equipped to cast this spell.")
        return false
    end

    for k, v in pairs(quiver) do
        if ammoSlot:getId() == k then
            ammoSlot:addItem(v["item"], v["amount"])
        end
    end

    return combat:execute(creature, variant)
end
Threw a combat error, I defined combat and threw the const inside it. Much appreciated. Posting code for anyone who might want to use it. I was not expecting anyone to clean up my code and write in the check for me. Greatly appreciated
 
Last edited:
Solution
Works, but throws a combat error. Combat is not defined
Lua:
Lua Script Error: [Spell Interface]
data/spells/scripts/conjuring/conjure_arrow.lua:onCastSpell
data/spells/scripts/conjuring/conjure_arrow.lua:21: attempt to index global 'combat' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/spells/scripts/conjuring/conjure_arrow.lua:21: in function <data/spells/scripts/conjuring/conjure_arrow.lua:7>

Really was not expecting anyone to do the script lol. I enjoyed the struggle to get it to where I had it. Appreciate it though.
alright let me explain your "problem" so you can keep your own script...
by using
Lua:
local quiver = creature:getItemById(13260)
you're getting any item (not only equiped one) to add your arrow

you should get the equiped quiver using
Lua:
creature:getSlotItem(CONST_SLOT_AMMO)

and then compare the id and add the item to it, this will prevent to add items to the "wrong" quiver

rewriting your own script, it will look like this...

Lua:
function onCastSpell(creature, variant)
  local quiver = creature:getSlotItem(CONST_SLOT_AMMO)

  if not quiver then
    return false
  end

  if quiver:getId() == 13260  then
    return quiver:addItem(2544, 15)
  elseif quiver:getId() == 13262 then
    return quiver:addItem(2544, 20)
  elseif quiver:getId() == 13259 then
    return quiver:addItem(2544, 30)
  end
end
 
alright let me explain your "problem" so you can keep your own script...
by using
Lua:
local quiver = creature:getItemById(13260)
you're getting any item (not only equiped one) to add your arrow

you should get the equiped quiver using
Lua:
creature:getSlotItem(CONST_SLOT_AMMO)

and then compare the id and add the item to it, this will prevent to add items to the "wrong" quiver

rewriting your own script, it will look like this...

Lua:
function onCastSpell(creature, variant)
  local quiver = creature:getSlotItem(CONST_SLOT_AMMO)

  if not quiver then
    return false
  end

  if quiver:getId() == 13260  then
    return quiver:addItem(2544, 15)
  elseif quiver:getId() == 13262 then
    return quiver:addItem(2544, 20)
  elseif quiver:getId() == 13259 then
    return quiver:addItem(2544, 30)
  end
end
I like the tables better. I am just getting back into OTS (I wasnt that great to begin with). It was just an idea I was tinkering with.
 
Back
Top