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

Help! Scroll of Capacity TFS 1.X

snuckles

Banned User
Joined
Apr 4, 2009
Messages
74
Reaction score
3
Location
Hong Kong
Lua:
------------
-- Config --
------------

local config = {

amount = 500, -- Cap to add.
price = 500000 -- Price of it in gp.

}

------------
-- Script --
------------

function onUse(cid, item, fromPosition, itemEx, toPosition)

local playerpos = getCreaturePosition(cid)

function getPlayerMaxCap(cid)
local query = db.getResult("SELECT `cap` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. ";")
if query:getID() ~= -1 then
return query:getDataInt("cap")
end
query:free()
return LUA_ERROR
end

function AddCap()
doPlayerSetMaxCapacity(cid, (getPlayerMaxCap(cid) + config.amount))
db.executeQuery("UPDATE `players` SET `cap` = " .. (getPlayerMaxCap(cid) + config.amount) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
end

if getPlayerMoney(cid) < config.price then
doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
doSendMagicEffect(playerpos, CONST_ME_POFF)
else
doPlayerRemoveMoney(cid, config.price)
AddCap()
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " of capacity!")
doRemoveItem(item.uid, 1)
end
return TRUE
end
Post automatically merged:

Need help to fix this! transform to version TFS 1.X more
 
I think you are missing an XML line that registers this script. Could you post that?
Which itemid do you use for this scroll of capacity?

Edit: Presuming you want itemid 7724 (scroll).

Place in data/scripts/capacityscroll.lua
Lua:
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(cid, player)
    local playerMaxCap = false
    local resultId = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    if resultId ~= false then
        local playerMaxCap = query:getDataInt("cap")
        result.free(resultId)
    end
    if playerMaxCap ~= false then
        doPlayerSetMaxCapacity(cid, (playerMaxCap + config.amount))
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
        return true
    end
    return false
end

-- Scroll of capacity
local scrollOfCapacity = Action()
scrollOfCapacity:id(7724)

function scrollOfCapacity.onUse(player, item)
    local cid = player:getId()
    if getPlayerMoney(cid) < config.price then
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        doSendMagicEffect(player:getPosition(), CONST_ME_POFF)
    else
        doPlayerRemoveMoney(cid, config.price)
       
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " of capacity!")
        doRemoveItem(item.uid, 1)
    end
    return true
end
scrollOfCapacity:register()
 
Last edited:
I think you are missing an XML line that registers this script. Could you post that?
Which itemid do you use for this scroll of capacity?

Edit: Presuming you want itemid 7724 (scroll).

Place in data/scripts/capacityscroll.lua
Lua:
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(cid, player)
    local playerMaxCap = false
    local resultId = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    if resultId ~= false then
        local playerMaxCap = query:getDataInt("cap")
        result.free(resultId)
    end
    if playerMaxCap ~= false then
        doPlayerSetMaxCapacity(cid, (playerMaxCap + config.amount))
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
        return true
    end
    return false
end

-- Scroll of capacity
local scrollOfCapacity = Action()
scrollOfCapacity:id(7724)

function scrollOfCapacity.onUse(player, item)
    local cid = player:getId()
    if getPlayerMoney(cid) < config.price then
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        doSendMagicEffect(player:getPosition(), CONST_ME_POFF)
    else
        doPlayerRemoveMoney(cid, config.price)
      
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " of capacity!")
        doRemoveItem(item.uid, 1)
    end
    return true
end
scrollOfCapacity:register()
I use the itemid = 9004, but that is changeable, the problem was that previously I did not have any problem until I updated from 0.4 TFS to TFS 1.X and there the problem began, it only gave 5 capacity and removed the money and did not remove the object, now I will test it.
Post automatically merged:

notitle123.png

Not work
 
Last edited:
Can you show me please? Need help please!
XML:
<action itemid="7724" script="scroll_of_capacity.lua" />
Lua:
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(cid, player)
    local playerMaxCap = false
    local resultId = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    if resultId ~= false then
        local playerMaxCap = query:getDataInt("cap")
        result.free(resultId)
    end
    if playerMaxCap ~= false then
        doPlayerSetMaxCapacity(cid, (playerMaxCap + config.amount))
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
        return true
    end
    return false
end

function onUse(player, item)
    local cid = player:getId()
    if getPlayerMoney(cid) < config.price then
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        doSendMagicEffect(player:getPosition(), CONST_ME_POFF)
    else
        doPlayerRemoveMoney(cid, config.price)
       
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " of capacity!")
        doRemoveItem(item.uid, 1)
    end
    return true
end
 
XML:
<action itemid="7724" script="scroll_of_capacity.lua" />
Lua:
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(cid, player)
    local playerMaxCap = false
    local resultId = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    if resultId ~= false then
        local playerMaxCap = query:getDataInt("cap")
        result.free(resultId)
    end
    if playerMaxCap ~= false then
        doPlayerSetMaxCapacity(cid, (playerMaxCap + config.amount))
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
        return true
    end
    return false
end

function onUse(player, item)
    local cid = player:getId()
    if getPlayerMoney(cid) < config.price then
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        doSendMagicEffect(player:getPosition(), CONST_ME_POFF)
    else
        doPlayerRemoveMoney(cid, config.price)
      
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " of capacity!")
        doRemoveItem(item.uid, 1)
    end
    return true
end
Still bug... only gives 29 of capacity and show this message: You have received 500000 of capacity!
 
