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

RevScripts TFS[1.3] action revscript lever shop(cant get it to work)

jadix1

New Member
Joined
Aug 7, 2020
Messages
3
Reaction score
0
i just felt the need to say that this is my very first attempt at writing something.

so i made an attempt to make a shop that uses actionids on levers, but I just cannot get it to work and I cannot see any errors. what am I missing?

Lua:
local shop = {
    [2051] = {id = 12325, cost = 2, count = 1},
    [2052] = {id = 8300, cost = 3, count = 1},
    [2053] = {id = 12662, cost = 5, count = 1},
}

local levershop = Action()
function levershop.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local config = {
    shopitem = shop[item.aid],
    itemType = ItemType(ShopItem.id),
    token = item.itemid(6527)
    }
 
    if item.itemid == 1945 or 1946 then
        if(not doPlayerRemoveItem(cid, token, shopitem.cost)) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '..ShopItem.cost..' to buy '.. itemType:getName() .. '.')
    return false
    else doPlayerRemoveItem(cid, token, shopitem.cost) then
                player:addItem(ShopItem.id, ShopItem.count)
                doPlayerRemoveItem(cid, token, ShopItem.cost)
                player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. itemType:getName() .. '.')
                player:getPosition():sendMagicEffect(13)
            end
        end
    end
 
levershop:aid(shop)
levershop:register()
 
Last edited:
I see several things;
change
Lua:
shopitem = shop[item.aid],
to
Lua:
shopItem = shop[item:getActionId()],
reference:



The following line will not work as you expect for two reasons
itemType = ItemType(ShopItem.id),

a) ShopItem is not the same thing as shopitem, each letter has to be exact.
b) table key shopitem is not recognized before the table itself is constructed and therefore it will throw an error:
attempt to perform arithmetic on global 'shopitem' (a nil value)
You can test it on repl.it/languages/lua
Lua:
local cfg = {}
-- setting key-value pairs after table is constructed.
cfg.a = 1
cfg.b = cfg.a + 1
print(cfg.b)

a solution would be to set key-value pairs after the table is constructed like so:
Lua:
local config = {}
and then setting whatever key-value pair you need to set.



This should simply be token = 6527.
Lua:
token = item.itemid(6527)


change
Lua:
if item.itemid == 1945 or 1946 then
to
Lua:
if (item:getId() == 1945) or (item:getId() == 1946) then



doSomethingMethod is the old way. You should practice the new way.
change
Lua:
if(not doPlayerRemoveItem(cid, token, shopitem.cost)) then
to:
Lua:
if not player:removeItem(config.token, config.shopItem.cost) then


change
Lua:
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '..ShopItem.cost..' to buy '.. itemType:getName() .. '.')
to
Lua:
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '.. config.shopItem.cost ..' to buy '.. config.itemType:getName() .. '.')



Replace
Lua:
        else doPlayerRemoveItem(cid, token, shopitem.cost) then
            player:addItem(ShopItem.id, ShopItem.count)
            doPlayerRemoveItem(cid, token, ShopItem.cost)
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. itemType:getName() .. '.')
            player:getPosition():sendMagicEffect(13)
        end
with

Lua:
        end
        player:addItem(config.shopItem.id, config.shopItem.count)
        doPlayerRemoveItem(cid, config.token, config.shopItem.cost)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. config.itemType:getName() .. '.')
        player:getPosition():sendMagicEffect(13)

and remove doPlayerRemoveItem(cid, token, ShopItem.cost) which is called after player:addItem

..so it looks like
Lua:
    if (item:getId() == 1945) or (item:getId() == 1946) then
        if not player:removeItem(config.token, config.shopitem.cost) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '..ShopItem.cost..' to buy '.. itemType:getName() .. '.')
            return false
        end

        player:addItem(config.shopItem.id, config.shopItem.count)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. config.itemType:getName() .. '.')
        player:getPosition():sendMagicEffect(13)
    end

the reason for this change is because you are already attempting to remove token from the player
if not player:removeItem(config.token, config.shopitem.cost) then
if removing token fails, the code inside that if-block will be executed and the player will receive "You need ...blabla" msg, and then the code will stop because return false - which stops code execution and exits the function with provided value false.



This will not work because shop is a table
Lua:
levershop:aid(shop)

.. action:aid() takes either 1 or multiple parameters like so:
Lua:
levershop:aid(2051) -- only 1 parameter
-- OR
levershop:aid(2051, 2052, 2053) -- multiple parameters



final:
Lua:
local shop = {
    [2051] = {id = 12325, cost = 2, count = 1},
    [2052] = {id = 8300, cost = 3, count = 1},
    [2053] = {id = 12662, cost = 5, count = 1},
}

