How do i make a trade system?

Hello! I am trying to make a trade system for my game, but I don’t know how.
Does anyone know how to make a trade system? Also, if you know of any good Tutorials, can you leave a link to them?
I am trading pets, not tools btw.
Thanks, Mason

Trading systems are a lot of communication from the server to the participants in the trade.

For example if Player1 accepts the trade Player2 needs to be told this, so then the server would Fire a RemoteEvent telling Player2 that Player1 accepted.

Another example is Player2 adding a item. The server would need to Fire Player1 telling them that Player2 added a item.

Lastly, the LocalScript handling the ui would listen to these remotes firing them and then handle it accordingly.

Example code

-- Server Script
accept_trade.OnClientEvent:Connect(function(player, tradeId)
    -- find trade
    local trade = Trades[tradeId]
    if trade then
        update_client:FireClient(trade.partner, "Accepted", player)
    end
end)


-- LocalScript
update_client.OnServerEvent:Connect(function(updateType, partner)
    if updateType == "Accepted" then
        -- update ui visuals 
   end
end)

If you’re familiar with Object Oriented Programming then creating trades are a lot easier to me personally utilizing OOP.

-- Object Oriented Programming approach

local Trade = {}
Trade.__Index = Trade

Trade.new(player1, player2)
    local self = setmetatable({}, Trade)

    self.player1 = { player = player1, offer = {}, accepted = false }    
    self.player2 = { player = player2, offer = {}, accepted = false } 
 
    return self
end

function Trade:accept(player)
    if player == trade.player1.player then
        trade.player1.accepted = true
        remote:FireClient(trade.player2.player, "Accepted", player)
    elseif player == trade.player2.player then
        trade.player2.accepted = true
        remote:FireClient(trade.player1.player, "Accepted", player)
    end
end

function Trade:Destroy()
    setmetatable(self, nil)
end

remember about exploiters. they can fake offers to scam others

1 Like

Well of course, however this is just telling OP how a trade system works basically. The logic behind client communication can be overwhelming when first attempting to make a trade system. However, it’s then up to OP to take this example and create their own server checks.