Still bug... only gives 29 of capacity and show this message: You have received 500000 of capacity!
Lua:
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(cid, player)
    local playerMaxCap = false
    local resultId = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    if resultId ~= false then
        local playerMaxCap = query:getDataInt("cap")
        result.free(resultId)
    end
    if playerMaxCap ~= false then
        doPlayerSetMaxCapacity(cid, (playerMaxCap + config.amount))
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. getPlayerGUID(cid) .. ";")
        return true
    end
    return false
end

function onUse(player, item)
    local cid = player:getId()
    if getPlayerMoney(cid) < config.price then
        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        doSendMagicEffect(player:getPosition(), CONST_ME_POFF)
    else
        doPlayerRemoveMoney(cid, config.price)
        doPlayerAddCap(cid, player)
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " of capacity!")
        doRemoveItem(item.uid, 1)
    end
    return true
end
 
you need to add database functions to your datapack.. goto data/lib create database.lua
add this
Lua:
db.updateQueryLimitOperator = db.updateLimiter
db.stringComparisonOperator = db.stringComparer
db.stringComparison = db.stringComparer
db.executeQuery = db.query
db.quote = db.escapeString

if(result == nil) then
    print("> WARNING: Couldn't load database lib.")
    return
end

Result = createClass(nil)
Result:setAttributes({
    id = -1,
    query = ""
})

function Result:getID()
    return self.id
end

function Result:setID(_id)
    self.id = _id
end

function Result:getQuery()
    return self.query
end

function Result:setQuery(_query)
    self.query = _query
end

function Result:create(_query)
    self:setQuery(_query)
    local _id = db.storeQuery(self:getQuery())
    if(_id) then
        self:setID(_id)
    end

    return self:getID()
end

function Result:getRows(free)
    local free = free or false
    if(self:getID() == -1) then
        error("[Result:getRows] Result not set!")
    end

    local c = 0
    repeat
        c = c + 1
    until not self:next()

    local _query = self:getQuery()
    self:free()
    if(not free) then
        self:create(_query)
    end

    return c
end

function Result:getDataInt(s)
    if(self:getID() == -1) then
        error("[Result:getDataInt] Result not set!")
    end

    return result.getDataInt(self:getID(), s)
end

function Result:getDataLong(s)
    if(self:getID() == -1) then
        error("[Result:getDataLong] Result not set!")
    end

    return result.getDataLong(self:getID(), s)
end

function Result:getDataString(s)
    if(self:getID() == -1) then
        error("[Result:getDataString] Result not set!")
    end

    return result.getDataString(self:getID(), s)
end

function Result:getDataStream(s)
    if(self:getID() == -1) then
        error("[Result:getDataStream] Result not set!")
    end

    return result.getDataStream(self:getID(), s)
end

function Result:next()
    if(self:getID() == -1) then
        error("[Result:next] Result not set!")
    end

    return result.next(self:getID())
end

function Result:free()
    if(self:getID() == -1) then
        error("[Result:free] Result not set!")
    end

    self:setQuery("")
    local ret = result.free(self:getID())
    self:setID(-1)
    return ret
end

Result.numRows = Result.getRows
function db.getResult(query)
    if(type(query) ~= 'string') then
        return nil
    end

    local ret = Result:new()
    ret:create(query)
    return ret
end
then goto data/lib/lib.lua and add this line at the end of the file
XML:
dofile('data/lib/database.lua')
the re test your script it should work now
 
Still bug this script...
try!
Lua:
------------
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(player)
    local playerMaxCap = 0
    local result = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. player:getId() .. ";")
    if result then
        playerMaxCap = result:getDataInt("cap")
        result:free()
    end
    if playerMaxCap then
        player:setCapacity(playerMaxCap + config.amount)
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. player:getId() .. ";")
        player:save()
        return true
    end
    return false
end


function onUse(player, item)
    if player:getMoney() < config.price then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    else
        player:removeMoney(config.price)
        doPlayerAddCap(player)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " oz of capacity!")
        item:remove(1)
    end
    return true
end
 
Last edited:
try!
Lua:
------------
-- Config --
------------
local config = {
    amount = 500 * 1000, -- Cap to add in oz
    price = 50 * 10000 -- Price in crystal coins
}

------------
-- Script --
------------

local function doPlayerAddCap(player)
    local playerMaxCap = 0
    local result = db.storeQuery("SELECT `cap` FROM `players` WHERE `id` = " .. player:getId() .. ";")
    if result then
        playerMaxCap = result:getDataInt("cap")
        result:free()
    end
    if playerMaxCap then
        player:setCapacity(playerMaxCap + config.amount)
        db.query("UPDATE `players` SET `cap` = " .. (playerMaxCap + config.amount) .. " WHERE `id` = " .. player:getId() .. ";")
        return true
    end
    return false
end


function onUse(player, item)
    local player = player:getId()
    local player = Player(player)
  
    if not player then
        return false
    end
  
    if player:getMoney() < config.price then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You don't have enough money to buy this. You need " .. config.price .. " gp.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    else
        player:removeMoney(config.price)
        doPlayerAddCap(player)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have received " .. config.amount .. " oz of capacity!")
        item:remove(1)
    end
    return true
end

There's no need to do the below, onUse passes the player object directly, it shouldn't ever be nil...
Lua:
local player = player:getId()
local player = Player(player)
   
if not player then
    return false
end

Other than that it's good.

but one question for the OP...
Is there any need to do database queries, or is this a purchasable store item where you want to ensure data is updated in the event of a crash?

Otherwise...just update the cap and let server saves do its job, no need for the db queries.
 
Back
Top