local levershop = Action()
function levershop.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local config = {}

    config.shopItem = shop[item:getActionId()],
    config.itemType = ItemType(config.shopItem.id)
    config.token    = 6527

    if (item:getId() == 1945) or (item:getId() == 1946) then
        if not player:removeItem(config.token, config.shopItem.cost) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '.. config.shopItem.cost ..' to buy '.. config.itemType:getName() .. '.')
            return false
        end

        player:addItem(config.shopItem.id, config.shopItem.count)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. itemType:getName() .. '.')
        player:getPosition():sendMagicEffect(13)
    end
end

levershop:aid(2051, 2052, 2053)
levershop:register()
 
I see several things;
change
Lua:
shopitem = shop[item.aid],
to
Lua:
shopItem = shop[item:getActionId()],
reference:



The following line will not work as you expect for two reasons
itemType = ItemType(ShopItem.id),

a) ShopItem is not the same thing as shopitem, each letter has to be exact.
b) table key shopitem is not recognized before the table itself is constructed and therefore it will throw an error:
attempt to perform arithmetic on global 'shopitem' (a nil value)
You can test it on repl.it/languages/lua
Lua:
local cfg = {}
-- setting key-value pairs after table is constructed.
cfg.a = 1
cfg.b = cfg.a + 1
print(cfg.b)

a solution would be to set key-value pairs after the table is constructed like so:
Lua:
local config = {}
and then setting whatever key-value pair you need to set.



This should simply be token = 6527.
Lua:
token = item.itemid(6527)


change
Lua:
if item.itemid == 1945 or 1946 then
to
Lua:
if (item:getId() == 1945) or (item:getId() == 1946) then



doSomethingMethod is the old way. You should practice the new way.
change
Lua:
if(not doPlayerRemoveItem(cid, token, shopitem.cost)) then
to:
Lua:
if not player:removeItem(config.token, config.shopItem.cost) then


change
Lua:
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '..ShopItem.cost..' to buy '.. itemType:getName() .. '.')
to
Lua:
player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '.. config.shopItem.cost ..' to buy '.. config.itemType:getName() .. '.')



Replace
Lua:
        else doPlayerRemoveItem(cid, token, shopitem.cost) then
            player:addItem(ShopItem.id, ShopItem.count)
            doPlayerRemoveItem(cid, token, ShopItem.cost)
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. itemType:getName() .. '.')
            player:getPosition():sendMagicEffect(13)
        end
with

Lua:
        end
        player:addItem(config.shopItem.id, config.shopItem.count)
        doPlayerRemoveItem(cid, config.token, config.shopItem.cost)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. config.itemType:getName() .. '.')
        player:getPosition():sendMagicEffect(13)

and remove doPlayerRemoveItem(cid, token, ShopItem.cost) which is called after player:addItem

..so it looks like
Lua:
    if (item:getId() == 1945) or (item:getId() == 1946) then
        if not player:removeItem(config.token, config.shopitem.cost) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '..ShopItem.cost..' to buy '.. itemType:getName() .. '.')
            return false
        end

        player:addItem(config.shopItem.id, config.shopItem.count)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. config.itemType:getName() .. '.')
        player:getPosition():sendMagicEffect(13)
    end

the reason for this change is because you are already attempting to remove token from the player
if not player:removeItem(config.token, config.shopitem.cost) then
if removing token fails, the code inside that if-block will be executed and the player will receive "You need ...blabla" msg, and then the code will stop because return false - which stops code execution and exits the function with provided value false.



This will not work because shop is a table
Lua:
levershop:aid(shop)

.. action:aid() takes either 1 or multiple parameters like so:
Lua:
levershop:aid(2051) -- only 1 parameter
-- OR
levershop:aid(2051, 2052, 2053) -- multiple parameters



final:
Lua:
local shop = {
    [2051] = {id = 12325, cost = 2, count = 1},
    [2052] = {id = 8300, cost = 3, count = 1},
    [2053] = {id = 12662, cost = 5, count = 1},
}

local levershop = Action()
function levershop.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local config = {}

    config.shopItem = shop[item:getActionId()],
    config.itemType = ItemType(config.shopItem.id)
    config.token    = 6527

    if (item:getId() == 1945) or (item:getId() == 1946) then
        if not player:removeItem(config.token, config.shopItem.cost) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You need '.. config.shopItem.cost ..' to buy '.. config.itemType:getName() .. '.')
            return false
        end

        player:addItem(config.shopItem.id, config.shopItem.count)
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You bought a '.. itemType:getName() .. '.')
        player:getPosition():sendMagicEffect(13)
    end
end

levershop:aid(2051, 2052, 2053)
levershop:register()
holy shit, i cannot thank you enough! thought it was just one silly mistake or something in there, guess I got a lot to learn!
 
Back
Top