Hi, I wanted to know how to make a tool trade system. So basically the players start a trade, they talk about it and then they can put in the tools they want to trade in a gui and then the trade goes through. I have no idea how this is made, because I looked around for tutorials and couldn’t see any, so help would be nice, as well as code snippets explaining how it works would make me much obliged.
I’m not one for giving people code flat out, but I will explain the basics of what you need to do.
Obviously you need your GUI for trading. This isn’t really a question about making a GUI so I’m going to assume that you have your GUI, and you can drag your tools to and from it or however you want to put them into it.
Now first thing you need is for players to initiate the trade with each other. Again this is done with a GUI of some sort, and a remote event to the server that basically tells it: “hey player1 is trying to trade with player2” is that ok? (The server considers it “ok” if neither player1 or player2 is currently trading, they’re both online, and they both have trades enabled, and probably also that neither player1 or player2 has sent a trade request in the last 5 seconds or so to prevent spamming.)
Next up, you fire a remote event to the two clients involved in the trade that opens up the trade GUI for each of them. The server will just represent this as a table that represents the state of the trade. Something like this:
trade = {}
trade.locked = false
trade.confirm_locked = false
trade.confirm_locked_time = 0
trade.player1 = {}
trade.player2 = {}
trade.player1.id = plr1_id
trade.player2.id = plr2_id
trade.player1.offer = {}
trade.player2.offer = {}
trade.player1.last_change = 0
trade.player2.last_change = 0
trade.player1.confirmed = false
trade.player2.confirmed = false
This is probably the barebones number of variables you need for this.
You’re going to need to use remote events to check each time a player adds or removes and item from this GUI. Each time they DO add or remove a tool from it, you need probably do at least these things:
- Fire remote event to the server saying you’re trying to add a tool to the GUI and check if that’s okay.
- If it is okay, (meaning the trade isn’t locked and both players are still connected) update the server’s state of the trade by changing “trade.player1.offer[1] = unique_tool_id” if a tool is added or “trade.player1.offer[1] =nil” if it’s removed. (Note this isn’t exact code, you need to keep up with how many items the player has added so you’re changing the correct offer box.)
- Set the “trade.confirm_locked” variable to true and set the “trade.confirm_locked_timer” to some amount probably like 5 seconds and have it count down. This prevents a player from confirming a trade if a player last minute adds or removes something.
- Fire a remote event to the clients that updates the state of the trade GUI with any changes to what has been offered as well as the timer for confirming the trade.
- Repeat 1-4 until the trade state is satisfactory for both players.
- One player fires the “Confirm” remote event to the server and the server makes sure that the “trade.confirm_locked” value is false and sets that player’s “trade.player.confirmed” value to true.
- Once one player confirms the trade, if any change is made to the trade, you should reset all player confirm values to false and set the confirm timer to 5 seconds again and send a remote event to players letting them know something has changed.
- If after all of that, both players end up confirming, the trade should probably set one extra timer just in case to give 5 seconds for either player to back out and if not, sends a remote event to both telling them the trade was successful and to close the gui window.
- If the above happens, then the server should also take the items form the trade and swap their inventories. (You should actually at this point use whatever method you’re using to save your game to save these inventory changes to each player’s save data. Then update their actual inventory based on the saved data. This way if a player disconnects or something, the trade either happened or didn’t and there’s no wiggle room to duplicate items.)
I think that’s about it, let me know if I missed something anyone else.
Wow thanks so much for explaining this deeply, I’ll look into using this.