• 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 Addon Lever - rewrite to 1.X

OTcreator

Active Member
Joined
Feb 14, 2022
Messages
425
Solutions
1
Reaction score
44
Hello!
I need rewrite this to 1.X [1.4.2].
This is no full code.

I have problem with getItemCount.

Lua:
if (getPlayerStorageValue(cid, STORAGE1) == EMPTY_STORAGE) and getItemCountFromPosition(nameItem1, getItem1) == 100 and getItemCountFromPosition(nameItem2, getItem2) == 50 then
                    doRemoveItem(getTileItemById(getItem1,nameItem1).uid,100)
                    doRemoveItem(getTileItemById(getItem2,nameItem2).uid,50)
                    doPlayerAddOutfit(cid,CITIZEN,2)
                    doSendMagicEffect({x=1918,y=1232,z=5}, CONST_ME_HITBYFIRE)
                    setPlayerStorageValue(cid, STORAGE1, 1)
                    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have unlocked second citizen addon!")
 
Lua:
if (player:getStorageValue(STORAGE1) == EMPTY_STORAGE) and player:getItemCount(nameItem1) == 100 and player:getItemCount(nameItem2) == 50 then
    local item1 = getTileItemById({x = player:getPosition().x, y = player:getPosition().y, z = player:getPosition().z}, getItem1)
    local item2 = getTileItemById({x = player:getPosition().x, y = player:getPosition().y, z = player:getPosition().z}, getItem2)
    
    if item1 and item2 then
        item1:remove(100)
        item2:remove(50)
        
        player:addOutfitAddon(CITIZEN, 2)
        player:getPosition():sendMagicEffect(CONST_ME_HITBYFIRE)
        
        player:setStorageValue(STORAGE1, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have unlocked the second citizen addon!")
    end
end
 
Lua:
if (player:getStorageValue(STORAGE1) == EMPTY_STORAGE) and player:getItemCount(nameItem1) == 100 and player:getItemCount(nameItem2) == 50 then
    local item1 = getTileItemById({x = player:getPosition().x, y = player:getPosition().y, z = player:getPosition().z}, getItem1)
    local item2 = getTileItemById({x = player:getPosition().x, y = player:getPosition().y, z = player:getPosition().z}, getItem2)
   
    if item1 and item2 then
        item1:remove(100)
        item2:remove(50)
       
        player:addOutfitAddon(CITIZEN, 2)
        player:getPosition():sendMagicEffect(CONST_ME_HITBYFIRE)
       
        player:setStorageValue(STORAGE1, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have unlocked the second citizen addon!")
    end
end

It won't work.
The player puts the items on the position , pulls the lever and it is on the position to check if there are items in the given quantities and not if the player has them in BP.
 
So I put some, plus I added the simple script.

Lua:
local positionToCheck = {x = 1918, y = 1232, z = 5}

local item1 = getTileItemById(positionToCheck, getItem1)
local item2 = getTileItemById(positionToCheck, getItem2)

if item1 and item2 then
    local item1Count = item1:getCount()
    local item2Count = item2:getCount()
   
    if item1Count >= 100 and item2Count >= 50 then
        item1:remove(100)
        item2:remove(50)
       
        player:addOutfitAddon(CITIZEN, 2)
        positionToCheck:sendMagicEffect(CONST_ME_HITBYFIRE)
       
        player:setStorageValue(STORAGE1, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have unlocked the second citizen addon!")
    end
end
 
So I put some, plus I added the simple script.

Lua:
local positionToCheck = {x = 1918, y = 1232, z = 5}

local item1 = getTileItemById(positionToCheck, getItem1)
local item2 = getTileItemById(positionToCheck, getItem2)

if item1 and item2 then
    local item1Count = item1:getCount()
    local item2Count = item2:getCount()
  
    if item1Count >= 100 and item2Count >= 50 then
        item1:remove(100)
        item2:remove(50)
      
        player:addOutfitAddon(CITIZEN, 2)
        positionToCheck:sendMagicEffect(CONST_ME_HITBYFIRE)
      
        player:setStorageValue(STORAGE1, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have unlocked the second citizen addon!")
    end
end

Eh. Not working.
Can you rewrite this function and tell me how I can add this to 1.4.2 tfs?

Lua:
function getItemCountFromPosition(item, pos)
    local item_pos = {x = pos.x, y = pos.y, z = pos.z}
    local item_count = 0
    for i = 1, 255 do
        local check_pos = {x = item_pos.x, y = item_pos.y, z = item_pos.z, stackpos = i}
        if getThingFromPos(check_pos).itemid <= 0 then
            break
        elseif getThingFromPos(check_pos).itemid == item then
            if isItemStackable(getThingFromPos(check_pos).itemid) == true then
                item_count = item_count + getThingFromPos(check_pos).type
            else
                item_count = item_count + 1
            end
        end
    end
    return item_count
end
 
What is printed on the console? Did an error appear on the console? If so, post it here so we can understand what the error was.
I have a lot of similar lines with such code and I need to have it written practically 1:1 just change the code to 1.X.

ETC.:
getItemCountFromPosition(nameItem1, getItem1) == 100 and getItemCountFromPosition(nameItem2, getItem2) == 50 then
To 1.X:
getThing:Blabla():blalbla == 100 and getThing:Blabla():blalbla == 50 then
[TFS 1.x code]
 
Lua:
local positionToCheck = {x = 1918, y = 1232, z = 5}
local player = Player(cid)
local getItem1 = 12345  -- Replace with the correct ID of the first item
local getItem2 = 67890  -- Replace with the correct ID of the second item
local emptyStorage = 1001  -- Replace with the correct value of empty storage
local CITIZEN = 126  -- Replace with the correct outfit value for citizen
local CONST_ME_HITBYFIRE = 3

local item1 = getTileItemById(positionToCheck, getItem1)
local item2 = getTileItemById(positionToCheck, getItem2)

if player:getStorageValue(emptyStorage) == 0 and item1 and item2 then
    local item1Count = item1:getCount()
    local item2Count = item2:getCount()

    if item1Count >= 100 and item2Count >= 50 then
        item1:remove(100)
        item2:remove(50)

        player:addOutfitAddon(CITIZEN, 2)
        positionToCheck:sendMagicEffect(CONST_ME_HITBYFIRE)

        player:setStorageValue(emptyStorage, 1)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have unlocked the second citizen addon!")
    end
end

or

Lua:
local getPlayerItems = {x = 1918, y = 1232, z = 5}
local getItem1 = 12345  
local getItem2 = 67890  
local emptyStorage = 1001  
local CITIZEN = 126  
local CONST_ME_HITBYFIRE = 3  
local leverStorageValue = 2000  

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    
    if player:getStorageValue(leverStorageValue) ~= 1 then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need to pull the lever first..")
        return true
    end
    
    local item1Count = player:getItemCount(getItem1)
    local item2Count = player:getItemCount(getItem2)
    
    if item1Count >= 100 and item2Count >= 50 then
        player:removeItem(getItem1, 100)
        player:removeItem(getItem2, 50)
        
        player:addOutfitAddon(CITIZEN, 2)
        getPlayerItems:sendMagicEffect(CONST_ME_HITBYFIRE)
        
        player:setStorageValue(emptyStorage, 1)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have unlocked the second citizen addon!")
    else
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You do not have the necessary items.")
    end
    
    return true
end
 
Last edited:
Eh. Not working.
Can you rewrite this function and tell me how I can add this to 1.4.2 tfs?

Lua:
function getItemCountFromPosition(item, pos)
    local item_pos = {x = pos.x, y = pos.y, z = pos.z}
    local item_count = 0
    for i = 1, 255 do
        local check_pos = {x = item_pos.x, y = item_pos.y, z = item_pos.z, stackpos = i}
        if getThingFromPos(check_pos).itemid <= 0 then
            break
        elseif getThingFromPos(check_pos).itemid == item then
            if isItemStackable(getThingFromPos(check_pos).itemid) == true then
                item_count = item_count + getThingFromPos(check_pos).type
            else
                item_count = item_count + 1
            end
        end
    end
    return item_count
end

ret.items from this function should do the trick:
Lua:
function getTileInfo(position)
    local t = Tile(position)
    if not t then
        return false
    end

    local ret = pushThing(t:getGround())
    ret.protection = t:hasFlag(TILESTATE_PROTECTIONZONE)
    ret.nopz = ret.protection
    ret.nologout = t:hasFlag(TILESTATE_NOLOGOUT)
    ret.refresh = t:hasFlag(TILESTATE_REFRESH)
    ret.house = t:getHouse()
    ret.bed = t:hasFlag(TILESTATE_BED)
    ret.depot = t:hasFlag(TILESTATE_DEPOT)

    ret.things = t:getThingCount()
    ret.creatures = t:getCreatureCount()
    ret.items = t:getItemCount()
    ret.topItems = t:getTopItemCount()
    ret.downItems = t:getDownItemCount()
    return ret
end

(it's on compat.lua file)
 
ret.items from this function should do the trick:
Lua:
function getTileInfo(position)
    local t = Tile(position)
    if not t then
        return false
    end

    local ret = pushThing(t:getGround())
    ret.protection = t:hasFlag(TILESTATE_PROTECTIONZONE)
    ret.nopz = ret.protection
    ret.nologout = t:hasFlag(TILESTATE_NOLOGOUT)
    ret.refresh = t:hasFlag(TILESTATE_REFRESH)
    ret.house = t:getHouse()
    ret.bed = t:hasFlag(TILESTATE_BED)
    ret.depot = t:hasFlag(TILESTATE_DEPOT)

    ret.things = t:getThingCount()
    ret.creatures = t:getCreatureCount()
    ret.items = t:getItemCount()
    ret.topItems = t:getTopItemCount()
    ret.downItems = t:getDownItemCount()
    return ret
end

(it's on compat.lua file)

How to check this ID and count on one line?
Post automatically merged:

Lua:
local positionToCheck = {x = 1918, y = 1232, z = 5}
local player = Player(cid)
local getItem1 = 12345  -- Replace with the correct ID of the first item
local getItem2 = 67890  -- Replace with the correct ID of the second item
local emptyStorage = 1001  -- Replace with the correct value of empty storage
local CITIZEN = 126  -- Replace with the correct outfit value for citizen
local CONST_ME_HITBYFIRE = 3

local item1 = getTileItemById(positionToCheck, getItem1)
local item2 = getTileItemById(positionToCheck, getItem2)

if player:getStorageValue(emptyStorage) == 0 and item1 and item2 then
    local item1Count = item1:getCount()
    local item2Count = item2:getCount()

    if item1Count >= 100 and item2Count >= 50 then
        item1:remove(100)
        item2:remove(50)

        player:addOutfitAddon(CITIZEN, 2)
        positionToCheck:sendMagicEffect(CONST_ME_HITBYFIRE)

        player:setStorageValue(emptyStorage, 1)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have unlocked the second citizen addon!")
    end
end

or

Lua:
local getPlayerItems = {x = 1918, y = 1232, z = 5}
local getItem1 = 12345
local getItem2 = 67890
local emptyStorage = 1001
local CITIZEN = 126
local CONST_ME_HITBYFIRE = 3
local leverStorageValue = 2000

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
  
    if player:getStorageValue(leverStorageValue) ~= 1 then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need to pull the lever first..")
        return true
    end
  
    local item1Count = player:getItemCount(getItem1)
    local item2Count = player:getItemCount(getItem2)
  
    if item1Count >= 100 and item2Count >= 50 then
        player:removeItem(getItem1, 100)
        player:removeItem(getItem2, 50)
      
        player:addOutfitAddon(CITIZEN, 2)
        getPlayerItems:sendMagicEffect(CONST_ME_HITBYFIRE)
      
        player:setStorageValue(emptyStorage, 1)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have unlocked the second citizen addon!")
    else
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "You do not have the necessary items.")
    end
  
    return true
end

@Evil Puncker
I found this, but when don't have ItemID on tile , get error.
When i have 2 count, script working good, and get else message.
When have == ok qty , if working good and give me addon.
But when I dont have ItemID on tile, have error with nill value.


Tile(POS):getItemById(ITEMID):getCount()

Etc.
If Tile(POS):getItemById(2160):getCount() == 100 then (when not have item 2160 on tile = error).
Give addon
Else
Test message f*CK u.
End
Return true
End
 
Last edited:
Back
Top