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

TFS 1.X+ Item what can be used only by player what get it.

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
46
Its possible? Im thinking about exp items, what can be got from x npc for something, but i want add "permission" to use it only for player what collected it (tfs 1.5 8.6)
 
Last edited:
Solution
Somehow i can't edit my post, i did a mistake, here is a fix

Lua:
UNIQUE_CUSTOM_ATTRIBUTE = "CUSTOM_UNIQUE_USAGE"

function Player.canUseUniqueItem(self, item)
 
    if not item then
       return nil
    end
 
    local attr = item:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr and attr == self:getGuid() then
       return true
    end
    return false
 
end

function Player.setUniqueItemUsage(self, item)
 
    if not item then
       return nil
    end
 
    return item:setCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE, self:getGuid())
end

function Item.getUniqueUsage(self)
 
    local attr = self:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr then
       return attr
    end
 
    return false
end


---example script for...
woah, I'm so satisfied now, having it completed. Did a lot of testing, looks a little bad atm, but it works so I publish.
Both luas go to actions/scripts


this was for the chest giving the exp scroll (26434 is ID of the scroll, you will probably have to change it to your own scroll)
ChestGivingScrollOnUse.lua
Lua:
-- Made by Tofame
-- Gives item that is named with a player's name.
local chestStorage = 1430

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
if player:getStorageValue(chestStorage) > 1 then
return player:sendCancelMessage("You have already opened this chest!")
end
    local name = player:getName()
    local itemExp = player:addItem(26434, 1)
itemExp:setActionId(998)
itemExp:setAttribute("name", "" .. player:getName() .." 's Exp Scroll")
player:setStorageValue(chestStorage, 2)
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have obtained an exp scroll that only you can use!")
return
end

PersonalExpScroll.lua
Lua:
-- Made by Tofame
-- Allows for item usage only for the owner!

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.actionid == 998 then
        local itemName = item:getName()
        if itemName == ("" .. player:getName() .." 's Exp Scroll") then
            player:addExperience(250000)
            item:remove(1)
        else
            return player:sendCancelMessage("This scroll exp is not yours!")
        end
    else
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "ok") -- this shouldnt be shown,
        -- if it shows it means scroll wont work
    end
end

actions.xml
XML:
<action actionid="998" script="PersonalExpScroll.lua" />
<action actionid="997" script="ChestGivingScrollOnUse.lua" />
 
Last edited:
If you are using modern TFS you may use a custom attribute in the item which would save the owner player guid and when the item is used you can check if the saved player guid is the same as the using player guid.
 
Lua:
UNIQUE_CUSTOM_ATTRIBUTE = "CUSTOM_UNIQUE_USAGE"

function Player.canUseUniqueItem(item)
  
    if not item then
       return nil
    end
  
    local attr = item:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr and attr == self:getGuid() then
       return true
    end
    return false
  
end

function Player.setUniqueItemUsage(item)
  
    if not item then
       return nil
    end
  
    return item:setCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE, self:getGuid())
end

function Item.getUniqueUsage()
  
    local attr = self:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr then
       return attr
    end
  
    return false
end


---example script for scroll use check
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if item:getUniqueUsage() then
       if not player:canUseUniqueItem(item) then
          player:sendCancelMessage("You are not allowed to use this item.")
          return true
       end
    
       player:addExperience(666)
       item:remove(1)
       player:sendCancelMessage("blabla congratulations.")
    end
  
    return true
end

---example to obtain unique item
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:sendCancelMessage("Congratulations, you have found unique scroll that only you can use it.")
    local reward = player:addItem(6666)
    player:setUniqueItemUsage(reward)
    return true
end
 
Last edited:
thanks, I will test it on the weekend when I get home, because currently I have no way to check it in the truck
 
Somehow i can't edit my post, i did a mistake, here is a fix

Lua:
UNIQUE_CUSTOM_ATTRIBUTE = "CUSTOM_UNIQUE_USAGE"

function Player.canUseUniqueItem(self, item)
 
    if not item then
       return nil
    end
 
    local attr = item:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr and attr == self:getGuid() then
       return true
    end
    return false
 
end

function Player.setUniqueItemUsage(self, item)
 
    if not item then
       return nil
    end
 
    return item:setCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE, self:getGuid())
end

function Item.getUniqueUsage(self)
 
    local attr = self:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr then
       return attr
    end
 
    return false
end


---example script for scroll use check
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if item:getUniqueUsage() then
       if not player:canUseUniqueItem(item) then
          player:sendCancelMessage("You are not allowed to use this item.")
          return true
       end
   
       player:addExperience(666)
       item:remove(1)
       player:sendCancelMessage("blabla congratulations.")
    end
 
    return true
end

---example to obtain unique item
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:sendCancelMessage("Congratulations, you have found unique scroll that only you can use it.")
    local reward = player:addItem(6666)
    player:setUniqueItemUsage(reward)
    return true
end
 
Solution
Somehow i can't edit my post, i did a mistake, here is a fix

Lua:
UNIQUE_CUSTOM_ATTRIBUTE = "CUSTOM_UNIQUE_USAGE"

function Player.canUseUniqueItem(self, item)
 
    if not item then
       return nil
    end
 
    local attr = item:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr and attr == self:getGuid() then
       return true
    end
    return false
 
end

function Player.setUniqueItemUsage(self, item)
 
    if not item then
       return nil
    end
 
    return item:setCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE, self:getGuid())
end

function Item.getUniqueUsage(self)
 
    local attr = self:getCustomAttribute(UNIQUE_CUSTOM_ATTRIBUTE)
    if attr then
       return attr
    end
 
    return false
end


---example script for scroll use check
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    if item:getUniqueUsage() then
       if not player:canUseUniqueItem(item) then
          player:sendCancelMessage("You are not allowed to use this item.")
          return true
       end
  
       player:addExperience(666)
       item:remove(1)
       player:sendCancelMessage("blabla congratulations.")
    end
 
    return true
end

---example to obtain unique item
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:sendCancelMessage("Congratulations, you have found unique scroll that only you can use it.")
    local reward = player:addItem(6666)
    player:setUniqueItemUsage(reward)
    return true
end

I tested it and working perfect! :) Thanks you so much.
 
Back
Top