• 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 binds items when equipe

_M4G0_

Well-Known Member
Joined
Feb 6, 2016
Messages
504
Solutions
16
Reaction score
98
if possible,
if player equips certain item then that item will bind to player so only that player can use that item

tfs 1.2
 
Try this.

data/events/scripts/player.lua
Put this at the top of the script, above all functions.
Lua:
local bindableItems = {1111, 2222, 3333, 4444} -- itemId's
Change
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    return true
end
To
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)

    -- filter out all items that are not bindable
    if not table.contains(bindableItems, item:getId()) then
        return true
    end
   
    -- check to ensure that the item is being moved to an equippable inventory slot
    if toPosition.x ~= CONTAINER_POSITION then
        return true
    end
   
    if toPosition.y == CONST_SLOT_BACKPACK and self:getSlotItem(CONST_SLOT_BACKPACK) then
        return true
    end
   
    if toPosition.y > CONST_SLOT_AMMO then
        return true
    end
   
    -- now we check for item binding
    local playerName = self:getName()
    local boundItem = item:getCustomAttribute("BoundItem")
   
    if not boundItem then
        item:setCustomAttribute("BoundItem", playerName)
        self:sendTextMessage(MESSAGE_STATUS_SMALL, "This item is now bound to this character.")
        return true
    end
   
    if boundItem ~= playerName then
        self:sendTextMessage(MESSAGE_STATUS_SMALL, "Unable to equip. This item is already bound to another character.")
        return false
    end
   
    return true
end

Change these 2 functions
Lua:
function Player:onLook(thing, position, distance)
    local description = "You see " .. thing:getDescription(distance)   
    if self:getGroup():getAccess() then
    .
    .
    .
    end
    self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end
Lua:
function Player:onLookInTrade(partner, item, distance)
    self:sendTextMessage(MESSAGE_INFO_DESCR, "You see " .. item:getDescription(distance))
end
to
Lua:
function Player:onLook(thing, position, distance)
    local description = "You see " .. thing:getDescription(distance)
   
    if thing:isItem() and table.contains(bindableItems, thing:getId()) then
        local boundItem = thing:getCustomAttribute("BoundItem")
        if not boundItem then
            description = string.format("%s\nItem is currently not bound.", description)
        else
            description = string.format("%s\nItem is currently bound to %s.", description, boundItem)
        end
    end
   
    if self:getGroup():getAccess() then
    .
    .
    .
    end
    self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end
Lua:
function Player:onLookInTrade(partner, item, distance)
    self:sendTextMessage(MESSAGE_INFO_DESCR, "You see " .. item:getDescription(distance))
   
    if table.contains(bindableItems, item:getId()) then
        local boundItem = item:getCustomAttribute("BoundItem")
        if not boundItem then
            description = string.format("%s\nItem is currently not bound.", description)
        else
            description = string.format("%s\nItem is currently bound to %s.", description, boundItem)
        end
    end
end
 
Last edited:
Try this.

data/events/scripts/player.lua
Put this at the top of the script, above all functions.
Lua:
local bindableItems = {1111, 2222, 3333, 4444} -- itemId's
Change
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    return true
end
To
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)

    -- filter out all items that are not bindable
    if not table.contains(bindableItems, item:getId()) then
        return true
    end
  
    -- check to ensure that the item is being moved to an equippable inventory slot
    if toPosition.x ~= CONTAINER_POSITION then
        return true
    end
  
    if toPosition.y == CONST_SLOT_BACKPACK and self:getSlotItem(CONST_SLOT_BACKPACK) then
        return true
    end
  
    if toPosition.y > CONST_SLOT_AMMO then
        return true
    end
  
    -- now we check for item binding
    local playerName = self:getName()
    local boundItem = item:getCustomAttribute("BoundItem")
  
    if not boundItem then
        item:setCustomAttribute("BoundItem", playerName)
        self:sendTextMessage(MESSAGE_STATUS_SMALL, "This item is now bound to this character.")
        return true
    end
  
    if boundItem ~= playerName then
        self:sendTextMessage(MESSAGE_STATUS_SMALL, "Unable to equip. This item is already bound to another character.")
        return false
    end
  
    return true
end

Change these 2 functions
Lua:
function Player:onLook(thing, position, distance)
    local description = "You see " .. thing:getDescription(distance)  
    if self:getGroup():getAccess() then
    .
    .
    .
    end
    self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end
Lua:
function Player:onLookInTrade(partner, item, distance)
    self:sendTextMessage(MESSAGE_INFO_DESCR, "You see " .. item:getDescription(distance))
end
to
Lua:
function Player:onLook(thing, position, distance)
    local description = "You see " .. thing:getDescription(distance)
  
    if thing:isItem() and table.contains(bindableItems, thing:getId()) then
        local boundItem = thing:getCustomAttribute("BoundItem")
        if not boundItem then
            description = string.format("%s\nItem is currently not bound.", description)
        else
            description = string.format("%s\nItem is currently bound to %s.", description, boundItem)
        end
    end
  
    if self:getGroup():getAccess() then
    .
    .
    .
    end
    self:sendTextMessage(MESSAGE_INFO_DESCR, description)
end
Lua:
function Player:onLookInTrade(partner, item, distance)
    self:sendTextMessage(MESSAGE_INFO_DESCR, "You see " .. item:getDescription(distance))
  
    if table.contains(bindableItems, item:getId()) then
        local boundItem = item:getCustomAttribute("BoundItem")
        if not boundItem then
            description = string.format("%s\nItem is currently not bound.", description)
        else
            description = string.format("%s\nItem is currently bound to %s.", description, boundItem)
        end
    end
end
I will try later
 
Let me know how it goes.
i have changed function
Lua:
thing:getCustomAttribute("BoundItem") to thing:getAttribute("BoundItem")

item:setCustomAttribute("BoundItem", playerName) to item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, ("BoundItem %s"):format(item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION), playerName))

end

02:51 You see a Wand of Remedy (Atk:1, critical hit chance +2%, critical hit damage +2%, life leech chance +2%, life leech amount +2%, mana leech chance +2%, mana leech amount +2%, magic level +25).
It can only be wielded properly by sorcerers of level 100 or higher.
BoundItem
Item is currently bound to 0.

"Unable to equip. This item is already bound to another character."
 
Ah. I did not realize 1.2 didn't have custom attributes.
I can't competently make this system without testing stuff in that case.

Sorry lol
 
Back
Top