• 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 Script wont set charges on Item?

Extrodus

|| Blazera.net ||
Premium User
Joined
Dec 22, 2008
Messages
2,737
Solutions
7
Reaction score
541
Location
Canada
So I'm trying to give the player an Item with 1000 charges on it (fiery clerical mace) and it will give the item, but wont set the charges even with the script set up properly. No errors in console, it just gives a clerical mace with only 1 charge.

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid)

    if not chest then
        return true
    end

    local uniqueid = chest:getUniqueId()
    if player:getStorageValue(uniqueid) == -2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local reward = nil
    local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)

    for i = start, chest:getSize() do
        reward = chest:getItem(i)
        if not reward then
            break
        end

        if reward:getWeight() > player:getFreeCapacity() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
            player:setStorageValue(uniqueid, i)
            break
        else
            local reward_container = Container(reward:getUniqueId())
            if reward_container then
                reward_container = reward_container:clone()
                reward_container:moveTo(player)
            else
                player:addItem(reward:getId(), reward:getCount(), reward:getCharges()):setActionId(reward:getActionId())
            end
            local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
            if reward:getCount() > 1 then
                reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
            end

            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')


            player:setStorageValue(uniqueid, -2)
        end
    end

    return true
end

As you see "reward:getCharges()" is there, but it doesn't work for some reason. It's 2AM in the morning, I must just be looking at it wrong. If anyone see's the problem, I'd greatly appreciate the help!
 
This might be why your only receiving 1 charge
Code:
// player:addItem(itemId[, count = 1[, canDropOnMap = true[, subType = 1[, slot = CONST_SLOT_WHEREEVER]]]])

The arguments passed to the function must be in perfect alignment meaning if an apple was meant to be past you need to send the apple and not a banana or orange.

Unless the function is overloaded, meaning if the function is defined with the same name but different parameters.

Sending any number greater than 0 is essentially sending a true value.

So armed with the definition above, we can try adding false to the method addItem
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid)

    if not chest then
        return true
    end

    local uniqueid = chest:getUniqueId()
    if player:getStorageValue(uniqueid) == -2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local reward = nil
    local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)

    for i = start, chest:getSize() do
        reward = chest:getItem(i)
        if not reward then
            break
        end

        if reward:getWeight() > player:getFreeCapacity() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
            player:setStorageValue(uniqueid, i)
            break
        else
            local reward_container = Container(reward:getUniqueId())
            if reward_container then
                reward_container = reward_container:clone()
                reward_container:moveTo(player)
            else
                -- player:addItem(itemId[, count = 1[, canDropOnMap = true[, subType = 1[, slot = CONST_SLOT_WHEREEVER]]]])
                player:addItem(reward:getId(), reward:getCount(), false, reward:getCharges()):setActionId(reward:getActionId())
            end
            local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
            if reward:getCount() > 1 then
                reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
            end

            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')


            player:setStorageValue(uniqueid, -2)
        end
    end

    return true
end
 
Last edited:
Back
Top