Interaction two clients (trade)

Hello! I would like to know how the interaction between two clients is made possible.

For example, one customer has a blue part and the other client has a red part and if they want to exchange them, how is this exchange possible?

1 Like

It’s possible but not directly unless using network ownership which I do not recommend as it opens the possibility to be exploited. Optimally, you’d have to use remote events/functions to achieve this. I can help you with that if you are looking for more of an in-depth answer.

1 Like

Understand… But is it very complex to make all that connection? I’d love it if you could help me, I’d really appreciate it. Do you have any examples for exchanging objects? Or something that can help me

1 Like

how are they storing the data? You basically tell blue to send a request to the server saying it wants to trade with red and the server will wait for red to respond. if it gets the signal the server sends the data to them

1 Like

It isn’t too difficult to manage. Here’s what I would do personally, it is very basic though.

local myRemoteFunction = location -- put wherever your remote function is
local myRemoteEvent = location -- we will use the RemoteEvent to tell the client that the trade was successful
local acceptButton = guiButton -- assuming you're using a UI button to accept the trade

local myItem = game:GetService('Players').LocalPlayer.Inventory.Item -- this is where your item would be. I assume you would use a valuebase taking datastores in account.
local targetPlayer = game:GetService('Players'):FindFirstChild('MatiasHarders') -- just using you as an example

guiButton.MouseButton1Click:Connect(function()
    local tradeSuccessful, errorMessage = myRemoteFunction:InvokeServer(myItem, targetPlayer) -- this is where you'd invoke the server using that item specified above. You also want to send the target at a certain point.
    if not tradeSuccessful then
        print(errorMessage) -- if the trade isn't successful then print the error message provided from the server
    elseif tradeSuccessful == 'Pending' then
        print('Trade is pending!')
    end
end)

myRemoteEvent.OnClientEvent:Connect(function(successful, message)
    if not successful then -- if the trade didn't go through then
        print(message)
    end
end)

Server script:

local myRemoteFunction = location -- put wherever your remote function is. Should be the same one as above.
local myRemoteEvent = location -- same remoteevent as above
local activeTrades = {} -- I would store the current active trades in a table

myRemoteFunction.OnServerInvoke = function(player, item, target)
    local trade = activeTrades[player.Name..':'..target.Name] or activeTrades[target.Name..':'..player.Name]
    if trade then
        if trade['Accepted_'..target.Name] == true then
            if trade['Item_'..target.Name and item then -- if both items exist then
                trade['Item_'..target.Name].Parent = player.Inventory -- put the target's item in the requester's inventory
                item.Parent = target.Inventory -- put the requester's item in the target's inventory
                activeTrades[player.Name..':'..target.Name] = nil -- delete the trade
                activeTrades[target.Name..':'..player.Name] = nil
                myRemoteEvent:FireClient(target, true)
                return true -- return that the trade was successful to the client
            else
            myRemoteEvent:FireClient(target, 'One player\'s item doesn\'t exist!')
            return false, 'One player\'s item doesn\'t exist!'
        end
    else -- if the trade doesn't exist then create a new trade
        if item then -- if it exists on the server, this is to prevent exploits and potentially catch any errors./
            activeTrades[player.Name..':'..target.Name] = { -- would look like '7z99:MatiasHarders'
                ['Item_'..player.Name] = item;
                ['Accepted_'..player.Name] = true
                ['Accepted_'..target.Name] = false
            }
        return 'Pending'
        end
    else
        myRemoteEvent:FireClient(target, false, player.Name..'\'s item does not exist!') -- fire the target
        return false, player.Name..'\'s item does not exist!'
    end
end
1 Like

Woow! It’s not as difficult as it seems, thank you very much for your explanation and the code, tomorrow early I will do some tests with all this, I keep learning, thank you all and a lot of success in your projects!

1 Like