Trader
Based on game build 714014 | Last updated: 2026-03-03
Overview
The Trader component enables an entity to participate in trade interactions by defining conditions under which it will accept gifts from other entities. It validates physical eligibility (e.g., not dead, not sleeping), checks for visual preference (via custom test callbacks), and handles item transfer. It automatically manages the trader and alltrader tags on the entity based on its enabled and acceptnontradable properties.
Usage example
local inst = CreateEntity()
inst:AddComponent("trader")
inst.components.trader:SetAcceptTest(function(trader, item, giver, count)
return item:HasTag("coin")
end)
inst.components.trader:SetOnAccept(function(trader, giver, item, count)
-- Handle successful trade logic
end)
Dependencies & tags
Components used: health, inventory, inventoryitem, itemmimic, playercontroller, sleeper, stackable
Tags: Adds trader, and conditionally adds alltrader when acceptnontradable is true and enabled is true.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
enabled | boolean | true | Whether the trader is active and able to accept items. |
deleteitemonaccept | boolean | true | Whether the accepted item is destroyed (true) or moved into the trader’s inventory (false). |
acceptnontradable | boolean | false | Whether the trader accepts items that lack the tradable tag (triggers alltrader tag if enabled). |
acceptstacks | boolean or nil | nil | Whether the trader accepts partial stacks; when set, stackable:Get(count) is used instead of removing the whole stack. |
test | function or nil | nil | Callback used in WantsToAccept to determine if the item is wanted. |
abletoaccepttest | function or nil | nil | Callback used in AbleToAccept to override the default physical eligibility check. |
onaccept | function or nil | nil | Callback invoked when a trade is successfully completed. |
onrefuse | function or nil | nil | Callback invoked when a trade is refused (e.g., item unwanted). |
Main functions
IsTryingToTradeWithMe(inst)
- Description: Determines whether the given entity
instis attempting to trade with this trader (via buffered action or remote interaction). - Parameters:
inst(Entity) - The candidate trading partner. - Returns:
trueifinsthas an activeGIVE,GIVEALLTOPLAYER, orGIVETOPLAYERaction targeting this trader; otherwisefalse.
IsAcceptingStacks()
- Description: Reports whether partial-stack trades are accepted.
- Parameters: None.
- Returns: Value of
self.acceptstacks;nilif unset.
Enable()
- Description: Enables the trader and ensures the
tradertag is present. - Parameters: None.
- Returns: Nothing.
Disable()
- Description: Disables the trader and removes the
tradertag. - Parameters: None.
- Returns: Nothing.
SetAcceptTest(fn)
- Description: Sets a callback function used to decide whether the trader wants the item (i.e., is not disgusted by it). This is checked after
AbleToAccept. - Parameters:
fn(function) - Signature:function(trader, item, giver, count) return should_accept end. - Returns: Nothing.
- Error states: Does not trigger action failure or reasons; use
SetAbleToAcceptTestfor such behavior.
SetAbleToAcceptTest(fn)
- Description: Sets a callback to override the default physical eligibility check in
AbleToAccept. Useful for custom failure reasons. - Parameters:
fn(function) - Signature:function(trader, item, giver, count) return boolean, reason end. - Returns: Nothing.
SetOnAccept(fn)
- Description: Registers a callback invoked when an item is successfully accepted.
- Parameters:
fn(function) - Signature:function(trader, giver, item, count). - Returns: Nothing.
SetOnRefuse(fn)
- Description: Registers a callback invoked when an item is refused (e.g., not wanted).
- Parameters:
fn(function) - Signature:function(trader, giver, item). - Returns: Nothing.
SetAcceptStacks()
- Description: Enables partial-stack trades (sets
acceptstackstotrue). - Parameters: None.
- Returns: Nothing.
AbleToAccept(item, giver, count)
- Description: Checks whether the trader is physically capable of accepting the item.
- Parameters:
item(Entity or nil) - The item being traded.giver(Entity or nil) - The entity giving the item.count(number or nil) - Number of items in the stack intended for trade.
- Returns:
trueif the trader can accept; otherwisefalse, "REASON"(e.g.,"DEAD","SLEEPING","BUSY","ITEMMIMIC"). - Error states: Returns
falseifenabledis false,itemisnil, orabletoaccepttestreturnsfalse. Mimic items are rejected unlessacceptsmimicsis true (currently commented out).
WantsToAccept(item, giver, count)
- Description: Checks whether the trader wants the item (subjective preference). Called after
AbleToAccept. - Parameters: Same as
AbleToAccept. - Returns:
trueifenabledand (notestfunction ortest()returnstrue); otherwisefalse. - Error states: Never returns a reason string—this is handled separately by
onrefuse.
AcceptGift(giver, item, count)
- Description: Attempts to accept the gift, performing all checks, item transfer, and side effects.
- Parameters:
giver(Entity or nil) - The entity giving the item.item(Entity) - The item being given.count(number or nil) - Number of items to accept. Defaults to1.
- Returns:
trueif accepted;falseif refused or ineligible. - Error states: Returns
falseimmediately ifAbleToAcceptfails; ifWantsToAcceptfails, triggersonrefuseand returnsfalse. If accepted anddeleteitemonacceptistrue, the item is removed; otherwise it is added to the trader’s inventory. Thetradeevent is pushed after processing.
GetDebugString()
- Description: Returns a debug-friendly string representation of the
enabledstate. - Parameters: None.
- Returns:
"true"ifenabled; otherwise"false".
Events & listeners
- Listens to:
enabledandacceptnontradableproperty changes (viaClassproperty hooksonenabledandonacceptnontradable) to updatetrader/alltradertags. - Pushes:
trade- fired after a successful trade with payload{ giver = giver, item = item }.