Roblox Guide Help

Trade Module

In your wally.toml file, add this to your dependencies:

[dependencies] trade-module = "release/trade-module@0.0.1"

You have two solutions to import this module, depending if you wish to import the Controller or the Service on your own, or if you let the module give you the right code.

local TradeModule = require(ReplicatedStorage.Packages["trade-module"]) -- Serves for initialization for both the controller and -- the service depending where it's called.
local TradeService = Knit.GetService("TradeService")
local TradeController = Knit.GetController("TradeController")

Configuration

A file called TradeModule.config.lua is to be created in ReplicatedStorage/Shared/Configs.

return table.freeze({ TradePromptCallback = function() return true end, TradeAcceptedCallback = function() print("trade has started") end, TradeDeclinedCallback = function() print("trade has been refused") end, })

Explanation:

  • TradePromptCallback: This function is called whenever the player receive a trade request. If this callback returns true, the process continues, else, it is considered declined.

  • TradeAcceptedCallback: A function that's called whenever a trade is accepted.

  • TradeDeclinedCallback: A function that's called whenever a trade is declined.

Usage

Events

  • OnTradeRequest: Triggers when the player sends a trade request.

  • OnTradeRequestAccepted: Triggers when the player accepts a trade request.

  • OnTradeRequestDeclined: Triggers when the player declines a trade request.

  • OnTradeRequestCancelled: Trigger when a trade request is cancelled (Player leaving mid-trade for example).

  • OnTradeRequestCompleted: Triggers when the trade is valid and completed.

Methods

Client Side

Explanation

  • targetPlayer: The player to send the trade request to.


button.MouseButton1Click:Connect(function() TradeController:SendTradeRequest(targetPlayer) end)

    Client Side

    For more modularity, in order to accept a request, your own callback function must be implemented into the config file.


    TradePromptCallback will be automatically called whenever a trade request is received, and will continue the trade process if it returns "true".

      Client Side

      Explanation

      • sourcePlayer: The player to decline the trade request of.


      button.MouseButton1Click:Connect(function() TradeController:DeclineTradeRequest(sourcePlayer) end)

        Server Side

        Explanation

        • sourcePlayer: One of the player of the trade.

        • targetPlayer: The other player of the trade.


        local trade = TradeService:GetTrade(player1, player2) print(trade)

          Server Side

          Explanation

          • trade: The trade you're attempting to add items to. (Obtained from TradeService:GetTrade)

          • player: The player that will receive the items.

          • item: The name of the item you're adding.


          local trade = TradeService:GetTrade(player1, player2) TradeService:addItem(trade, player1, "SuperCoolPet")

          local trade = TradeService:GetTrade(player1, player2) TradeService:removeItem(trade, player1, "SuperCoolPet")

            Server Side


            To be called whenever you're sure items are all valid!


            Explanation

            • sourcePlayer: One of the player of the trade.

            • targetPlayer: The other player of the trade.


            TradeService:CompleteTradeRequest(player1, player2)

              Server Side


              To be called whenever the trade is 100% complete!


              Explanation

              • player1: One of the player of the trade.

              • player2: The other player of the trade.


              TradeService:FreeTrade(player1, player2)
                Last modified: 17 December 2024