Making a trading system?

Currently i am using a table of objects then swapping the parents when the trade is complete, but is that the best way to do it? Im also curious how games like pet sim replicated the traded pet gui to the player since the server cannot access the playergui unless its put there by the server? For managing the trade data i am using a table with dictionaries of players userid’s to manage what they are trading, but is that the best way to do it? What i am really asking here is what is the best way to make a trading system and replicate data about trades from client to clients guis?

10 Likes

The general rule is to never trust the client. In your use case having the client send data to another client directly is a bad idea as the data sent could be tampered with. Instead I would have the server manage the trades as then the data cannot be tampered with. Having the server manage trades has a few advantages:

  • Items wont be able to be duped that easily as the client wont be able to tamper with the data.

  • You are giving the server more control over your game making it harder for the client to tamper with things.

You are probably wondering how this could be achieved. I have never made a trading system so bare with me. Here is a simple way this could be achieved:

  • You would first have a table containing all the pets each player owns. Each time the player buys a new pet then the server will add that pet to their list of pets. When the player trades the pets or sells them the pet will be removed from the table. This will give the server an up to date image of what pets each player owns.

  • When the client clicks trade on the GUI it sends a message to the server via a remote event.

  • The server checks if the trade is valid by checking if the player is trading any pets they do not own. This could be achieved by the server checking what pets the player owns by looking at their owned pets in the table we made earlier. If the player is trading pets that they do not own then the trade will be cancelled.

  • Then if the server checks go all right then the trade will proceed. The server will then give the player the player is trading the pets and then the server will remove the traded pets from their table of owned pets.

Having the server checks is essential as the client is sending the server data. Without the server checks the client could easily dupe and give them self pets. Remember if you have no choice but to send the server data from the client you must have server checks in place.

9 Likes

Thanks for the reply I think i understand now I just have a couple more questions, if your keeping your pet data in a table on the server how does the player access it for purposes such as looking at the pet info through a gui? Do you keep a pet folder on the client so that they can see that info? Also when the client sends a trade via a remote event do you pass in the pet id/ name or the pet object itself? Also when the player sends pet data to the server do you use a remote event to display that pet on the other players trading gui?

1 Like

you can send all this information in a table then index it on the other client. For example

– Client

local Item = {}
Item.Name = ‘Pet’
Item.Id = 0
Event:FireServer(‘AddItem’,plr1,plr2,Item)

– Server

function AddItem(…)
local plr1,pl2,item = …
– could verify whether the item is owned by plr1 incase they fire the event with an item they do not own
Event:FireClient(plr1,‘AddItem’,item)
Event:FireClient(plr2,‘AddItem’,Item)
end

Event.OnServerEvent:connect(function(request,…)
if request == ‘AddItem’ then
AddItem(…)
end
end)

– Client
function AddItem(…)
– Add item to ongoing trade
end

Event.OnClientEvent:connect(function(request,…)
if request == ‘AddItem’ then
AddItem(…)
end
end)

13 Likes

When the server validates a trade, it should send info back to the clients through a remote event describing the change in the players inventories so the client can keep it’s information updated.

If you inform the clients of any changes when they take place the client will have the information. You just need it to update GUIs at the right time.

You just want to pass the pet ID. The server just needs to know what the pet is in order to complete the trade.

You should keep a copy of pet information on the client so that your scripts can access it without having to request it from the server every time.

2 Likes

You Should Put the pets inside the Folder and Put it inside Server Storage to keep it safe so exploiters won’t Tamper with it.