• 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 Check to see if a player has said item in inventory

noshirtstan

New Member
Joined
Aug 2, 2020
Messages
68
Reaction score
2
Hey folks, I'm trying to get my scrip to check if the player has a certain item before doing an action, but it doesn't seem to want to work.

I'm using TFS 1.3.

Here is what I have so far.
Lua:
if player:getItemById(2553) then -- check for pick
            mathCheck = math.floor(math.random() * 100) + 1
            if mathCheck >= 75 then
                player:addItem(26410, math.floor(math.random() * 3) + 1)
            elseif mathCheck <= 74 then
                player:addItem(26409, math.floor(math.random() * 3) + 1)
            end
            else
                return player:sendCancelMessage("You need a pick to use this!")
                end
            end
            player:setStorageValue(205000, os.time() + 1)

Any thoughts or help would be greatly appreciated.
 
Solution
E
That part of the script is working now that i changed the player:getItemById to player:getItemCount.

It's just not registering if the item is in the backpack/bag.
if player:getItemCount(2553) > 1 then -- check for pick

you are checking if user has more than 1, you need to check if he has more than 0 (or equal/greater)

if player:getItemCount(2553) >= 1 then -- check for pick
Hey folks, I'm trying to get my scrip to check if the player has a certain item before doing an action, but it doesn't seem to want to work.

I'm using TFS 1.3.

Here is what I have so far.
Lua:
if player:getItemById(2553) then -- check for pick
            mathCheck = math.floor(math.random() * 100) + 1
            if mathCheck >= 75 then
                player:addItem(26410, math.floor(math.random() * 3) + 1)
            elseif mathCheck <= 74 then
                player:addItem(26409, math.floor(math.random() * 3) + 1)
            end
            else
                return player:sendCancelMessage("You need a pick to use this!")
                end
            end
            player:setStorageValue(205000, os.time() + 1)

Any thoughts or help would be greatly appreciated.
if player:getItemCount(2553) > 1 then
 
Lua:
function onUse(player, item, fromPosition, itemEx, toPosition)
    if item.itemid == 26405 and isInArray({50605, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
      
        if player:isGatheringJob(item.actionid) then
      
            if player:getStorageValue(205000) > os.time() then
                player:sendCancelMessage("You must wait before mining this again!")
                return true
            end
            if player:getItemCount(2553) > 1 then -- check for pick
            mathCheck = math.floor(math.random() * 100) + 1
            if mathCheck >= 75 then
                player:addItem(26410, math.floor(math.random() * 3) + 1)
            elseif mathCheck <= 74 then
                player:addItem(26409, math.floor(math.random() * 3) + 1)
            end
            else
                return player:sendCancelMessage("You need a pick to use this!")
                end
            end
            player:setStorageValue(205000, os.time() + 1)
          
    elseif item.itemid == 26406 and isInArray({50601, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
      
    elseif item.itemid == 26407 and isInArray({50601, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
  
    elseif item.itemid == 26408 and isInArray({50601, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
      
    elseif item.itemid == 26404 and isInArray({    50601, 50602, 50603, 50604}, item.actionid) then
        return player:sendCancelMessage("This vein is exhausted!")
  
    elseif item.itemid == 2217 and item.actionid >= craftingGatheringJobConfig.baseRecipeStorage then
        if player:getStorageValue(item.actionid) == -1 then
            player:sendTextMessage(MESSAGE_EVENT_DEFAULT, "You have learned "..craftingGatheringJobConfig[item.actionid].skillName..", then the book burned to ashes after learning it's secrets.")
            player:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONHIT)
            player:setStorageValue(item.actionid, 10)
            Item(item.uid):remove(1)
        else
            player:sendCancelMessage("You already know "..craftingGatheringJobConfig[item.actionid].skillName..".")
        end
    end
end

There are two extra lines at the top that are empty, so line 1 is actually line 3. My bad.

Still doesnt work either way though.
 
Lua:
function onUse(player, item, fromPosition, itemEx, toPosition)
    if item.itemid == 26405 and isInArray({50605, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
      
        if player:isGatheringJob(item.actionid) then
      
            if player:getStorageValue(205000) > os.time() then
                player:sendCancelMessage("You must wait before mining this again!")
                return true
            end
            if player:getItemCount(2553) > 1 then -- check for pick
            mathCheck = math.floor(math.random() * 100) + 1
            if mathCheck >= 75 then
                player:addItem(26410, math.floor(math.random() * 3) + 1)
            elseif mathCheck <= 74 then
                player:addItem(26409, math.floor(math.random() * 3) + 1)
            end
            else
                return player:sendCancelMessage("You need a pick to use this!")
                end
            end
            player:setStorageValue(205000, os.time() + 1)
          
    elseif item.itemid == 26406 and isInArray({50601, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
      
    elseif item.itemid == 26407 and isInArray({50601, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
  
    elseif item.itemid == 26408 and isInArray({50601, 50602, 50603, 50604}, item.actionid) then
        if player:getCondition(player, CONDITION_COOLDOWN, 160) then
            return player:sendCancelMessage("You are already gathering!")
        end
      
        if not player:isGatheringJob(item.actionid) then
            return player:sendCancelMessage("You must learn "..craftingGatheringJobConfig[item.actionid].skillName.." before using this.")
        end
      
    elseif item.itemid == 26404 and isInArray({    50601, 50602, 50603, 50604}, item.actionid) then
        return player:sendCancelMessage("This vein is exhausted!")
  
    elseif item.itemid == 2217 and item.actionid >= craftingGatheringJobConfig.baseRecipeStorage then
        if player:getStorageValue(item.actionid) == -1 then
            player:sendTextMessage(MESSAGE_EVENT_DEFAULT, "You have learned "..craftingGatheringJobConfig[item.actionid].skillName..", then the book burned to ashes after learning it's secrets.")
            player:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONHIT)
            player:setStorageValue(item.actionid, 10)
            Item(item.uid):remove(1)
        else
            player:sendCancelMessage("You already know "..craftingGatheringJobConfig[item.actionid].skillName..".")
        end
    end
end

There are two extra lines at the top that are empty, so line 1 is actually line 3. My bad

This is the problem line.
Lua:
mathCheck = math.floor(math.random() * 100) + 1
math.random() requires a parameter.

math.random(100) for example, would provide a pseudo-random number from between 1-100.
math.random(50, 100) would provide a pseudo-random number from between 50-100.

You have math.random() through-out theupper code without any parameters, currently.
Hence, the nil value when you try to compare it.
 
That part of the script is working now that i changed the player:getItemById to player:getItemCount.

It's just not registering if the item is in the backpack/bag.
Post automatically merged:

I commented out the check for the item and the code runs perfect.
 
That part of the script is working now that i changed the player:getItemById to player:getItemCount.

It's just not registering if the item is in the backpack/bag.
if player:getItemCount(2553) > 1 then -- check for pick

you are checking if user has more than 1, you need to check if he has more than 0 (or equal/greater)

if player:getItemCount(2553) >= 1 then -- check for pick
 
Solution
Back
Top