YouTube2Text

Creating a Trading System! Roblox Studio Simulator Guide Episode 32 — Transcript

by MonzterDEV · 11,184 words · 1,492 segments · language en · Watch on YouTube

Full transcript

  1. 0:00in today's episode of the simulator
  2. 0:01series we are going to be scripting the
  3. 0:03trading system and as always if you guys
  4. 0:04do enjoy the video where it does help
  5. 0:05you out make sure you smash like button
  6. 0:06also subscribe button and turn post
  7. 0:07notifications on if you want to get
  8. 0:09notified when I upload more of Roblox
  9. 0:10with all my content additionally I have
  10. 0:11a patreon it feels like to support me
  11. 0:12and gain access to all the scripts and
  12. 0:14the game file that I made during this
  13. 0:15episode there's a link down below
  14. 0:16description and you guys can go and
  15. 0:17check that out with that being said
  16. 0:18let's get into it we're first gonna
  17. 0:19start off by scripting the box so inside
  18. 0:21of the trade menu GUI we're gonna open
  19. 0:22up the bond script right here and now
  20. 0:24for the settings bun variable we're
  21. 0:25going to change around the bunch UI dot
  22. 0:27left buttons holder and then we can get
  23. 0:29the trade button right from there and of
  24. 0:31course we'll rename this variable from
  25. 0:32settings button to trade button and now
  26. 0:34that's all done this will open up the
  27. 0:35trade menu GUI so that you can send
  28. 0:37requests to other players for trading
  29. 0:39the next thing we'll be scripting is the
  30. 0:40trade menu what we're going to do is
  31. 0:41we're actually going to delete the
  32. 0:42button and Handler script and then we're
  33. 0:44going to go directly into the manager
  34. 0:45script and start cleaning this up a
  35. 0:46little bit first of all we don't need
  36. 0:47that settings variable and we don't need
  37. 0:49the exit variable neither we can also
  38. 0:51delete this for Loop and this exit Mouse
  39. 0:53button one click right there and this is
  40. 0:55all good now we'll start modifying the
  41. 0:56on and off color variable that we have
  42. 0:58and what we're actually going to be
  43. 0:59changing them too is this little button
  44. 1:01right here so the background color for
  45. 1:03this bun right here just remember
  46. 1:04whenever we send out a tray request to a
  47. 1:06player we want to modify this button to
  48. 1:07say pending and have a purple background
  49. 1:09color but if it's not pending then we
  50. 1:11want it to be green and have the text
  51. 1:12send on it so we'll rename the first
  52. 1:14variable from on color to send
  53. 1:16underscore color and then from off color
  54. 1:18to pending color now the pending color
  55. 1:20is not going to be red it's actually
  56. 1:21going to be a purple so something like
  57. 1:23that now next thing we're going to do is
  58. 1:24we're going to rename the generate
  59. 1:25setting function to generate player and
  60. 1:27instead of accepting a setting which is
  61. 1:29going to be a table it'll actually
  62. 1:30accept a player which is of course a
  63. 1:32player now let's go inside of the frame
  64. 1:33inside of the container and inside of
  65. 1:35the template right here so that we can
  66. 1:37see all the components it has and what
  67. 1:38we're going to do is we're just going to
  68. 1:39delete most of the stuff in this
  69. 1:41function right here besides cloning and
  70. 1:42then also parenting it to The Container
  71. 1:44next what we're going to do is we're
  72. 1:45going to set the Clone dot name so the
  73. 1:48player.user ID so that we can easily
  74. 1:50identify this template then what we want
  75. 1:51to do is we want to modify the player
  76. 1:53which is actually a text label so we're
  77. 1:54going to set the text of the player text
  78. 1:56label to the player dot display name
  79. 1:58next inside of the Clone we have have
  80. 2:00the Avatar which is an image label and
  81. 2:02we're actually going to set this image
  82. 2:03to a thumbnail of the player which
  83. 2:05Roblox will actually provide for us so
  84. 2:06the way we get this is by using the
  85. 2:08player's service and then we call a
  86. 2:09method called get user thumbnail async
  87. 2:12and what we need to pass through here is
  88. 2:13the player's user ID and we also need to
  89. 2:15pass through an enum. thumbnail type so
  90. 2:18that we have to say the type of
  91. 2:19thumbnail that we want and we actually
  92. 2:20want the headshot for this and we also
  93. 2:22have to pass through a thumbnail size to
  94. 2:24this as well and for this because it is
  95. 2:26relatively small we'll probably want a
  96. 2:2748 by 48 but if you want something
  97. 2:29larger you can request that as well and
  98. 2:30then we also need to make sure that the
  99. 2:32visibility is set to true so we can
  100. 2:33actually see this and then we need to
  101. 2:35listen for whenever the button on this
  102. 2:36is actually clicked so clone dot button
  103. 2:38dot mouse Bond one click and connect
  104. 2:40that to an anonymous function now let's
  105. 2:42consider when we actually want to call
  106. 2:43this function we want to call this
  107. 2:44function whenever a player joins the
  108. 2:45game or leaves the game but also when we
  109. 2:48first join the game we also need to get
  110. 2:50all the players that are in our game
  111. 2:51currently and we need to generate a
  112. 2:52template for them so we can first start
  113. 2:54off by using a for Loop and we can say
  114. 2:56four underscore player in I pairs
  115. 2:58players get children do and all we have
  116. 3:00to do is call the generate player
  117. 3:01function and pass through the player and
  118. 3:03we also want to do this whenever a
  119. 3:04player is added to the game so
  120. 3:05players.player added connect function
  121. 3:07player and then we're just going to call
  122. 3:08the function once again and pass through
  123. 3:10the player now whenever a player leaves
  124. 3:11the game we actually want to delete the
  125. 3:13template so players that player removing
  126. 3:15connect function player and now we need
  127. 3:18to remove it from the container so the
  128. 3:19way we can do this is by saying local
  129. 3:20player template equals container fine
  130. 3:23first child
  131. 3:25player.userides if we find that template
  132. 3:27then what we'll do is we'll say player
  133. 3:30template destroy and I spelled that
  134. 3:31wrong but there we go now one thing is
  135. 3:33that we already have a variable called
  136. 3:34player in lowercase so what we're going
  137. 3:36to do is we're actually going to rename
  138. 3:37that variable from player to local
  139. 3:39player so we have no confusion with this
  140. 3:41variable right here and the variable
  141. 3:42called player inside of our other
  142. 3:44functions as well now the only time that
  143. 3:45we actually really need to use the local
  144. 3:47player is when we call this function and
  145. 3:49what we're going to do is we're going to
  146. 3:49say if player equals local player then
  147. 3:52return n and the reason we're doing that
  148. 3:54is because we don't want to actually
  149. 3:56generate a template for our own player
  150. 3:58because obviously we can't trade with
  151. 3:59ourselves elves so whenever this
  152. 4:00function is called if it is our player
  153. 4:02then we don't want to generate anything
  154. 4:03so we'll just return otherwise it'll
  155. 4:05generate a template for whatever player
  156. 4:06we pass through to here now whenever the
  157. 4:07button is clicked inside of the template
  158. 4:09we want to actually do something and
  159. 4:10what we want to do is we want to send a
  160. 4:11request to another player to see if they
  161. 4:13would like to trade with us now the way
  162. 4:14we're going to do this is by actually
  163. 4:15using a remote function so let's go
  164. 4:17inside of the replicated storage inside
  165. 4:19of the remotes folder and create a new
  166. 4:20remote function this remote function is
  167. 4:22going to be called send trade requests
  168. 4:25and then inside of where we're listening
  169. 4:26for the mouse button one click we're
  170. 4:27going to create a new variable called
  171. 4:29result and we're going to set that equal
  172. 4:30to the remotes dot send trade request
  173. 4:32invoke server and now the argument that
  174. 4:34we're going to pass through to here is
  175. 4:35the player.userid and remember this is
  176. 4:38referring to the player whose template
  177. 4:40this is not our own player now the only
  178. 4:42possible result that we're ever going to
  179. 4:43get back from this is either true or
  180. 4:44false so what we're going to do is we're
  181. 4:46going to say if result then and now we
  182. 4:47want to do something if there is a
  183. 4:49result what we want to do is we want to
  184. 4:50set the button so the button that we're
  185. 4:52listening to right now we want to
  186. 4:53actually set the background color 3 to
  187. 4:55the pending color and we want to modify
  188. 4:57the text of the button as well so
  189. 4:58button.test equals pending and now this
  190. 5:01is basically going to tell our player
  191. 5:02that the player we sent the trade
  192. 5:03request to is currently on cooldown so
  193. 5:05while it's pending we have to wait and
  194. 5:07we cannot send another invite so then
  195. 5:09what we're going to do is we're going to
  196. 5:10say task dot delay and this is basically
  197. 5:12going to say in 30 seconds reset this
  198. 5:14button so it no longer displays a
  199. 5:16cooldown now for the duration you can
  200. 5:17obviously pick however long you want
  201. 5:19players to have a trade request cooldown
  202. 5:20for I'm just going to say 15 and then
  203. 5:22we'll create Anonymous function inside
  204. 5:23of here and then we'll basically just do
  205. 5:24the opposite of what we just did so
  206. 5:26instead of setting the background color
  207. 5:27to the pending we'll set it to the send
  208. 5:29color because they're now able to send a
  209. 5:30request again and we'll set the text to
  210. 5:31send as well so whenever a player sends
  211. 5:33a trade request the button will display
  212. 5:34pending and then after the cooldown has
  213. 5:36expired then it will be reset to what it
  214. 5:38was originally so now that we have that
  215. 5:39all set up let's go ahead and actually
  216. 5:41comment out this if statement that we
  217. 5:43created right here and start up our game
  218. 5:44so that we can kind of see what a
  219. 5:46generated template looks like in case we
  220. 5:47missed anything and I don't know how I
  221. 5:49missed this Handler requirement
  222. 5:50statement up there but go ahead and
  223. 5:52delete that and then when you start your
  224. 5:53game you should have no issues so we can
  225. 5:55see when we pop into our game our Avatar
  226. 5:56has been generated our names right there
  227. 5:58and the button is set to send as as it
  228. 6:00should be by default we can't test the
  229. 6:01button clicking because we actually need
  230. 6:03a server to respond to a remote function
  231. 6:05otherwise we would test that right now
  232. 6:06as well now the next thing we're going
  233. 6:07to do is we're actually going to insert
  234. 6:09a local script inside of the trade
  235. 6:11request and we'll rename this to manager
  236. 6:12then what we can do is we can actually
  237. 6:14copy some of our standard variables from
  238. 6:15our other manager scripts like these
  239. 6:17ones right here for instance so we're
  240. 6:18going to get to player service we'll get
  241. 6:19the replicated storage we'll get the
  242. 6:20remotes folder and then we'll also get
  243. 6:22the player variable as well we also want
  244. 6:24to get the screen GUI and the frame we
  245. 6:25don't actually need a container but we
  246. 6:27will actually create a few more
  247. 6:28variables for this specific GUI Right
  248. 6:30Here Local message equals frame Dot
  249. 6:33message local Avatar equals frame dot
  250. 6:36Avatar local accept equals frame dot
  251. 6:40accept and local exit button equals
  252. 6:43frame dot exit button so then what we'll
  253. 6:45do is we'll create a new function called
  254. 6:46display GUI and this will accept the
  255. 6:48trader which is actually going to be a
  256. 6:50player so not us it'll be the opposite
  257. 6:52player and remember the GUI that we're
  258. 6:54working on is this one right here so a
  259. 6:55little notification will pop up whenever
  260. 6:57a player sends us a trade request so
  261. 6:58what we want to do first is we want to
  262. 7:00enable the screen GUI so the player sees
  263. 7:02the notification then we need to set the
  264. 7:03Avatar's image to the player's thumbnail
  265. 7:05and remember we do this by using a
  266. 7:07method inside of the player's service
  267. 7:09and the method is get user thumbnail
  268. 7:11async and then we'll pass through that
  269. 7:12player's user ID so trader.user ID enum
  270. 7:15headshot as the type and 100 by 100 as
  271. 7:18the size then for the text of the
  272. 7:20message we want to display the player
  273. 7:22dot display name and we want to combine
  274. 7:24that with another string and that's
  275. 7:25going to be has sent you a trade request
  276. 7:28exclamation mark and now we're actually
  277. 7:30going to take this string and we're
  278. 7:32going to turn that into a static
  279. 7:33variable so local message template
  280. 7:35equals has sent you a trade request and
  281. 7:37then we can put the variable right there
  282. 7:38just in case we come back in the future
  283. 7:39we can easily see what the string is for
  284. 7:41and we can modify now that we have this
  285. 7:42function set up we have to think about
  286. 7:43when do we actually want to call this
  287. 7:45we're going to be using a remote event
  288. 7:46and from the server whenever a player
  289. 7:48sends out a trade request is going to go
  290. 7:50to the server and then the server is
  291. 7:51going to see if the player can send a
  292. 7:53trade request to the other player and
  293. 7:54then we'll fire a remote event telling
  294. 7:56the other player that they have just
  295. 7:57received a request so we're going to
  296. 7:59create a new remote event in the remotes
  297. 8:00folder and we're going to rename this to
  298. 8:02trade request received so inside of the
  299. 8:05manager script we're going to listen to
  300. 8:06the trade request received event then
  301. 8:08that on client event and we're going to
  302. 8:09connect that to the display GUI function
  303. 8:11next we'll script the exabon so exabun
  304. 8:13dot mouse button one click connect
  305. 8:15function screen GUI dot enabled equals
  306. 8:18false simple as that now we also need to
  307. 8:20script the accept Button as well so
  308. 8:21we'll say accept.mousebond one click
  309. 8:23connect function and what this would do
  310. 8:25is whenever we click this button we of
  311. 8:26course want the screen's UI to not be
  312. 8:28enabled anywhere because we don't want
  313. 8:30to be able to see it and then what we
  314. 8:31also need to do is we need to use a
  315. 8:32remote event to tell the server that
  316. 8:34we've just accepted this trade request
  317. 8:35so we'll go ahead and add a new remote
  318. 8:36inside of here and this one's going to
  319. 8:38be called accept trade request so back
  320. 8:41inside whenever we click the accept
  321. 8:42button what we're going to do is we're
  322. 8:43going to say remote stop accept tray
  323. 8:45request fire server now one thing is is
  324. 8:48that we want to actually tell the server
  325. 8:49who we want to trade with or whose
  326. 8:51request we actually accepted in case
  327. 8:52multiple people senders request at once
  328. 8:54so what we're going to do is we're going
  329. 8:55to create a variable up here and that's
  330. 8:56going to be called Trader we're not
  331. 8:58going to set that to anything yet but
  332. 8:59whenever we call the display GUI
  333. 9:01function we're actually going to set the
  334. 9:02trader variable to the player that's
  335. 9:04sent to this function as well and the
  336. 9:05reason that we're doing that is because
  337. 9:06we can then easily tell the server whose
  338. 9:08trade request we're accepting so now
  339. 9:10that we've done all that work what we
  340. 9:11now need to do is we actually need to
  341. 9:12create a server script for working with
  342. 9:13some of these remote events so we're
  343. 9:15going to go into the server script
  344. 9:16service and we'll add a brand new script
  345. 9:17to this we'll rename this script to
  346. 9:19trade and of course inside of here we're
  347. 9:20going to do the player service so game
  348. 9:22get service players we're also going to
  349. 9:24the replicated storage as well and then
  350. 9:25from the replicated storage we're going
  351. 9:27to get the remotes so we'll say local
  352. 9:28remotes equals replicated storage dot
  353. 9:31remotes now remember that send trade
  354. 9:32request remote function that we made
  355. 9:33that's the first thing that we're going
  356. 9:35to work with so we're going to say
  357. 9:35remote it's not send trade request dot
  358. 9:38on server invoke and now we need to
  359. 9:39create a function up here so that
  360. 9:40function is going to be called send
  361. 9:42trade requests and that's of course
  362. 9:44going to accept a player this is going
  363. 9:46to be the person who sent it and then
  364. 9:47the target ID so a player's user ID that
  365. 9:50this player wants to trade with so then
  366. 9:52we can set the on invoke to the send
  367. 9:54trade request function just like that
  368. 9:55now inside this function we'll create a
  369. 9:57variable for the actual Target player
  370. 9:59and we can get the player by using the
  371. 10:00player's service and saying get player
  372. 10:02by user ID so that we can pass through
  373. 10:04the target ID to that method and then we
  374. 10:05should get returned a player but if we
  375. 10:07don't get returned to player then we
  376. 10:08just run a return end and stop the
  377. 10:10function right there now the next thing
  378. 10:11we need to do is we need to check if the
  379. 10:12player is on cooldown because we don't
  380. 10:14want our players to get spammed with
  381. 10:16trade requests so what we're going to do
  382. 10:17is we're going to create a new function
  383. 10:18and that's going to be called is on
  384. 10:20cooldown and that'll accept the player
  385. 10:22ID and the target ID as well which both
  386. 10:25will be numbers now we're actually going
  387. 10:26to create a table so that we can easily
  388. 10:28keep track of what players have sent out
  389. 10:30requests to others and if they're
  390. 10:31trading and other things like that so
  391. 10:33we'll create a new table and this is
  392. 10:34going to be called trade requests and
  393. 10:36that's going to be set to an empty table
  394. 10:37then we're going to do is inside of the
  395. 10:38is on cooldown function is we're going
  396. 10:40to create a variable called requests and
  397. 10:42we're going to set that equal to trade
  398. 10:43requests and we're going to index that
  399. 10:45with the player ID now if this table
  400. 10:46doesn't actually exist then we just want
  401. 10:48to return end because that would mean
  402. 10:50that the player is not on cooldown and
  403. 10:52now if the table does exist we actually
  404. 10:53need to find the targets ID inside of
  405. 10:56that table so we're going to say return
  406. 10:57table dot find quests and then we're
  407. 11:00going to look for the Target ID so if
  408. 11:02the target ID is found then something
  409. 11:03will be returned through this function
  410. 11:05so then we're going to say if is on
  411. 11:07cooldown and we'll pass through the
  412. 11:08player.userid and the target ID then
  413. 11:11return end so now that we know the
  414. 11:13player is not on cooldown we can
  415. 11:15actually send out the invite and what we
  416. 11:16need to do is we need to get the
  417. 11:18player's trade request table similar to
  418. 11:19how he did and is on cooldown so we're
  419. 11:22going to say local player table equals
  420. 11:24trade request and index that with the
  421. 11:26player dot user ID now the reason that
  422. 11:28we need to do this is because we need to
  423. 11:29check if this first exists before we can
  424. 11:31actually add a player to this table so
  425. 11:33then we're going to say if not player
  426. 11:34table so if this table doesn't exist
  427. 11:36then we actually just want to create the
  428. 11:37table so we're just going to set that to
  429. 11:39a blank table and then after we create
  430. 11:40that table we then want to set the
  431. 11:42player table variable to this table
  432. 11:43right here then after we've done that
  433. 11:45we're actually going to fire off our
  434. 11:46trade request received remote so we're
  435. 11:48going to say fire client and who are
  436. 11:50going to fire this to is the Target and
  437. 11:52we're going to pass through the player
  438. 11:53who is saying to that client that they
  439. 11:55want to trade with them now we of course
  440. 11:56need to add this target to the player's
  441. 11:58list so that they can be on cooldown so
  442. 12:00we're going to say table dot insert
  443. 12:01player table and we're going to pass
  444. 12:02through the target ID and then we'll
  445. 12:04remove the player from the cooldown by
  446. 12:05saying task dot delay and of course
  447. 12:07remember set this to however long your
  448. 12:08cooldown is we'll just say 15 seconds
  449. 12:10and we'll create an anonymous function
  450. 12:12inside of here and then what we're going
  451. 12:13to do is we're going to say local index
  452. 12:14equals table dot find trade requests and
  453. 12:18index that with the player.user ID and
  454. 12:20we're going to try to find the target ID
  455. 12:21and then we'll say table dot remove
  456. 12:23trade requests pass on the same variable
  457. 12:25so tray request player.userid and we're
  458. 12:27going to remove the index from that
  459. 12:29table so this will add their cooldown
  460. 12:31and then after the cooldown ends they
  461. 12:32will be removed from it and then finally
  462. 12:34we just need to return true so that we
  463. 12:36know this trade has been sent out
  464. 12:37successfully one thing we have to touch
  465. 12:38up real quick inside of the trade Crypt
  466. 12:40inside of the is on cooldown function we
  467. 12:42have to say actually if not requests so
  468. 12:44if the player doesn't have any requests
  469. 12:46sent out then we're going to return
  470. 12:47false because the player obviously
  471. 12:48wouldn't be on cooldown we can then
  472. 12:50start this up go into the trade menu and
  473. 12:51we can see that we can send ourselves a
  474. 12:53trade request and we can even see that
  475. 12:54the notification popped up down here as
  476. 12:56well so now that we have all that
  477. 12:57working the next thing that we actually
  478. 12:58need do is start scripting up accepting
  479. 13:01the trade requests and what I mean by
  480. 13:02this is inside of the manager anytime we
  481. 13:04click on the accept button we fire this
  482. 13:06accept trade request remote event right
  483. 13:08there so what we're going to do is back
  484. 13:09inside of the trade script we're going
  485. 13:10to say remotes dot accepttraderequest
  486. 13:12dot on server event connect and now
  487. 13:14we're going to create a new function for
  488. 13:15this so we'll say local function except
  489. 13:18trade requests and that's going to
  490. 13:20accept a player and a Target ID as well
  491. 13:23which is going to be a number so then we
  492. 13:24can connect that event to that function
  493. 13:26right there and what are we going to do
  494. 13:27inside this function well something
  495. 13:28similar we're actually going to get the
  496. 13:30target again from the target ID just to
  497. 13:32make sure that player exists and is
  498. 13:33inside of our game now what we need to
  499. 13:35do is we actually need to check if
  500. 13:36either of the players are currently
  501. 13:37trading with another player because if
  502. 13:39they are then we don't want to start up
  503. 13:40any more trades or else things can go
  504. 13:42bad so what we're going to do is we're
  505. 13:43going to create a new function up here
  506. 13:44and that'll be the top function and it's
  507. 13:46going to be called is trading and it'll
  508. 13:48accept the player that we want to check
  509. 13:49if they're trading now the way that
  510. 13:50we're actually going to track if players
  511. 13:52are trading or not is by once again
  512. 13:53using another table so we're going to
  513. 13:55say local trading equals and then a
  514. 13:57blank table and now what we're going to
  515. 13:58do is to check if a player is trading is
  516. 14:01return trading and the player's user ID
  517. 14:03so this will return if the player is in
  518. 14:05this table or not so going back inside
  519. 14:07of the accept trade request function
  520. 14:08we're going to say if is trading and
  521. 14:10we'll pass through the player and the
  522. 14:11target so if either of the players are
  523. 14:13trading then what we want to do is we
  524. 14:15just run return end and stop the
  525. 14:16function right there the next thing we
  526. 14:17need to check is if the player we're
  527. 14:19trying to trade with has sent us a
  528. 14:21request for us to accept and the reason
  529. 14:23that we need to do this is because of
  530. 14:24exploiters exploiters could tell us that
  531. 14:26they're accepting another player's trade
  532. 14:28but that player never actually sent them
  533. 14:29a trade request and if we let that go
  534. 14:31through then exploiters would be able to
  535. 14:33forcefully trade with all the other
  536. 14:34players on our server so what we're
  537. 14:36going to do is create another function
  538. 14:37and that's going to be called has sent
  539. 14:39request and what this is going to do is
  540. 14:41it's going to accept the acceptors ID
  541. 14:44which is of course going to be a number
  542. 14:45and then the sender's ID which is of
  543. 14:48course going to be a number as well the
  544. 14:49way we're going to be finding if a
  545. 14:51player has sent a requests or not is
  546. 14:52going to be by using that trade request
  547. 14:53table that we created earlier so what
  548. 14:55we're going to do is we're going to say
  549. 14:56local request table equals trade
  550. 14:58requests and we're gonna index that with
  551. 15:00the sender ID now if the request table
  552. 15:02doesn't actually exist then we're just
  553. 15:04going to return end right here because
  554. 15:05that would mean that the player has not
  555. 15:07sent any requests but now if that table
  556. 15:08does exist then what we're going to do
  557. 15:10is we're going to say table dot find and
  558. 15:11inside of the request table we're going
  559. 15:13to look for the acceptors ID and now if
  560. 15:15we're able to find that then we're going
  561. 15:16to return true otherwise if we get to
  562. 15:18the end of the function we're just going
  563. 15:19to return false so if we are able to
  564. 15:21find that player's name inside of the
  565. 15:22other player's request table then we
  566. 15:23know that the request has been sent so
  567. 15:25going back into our accept trade request
  568. 15:27function we're going to say if not has
  569. 15:29set request passed through the
  570. 15:30player.user ID and the target ID then
  571. 15:33return end so now if the target exists
  572. 15:36if neither of the players are trading
  573. 15:38and as long as the player has sent a
  574. 15:39request to the acceptor then we can
  575. 15:41actually get these two players trading
  576. 15:43so now let's add both of these players
  577. 15:44to the trading table so we're going to
  578. 15:46say player.userid equals Target ID and
  579. 15:49then we'll also do the opposite for the
  580. 15:50Target ID so we're going to say trading
  581. 15:52equals Target ID and this equals
  582. 15:54player.userid and now we're also going
  583. 15:56to create another remote event and we'll
  584. 15:57rename this one to trade B again and now
  585. 16:00we're gonna fire this remote event to
  586. 16:01both the clients so that we can activate
  587. 16:02their GUI and get them into the trading
  588. 16:04mode so we'll say remotes dot trade
  589. 16:06began fire clients so we're gonna pass
  590. 16:08through the player and we're also going
  591. 16:09to give that player the target ID and
  592. 16:11then we also need to call this for the
  593. 16:12Target and pass through the players user
  594. 16:14ID just like that and now this function
  595. 16:16is all done now that we've done all that
  596. 16:17we can actually begin scripting the
  597. 16:19trade GUI itself we're gonna go into the
  598. 16:21trade GUI and we're going to delete both
  599. 16:23the button and the Handler scripts and
  600. 16:24we'll only be left with the manager
  601. 16:26script now once inside of here we have
  602. 16:27to clean this up a little bit so of
  603. 16:29course we don't need the pets config or
  604. 16:30the pets model nor do we need the
  605. 16:31Handler we will actually keep the
  606. 16:33container variable but for the rest of
  607. 16:35the stuff we can pretty much delete it
  608. 16:36all except for the template variable and
  609. 16:38then we'll also delete all of this down
  610. 16:40here as well now once we're left with
  611. 16:42all these variables we have to create
  612. 16:43some more for a lot of the different
  613. 16:44components that are inside of our GUI so
  614. 16:46we'll start off by making a variable for
  615. 16:48our title now it's going to be set to
  616. 16:49frame dot title then we need to start
  617. 16:51making variables for all of the hour
  618. 16:52components so like our accepted our
  619. 16:55label our offer our Avatar and then
  620. 16:57we'll also do the same for their their
  621. 16:59components as well so say local our
  622. 17:01Avatar equals frame dot our Avatar then
  623. 17:05our label and we'll change the variable
  624. 17:07name once we get all the hours out of
  625. 17:09the way then our offer and finally our
  626. 17:11acceptance now we'll just quickly rename
  627. 17:13the variables once we got all four of
  628. 17:15our variables then we need to copy these
  629. 17:17and we will replace our with there so
  630. 17:19their Avatar their label and their offer
  631. 17:21and now we also need to rename the
  632. 17:23variables as well and there we go we've
  633. 17:24now got all of our and their components
  634. 17:26and variables as well we could also put
  635. 17:28a little space between those two so that
  636. 17:29we can easily differentiate them now
  637. 17:31that we have all those components we
  638. 17:32also need to get the accept button so
  639. 17:34local accept equals frame dot accept and
  640. 17:37we also need to get the decline Button
  641. 17:39as well so frame dot decline now
  642. 17:41remember in our trade script we fired
  643. 17:43the trade began remote whenever we want
  644. 17:45both of the players to begin trading so
  645. 17:47we're going to actually listen to this
  646. 17:48remote in our script right here so that
  647. 17:50we can display the GUI and start doing
  648. 17:52the trading stuff so we're going to say
  649. 17:54remotes.tradebegan dot on client event
  650. 17:56connect and we'll actually create a
  651. 17:58function for handling this so we're
  652. 17:59going to say local function begin trade
  653. 18:01and that's going to accept the target ID
  654. 18:03which is a number so now whenever this
  655. 18:05function is called we want to get the
  656. 18:07target from their target ID so we're
  657. 18:08going to say local Target equals players
  658. 18:10get player by user ID Target ID and of
  659. 18:13course if not Target then return and and
  660. 18:16now if we are able to get a Target what
  661. 18:17do we want to do we want to enable the
  662. 18:19screen GUI so we can actually see that
  663. 18:21we're going to trade with another player
  664. 18:22now before we enable the screen gy we
  665. 18:24also want to do a couple of things to
  666. 18:26sort of prepare our GUI as well but what
  667. 18:28we can do first is we can connect the
  668. 18:29trade began remote to this function
  669. 18:31right here and then we can create
  670. 18:32another function above this function and
  671. 18:33this function will be called update
  672. 18:35labels and that's going to accept a
  673. 18:37player which is going to be our Target
  674. 18:38now inside of this function we want to
  675. 18:40modify the text of the title and what do
  676. 18:42we want the title to say we wanted to
  677. 18:44say trade with and then we want it to
  678. 18:46display the players or the targets
  679. 18:48display name just like that but of
  680. 18:50course we have to join them together
  681. 18:51using two periods and now that will be
  682. 18:53good additionally we need to modify some
  683. 18:55of their variables as well so such as
  684. 18:57their label dot text we we want that to
  685. 18:59be set to target.display name and we
  686. 19:02want to join that with apostrophe s
  687. 19:03offer so this will display something
  688. 19:05like monster devs offer or something
  689. 19:07similar to that and we also need to
  690. 19:08update their Avatar image as well and
  691. 19:10what are we going to set that to well
  692. 19:12we're going to use the player service to
  693. 19:13get the player's thumbnail and we're
  694. 19:15going to pass through the target.user ID
  695. 19:16headshot as a thumbnail type and we'll
  696. 19:18say 60 by 60 for the size so now all
  697. 19:20that is good what we then want to do is
  698. 19:22we want to call this function inside of
  699. 19:24our begin trade function and pass
  700. 19:25through the Target now another thing
  701. 19:27that we need to consider is we need to
  702. 19:28actually modify or update our own labels
  703. 19:30as well when the script starts up so
  704. 19:32that it displays our own name our Avatar
  705. 19:34picture and stuff like that so what
  706. 19:35we'll do is towards the bottom of the
  707. 19:36script we're going to create a new
  708. 19:37function and this is going to be called
  709. 19:39startup and inside of here we're
  710. 19:40basically going to do the exact same
  711. 19:41thing that we did with their variables
  712. 19:43so we're literally going to copy and
  713. 19:45paste these right down here inside of
  714. 19:46their label we'll say our label instead
  715. 19:48of their Avatar will say our Avatar and
  716. 19:50now of course we're going to use
  717. 19:51player.user ID because we're our own
  718. 19:53player and then player dot display name
  719. 19:55just like that and we'll just call the
  720. 19:56starter function right below it because
  721. 19:58we want the function run as soon as the
  722. 19:59script starts up and we only ever have
  723. 20:01to call this function one time now
  724. 20:02another thing that we want to do is
  725. 20:03whenever we actually accept a trade and
  726. 20:05whenever our GUI is open we want to
  727. 20:07generate all the pets that are in our
  728. 20:09pet inventory and display them in our
  729. 20:11trade inventory as well now we're
  730. 20:12actually going to go look back inside of
  731. 20:14our pet inventory GUI and look inside of
  732. 20:16the manager and the Handler and we're
  733. 20:18going to use some of the components from
  734. 20:19here to do exactly that so for instance
  735. 20:21if we look inside of the Handler inside
  736. 20:23of the pet inventory we have this
  737. 20:25module.generate pet function right here
  738. 20:27which accepts a pet table and then this
  739. 20:29is the function that we actually use to
  740. 20:30modify the template into a pet inside of
  741. 20:32our pet inventory so now rather than
  742. 20:34making modifications to this specific
  743. 20:36method right here we're just going to
  744. 20:37copy some of the main things that this
  745. 20:39function does like all this stuff right
  746. 20:40here then back inside of the manager of
  747. 20:42our trade GUI we'll create a brand new
  748. 20:43function called generate pet and this
  749. 20:46will accept a pet table which is of
  750. 20:48course going to be a pet table we'll
  751. 20:49then paste the code from a pet Handler
  752. 20:50script into here so that we can start
  753. 20:52working with it now the template that
  754. 20:53we're cloning is the template that is
  755. 20:55inside of our container right here and
  756. 20:57this template is literally the exact
  757. 20:59same template that is inside of the pet
  758. 21:01inventory so all the children that are
  759. 21:02inside of this template are literally
  760. 21:04the exact same so we really don't have
  761. 21:06to modify too many things here what we
  762. 21:07do need to change is we need to replace
  763. 21:09pet with pet table as that's the new
  764. 21:11variable name so we're just going to do
  765. 21:12that we also need to create a pet models
  766. 21:14variable and we can grab that directly
  767. 21:16from this Handler right here so let's go
  768. 21:18ahead and grab that variable and we'll
  769. 21:19put it right here and now we also need
  770. 21:21to get this module as well which we
  771. 21:23actually can do that easily we'll go
  772. 21:25back to the topic we'll say local pets
  773. 21:27Handler equals require script dot parent
  774. 21:29dot parent dot pet inventory dot Handler
  775. 21:32and now we can actually use that when we
  776. 21:34need to so we'll go back down to the
  777. 21:35function and instead of saying
  778. 21:36module.generate viewport frame we'll say
  779. 21:38pet handler.generate viewport frame and
  780. 21:40finally at the end of this function we
  781. 21:41want to return the Clone so that we can
  782. 21:43make some more modifications to it in
  783. 21:45another function now we'll call this
  784. 21:46function every single time we want to
  785. 21:47generate a brand new pet if that's for
  786. 21:49our own pets or for the player that
  787. 21:50we're trading with as well now the next
  788. 21:52thing we need to do is we need to
  789. 21:53actually generate all of our own pets so
  790. 21:55what we're going to do is we're going to
  791. 21:56create a function local function
  792. 21:58generate pets and this is referring to
  793. 22:00our own pets so you could even rename
  794. 22:01this to generate our pets or something
  795. 22:03like that that'll probably make it less
  796. 22:05confusing now if we look at the manager
  797. 22:06that's inside of her pet inventory how
  798. 22:08do we generate our own pets well we go
  799. 22:10inside of the player inside of the
  800. 22:11player's pet inventory folder and then
  801. 22:13we get the children and then we use this
  802. 22:15pet instance to table method and we get
  803. 22:17a pet table and then we also call the
  804. 22:18generate pet method from the Handler as
  805. 22:20well and that's how we generate the pet
  806. 22:22for our pet inventory GUI so we can
  807. 22:24literally just copy this go back inside
  808. 22:26of our manager inside of this function
  809. 22:27so four underscore pet in ipears
  810. 22:29player.pet in Missouri get children do
  811. 22:31then we'll create a variable for the pet
  812. 22:33table instead of saying Handler we'll
  813. 22:34say pets Handler and then we've got the
  814. 22:36pet table from the instance and now
  815. 22:38instead of calling pet handler.genery
  816. 22:40pet we'll call the generate pet function
  817. 22:41that we just created now one thing that
  818. 22:43we also need to do inside the generate
  819. 22:44pet function is we actually need to
  820. 22:45return the Clones so that we can use
  821. 22:48that clone inside of this function right
  822. 22:49here so say local pet equals generate
  823. 22:52pet pass through the pet Table and there
  824. 22:53we go and now the reason that we're
  825. 22:55getting the pet return back to us is
  826. 22:56because we actually need to listen for
  827. 22:58when the is actually clicked on because
  828. 23:00then we need to perform a specific
  829. 23:01action on it we're not going to get too
  830. 23:03deep into that yet I just wanted to make
  831. 23:04sure that we generate all the pets so
  832. 23:06that it populates our trade GUI and we
  833. 23:08can actually see all the pets that we're
  834. 23:09able to trade with anyways we need to
  835. 23:11make sure that we call to generate our
  836. 23:12pets function whenever we actually start
  837. 23:14the trade so before we even update the
  838. 23:16labels we will generate our pets just
  839. 23:18like that now another thing that we need
  840. 23:19to think about is that considering we're
  841. 23:21generating the pets every single time we
  842. 23:22start a trade we need to make sure that
  843. 23:24we actually clear out all the pets
  844. 23:26anytime the trade ends otherwise we're
  845. 23:28gonna have a memory leak and we're gonna
  846. 23:29have way more pests displayed inside of
  847. 23:31our trade gy than we actually own so
  848. 23:33let's go ahead and create a brand new
  849. 23:34function towards the top of our script
  850. 23:35and this is going to be called cleanup
  851. 23:37and what we're going to do with this is
  852. 23:38we're going to Loop through each of the
  853. 23:39different containers so for instance
  854. 23:41we're going to Loop through this
  855. 23:42container we're going to Loop through
  856. 23:43our offer and their offer because all
  857. 23:45three of these variables are places that
  858. 23:47pets can be stored because remember by
  859. 23:49default our pets are in our container
  860. 23:51whenever we click on a pet inside of our
  861. 23:53container we want to put that into our
  862. 23:54offer so the player sees the pet and if
  863. 23:56a player selects any of their pets for
  864. 23:58trade it'll go into their offer as well
  865. 24:00and every single time a trade ends we
  866. 24:02always want to make sure that we clean
  867. 24:03all these containers out and there's no
  868. 24:04pets in there so that when the GUI is
  869. 24:06displayed next time we won't see any
  870. 24:08pets there so we'll first Loop through
  871. 24:09our offers so we're gonna say for
  872. 24:10underscore child in high pairs our offer
  873. 24:13get children do and then we want to
  874. 24:15check if the child is a text Bun then
  875. 24:17we're just gonna destroy it so then we
  876. 24:18can copy and paste this for Loop and
  877. 24:20we're gonna do the exact same thing for
  878. 24:22their offer then we can copy and paste
  879. 24:23it again and we're going to do something
  880. 24:25similar for the container but we have to
  881. 24:26modify it slightly so we're gonna check
  882. 24:28if the child is a text button and
  883. 24:31child.visible so this would mean that
  884. 24:33the child is visible and the reason that
  885. 24:35we're doing that is because we don't
  886. 24:36want to delete this default template
  887. 24:37right here because we need that so that
  888. 24:39we can create clones of it and this
  889. 24:41template should never be visible anyway
  890. 24:42because it's the default one so going
  891. 24:44through this Loop it'll only delete all
  892. 24:46the visible ones which would be the
  893. 24:47Clones of it now the next thing to think
  894. 24:49about is when are we actually going to
  895. 24:50call this cleanup function well we would
  896. 24:52call it whenever we actually click on
  897. 24:54the decline button so we're gonna go
  898. 24:55towards the bottom of our script and
  899. 24:56we'll add the decline button so d
  900. 24:58decline.mousebond one click connect
  901. 25:00function and now whenever we click the
  902. 25:02decline button we should actually use a
  903. 25:03remote event to communicate with the
  904. 25:05server and tell the server hey we no
  905. 25:06longer want to trade we've just declined
  906. 25:08the trade so we're going to go back into
  907. 25:09our replicated storage inside a remote
  908. 25:11folder and add a brand new remote event
  909. 25:13and we'll rename this to trade declined
  910. 25:15So then whenever we click on the decline
  911. 25:17Bond we're going to say remotes.trade
  912. 25:18declined by your server and now that
  913. 25:20we've declined it we'll then call our
  914. 25:22cleanup function and we'll disable the
  915. 25:23screen GUI so screen GUI dot enabled
  916. 25:26equals false so that we no longer see it
  917. 25:28now before we're actually able to start
  918. 25:29testing some of this stuff out we need
  919. 25:31to go back inside of the trader Quest UI
  920. 25:33inside of the manager and when we fire
  921. 25:34the accept trade request for mode event
  922. 25:36instead of passing through the trader we
  923. 25:38actually need to pass through the
  924. 25:39trader.user ID because inside of our
  925. 25:41trade script we are looking for the
  926. 25:42Target ID and not actually the target
  927. 25:44player and now that we have that all
  928. 25:46good we should be able to test this out
  929. 25:47so sending a trade with myself and then
  930. 25:49accepting it we can see that this pops
  931. 25:51up and we are trading with ourselves so
  932. 25:53we have two of my avatars right here and
  933. 25:55two of my names displayed with offer as
  934. 25:57well now no pets are being displayed in
  935. 25:59the GUI but I'm pretty sure that's
  936. 26:00because we don't have any pets as is so
  937. 26:03what I'll do real quick is I'll hatch a
  938. 26:04couple of pets so now that we have pets
  939. 26:06when we actually go to trade with
  940. 26:07ourselves and we accept the trade our
  941. 26:09pets are displayed now it clearly shows
  942. 26:11two times the normal amount of pets that
  943. 26:13we actually have the reason for this is
  944. 26:15because we're trading with ourselves so
  945. 26:16inside of our trade script when we fire
  946. 26:18off the trade began remote event we're
  947. 26:20firing this to ourselves twice because
  948. 26:22we're both the player and the target
  949. 26:23that's why there are two rows of these
  950. 26:25pets so remember this is all just for
  951. 26:27testing but this means that it is
  952. 26:29working as it should be which is good
  953. 26:31additionally we've partially scripted
  954. 26:32decline button so let's go ahead and
  955. 26:33click that and see if that works and it
  956. 26:35seems like it does work it most likely
  957. 26:36fires off the remote event that we
  958. 26:38haven't actually tracked on the server
  959. 26:39yet and it also does hide the GUI as
  960. 26:41well which means that things are going
  961. 26:42good now with that trade decliner remote
  962. 26:44event that we just created let's go back
  963. 26:45over the server and start scripting this
  964. 26:47remote event out first things first down
  965. 26:49here we actually need to get the remote
  966. 26:50so remotes.trade decline dot on server
  967. 26:53event connect and then we'll connect
  968. 26:55that to a function let's go ahead and
  969. 26:56create that brand new function up here
  970. 26:58so look function trade declined and this
  971. 27:01is going to accept a player so then we
  972. 27:03can connect that event to that function
  973. 27:04right there and now looking back at her
  974. 27:06is trading function is trading will
  975. 27:08actually return a value which will be
  976. 27:10the Target that the player is currently
  977. 27:11trading with if they're trading with any
  978. 27:13player at all and that's how you can
  979. 27:15actually get who a player is trading
  980. 27:17with by just their player alone so
  981. 27:18there's a local Target ID equals is
  982. 27:21trading and we're gonna pass through our
  983. 27:22player now if we don't get any Target ID
  984. 27:24return to us then that means that this
  985. 27:26player isn't actually trading with
  986. 27:27anybody so we'll just stop the function
  987. 27:29right there but if we do get a Target ID
  988. 27:30return to us then we also want to get
  989. 27:32that specific player as well so local
  990. 27:34Target equals players get player by user
  991. 27:36ID and pass through the target ID now we
  992. 27:39need to index the trading table with the
  993. 27:41player.user ID and we're going to set
  994. 27:43that to nil so that we know that this
  995. 27:44player is no longer trading with anybody
  996. 27:46and we also need to do the same thing as
  997. 27:47well for the Target so we're going to
  998. 27:49say Target ID and then set that to nil
  999. 27:51as well and then finally we'll use the
  1000. 27:53trade decline remote and we're going to
  1001. 27:55actually fire client and we're gonna
  1002. 27:56send that to the Target and we'll pass
  1003. 27:58through the player.user ID to that
  1004. 28:00remote as well and also we're only going
  1005. 28:01to do this if we actually find the
  1006. 28:03target so if Target then then we will
  1007. 28:05call that remote just like that so now
  1008. 28:07we need to go back inside of our manager
  1009. 28:09script right here and we need to listen
  1010. 28:10for that remote event to be fired so
  1011. 28:11we're going to say remote Stop Trade
  1012. 28:13declined dot on client event connect and
  1013. 28:16we're going to connect that to a brand
  1014. 28:17new function so let's go ahead and
  1015. 28:18create that function right here so local
  1016. 28:20function Decline and inside of this
  1017. 28:21function we're actually going to call
  1018. 28:22the cleanup function and we're going to
  1019. 28:24say screen GUI dot enabled equals false
  1020. 28:26because we don't want them to see it
  1021. 28:28anymore similar to exactly like what we
  1022. 28:29do down here so we could delete these
  1023. 28:31two lines of code and we can just call
  1024. 28:32this decline function right here just
  1025. 28:34like that and also whenever this trade
  1026. 28:35decline remote is fired we can also call
  1027. 28:37the decline function as well just like
  1028. 28:39that so now whenever we decline a trade
  1029. 28:41we're going to tell the server hey we
  1030. 28:42just declined a trade and then we're
  1031. 28:43going to clean this up and hide the GUI
  1032. 28:45and now from the server side if a player
  1033. 28:47that we're trading with declines the
  1034. 28:48trade then they're going to tell the
  1035. 28:49server and then the server is going to
  1036. 28:51tell us so then if we're told that the
  1037. 28:53other player has declined the trade then
  1038. 28:54we're going to call this decline
  1039. 28:55function right here clean up the GUI and
  1040. 28:57then hide it as well the next thing
  1041. 28:58we're going to be working on is
  1042. 28:59selecting specific pets whenever we
  1043. 29:01click on the pet that is stored with
  1044. 29:02inside of the container variable we want
  1045. 29:03to put that pet inside of our offer
  1046. 29:05which is another container and now if we
  1047. 29:07click on a pet that's already inside of
  1048. 29:08our offer we want to put that pet back
  1049. 29:10inside of the container because we would
  1050. 29:12effectively be unselecting or selecting
  1051. 29:14the pet based on their location so we're
  1052. 29:16going to go back to where generate our
  1053. 29:17pets function now inside of here we have
  1054. 29:19a variable which is pet and that's
  1055. 29:20actually the clone of the template
  1056. 29:21returned back to us so that would be a
  1057. 29:23text button now what we're going to do
  1058. 29:24is we're actually going to use a set
  1059. 29:26attribute method on this text button and
  1060. 29:28that should be that we're going to be
  1061. 29:29setting is called selected and by
  1062. 29:31default we're going to set that to false
  1063. 29:32because when we first generate the pets
  1064. 29:34they're not automatically selected by
  1065. 29:36default now whenever we click on a pet
  1066. 29:37we can then use this attribute to see if
  1067. 29:40this pet is selected and if it is
  1068. 29:41selected then we obviously want to
  1069. 29:43unselect it or if it's not selected then
  1070. 29:44we obviously want to select it so we're
  1071. 29:46going to say if pet get attribute
  1072. 29:48selected then so if the pet is selected
  1073. 29:50then what do we want to do well we want
  1074. 29:52to deselect it else select it so what
  1075. 29:54does deselecting and selecting entail
  1076. 29:56let's first work on selecting a pen when
  1077. 29:58we select the pet what do we want to do
  1078. 29:59well we want to actually set its
  1079. 30:01attribute of selected to be true so that
  1080. 30:04we know that it's selected now and we
  1081. 30:05also want to set the parent of this text
  1082. 30:07button to our offer because this is a
  1083. 30:09pet that we've now selected to be traded
  1084. 30:11to the other player additionally we're
  1085. 30:13going to need to use another remote
  1086. 30:14event so we'll go inside the remotes
  1087. 30:16folder and this remote event is going to
  1088. 30:17be called trade select pet so we'll then
  1089. 30:19call that remote so trade select pet
  1090. 30:21fire server and we're going to fire it
  1091. 30:23with the pettable dot uuid so we're
  1092. 30:25going to tell the server hey this is the
  1093. 30:27uuid of one of our pets that we want to
  1094. 30:29trade and then from the server we're
  1095. 30:30going to tell the other player that this
  1096. 30:32is the pet that the other player wants
  1097. 30:33to trade with you now we finished
  1098. 30:34everything for the selecting so that's
  1099. 30:36all handled perfectly what we can do to
  1100. 30:38make this a little bit easier to
  1101. 30:39understand later in the future if we
  1102. 30:40ever come back to this script is we'll
  1103. 30:42actually create a function for this so
  1104. 30:43we'll say local function select pet and
  1105. 30:45the arguments that we need to get passed
  1106. 30:47through to here is the button which is
  1107. 30:48of course going to be a text button and
  1108. 30:50the pet table which is of course going
  1109. 30:51to be that pet table that we've normally
  1110. 30:53been getting and I will just take all of
  1111. 30:54this and put that directly into here and
  1112. 30:56now instead of saying pet we are going
  1113. 30:58going to say button button just like
  1114. 31:00that and we'll use the pet table as
  1115. 31:01normal so then we just call the select
  1116. 31:02pet function right here and we can pass
  1117. 31:04through the pet which is going to be the
  1118. 31:05text button and the pet table which is
  1119. 31:07of course going to be the pet table just
  1120. 31:08like that so now we can basically copy
  1121. 31:10and paste this select pet function and
  1122. 31:12we can rename this from select pet to
  1123. 31:14deselect pet now we're basically just
  1124. 31:16going to do the opposite so instead of
  1125. 31:17setting this attribute to true we're
  1126. 31:18actually going to set the false instead
  1127. 31:20of setting the parent of the button to
  1128. 31:22our offer we're going to set it back to
  1129. 31:23the container and then instead of firing
  1130. 31:25off this remote we're actually going to
  1131. 31:26create another remote and we're going to
  1132. 31:28rename this from select to deselect so
  1133. 31:30then we'll fire off the trade deselect
  1134. 31:32pet and that'll once again be with the
  1135. 31:34uuid and now we have to actually call
  1136. 31:36this function right here so deselect pet
  1137. 31:38with the pet and the pet table just like
  1138. 31:40that now we can go into our game and
  1139. 31:42test this out so we're going to go ahead
  1140. 31:43and accept our own trade and then let's
  1141. 31:45go ahead and say for instance click on
  1142. 31:47the bunny now that we see that we've
  1143. 31:48clicked on the bunny the bunny is now
  1144. 31:49inside of our offer if we click on that
  1145. 31:51bunny again it is now removed from our
  1146. 31:53offer exactly as it should be so we can
  1147. 31:55click a couple of different pets we can
  1148. 31:56see they fill inside of this contain
  1149. 31:58enter if we click them again we are
  1150. 31:59unselecting them which means this is
  1151. 32:01working perfectly now let's go back over
  1152. 32:03to the server script and start working
  1153. 32:04on the server side of this stuff now we
  1154. 32:06have another remote to work with so
  1155. 32:07we're going to go ahead and copy this
  1156. 32:09and we're going to say trade select pet
  1157. 32:11instead of the trade Decline and we're
  1158. 32:12going to connect that to a brand new
  1159. 32:14function and let's go ahead and start
  1160. 32:15creating this function so local function
  1161. 32:17trade select pet of course the first
  1162. 32:19argument is going to be a player and the
  1163. 32:21second one is going to be the uuid of
  1164. 32:22the pet which is going to be a strength
  1165. 32:24now we're going to copy some of our
  1166. 32:25standard variables like this target ID
  1167. 32:27right here and then also getting the
  1168. 32:28target so first of all we need to check
  1169. 32:30if the player is trading with somebody
  1170. 32:31and then also get the target as well and
  1171. 32:33if we're not able to find a Target then
  1172. 32:35we just want to return end because that
  1173. 32:37player is no longer in the game the next
  1174. 32:38thing we need to do is we need to make
  1175. 32:39sure that the player actually owns the
  1176. 32:41pet of the uuid that they're telling us
  1177. 32:43that they want to trade so we're going
  1178. 32:44to say local pet instance equals player
  1179. 32:47and we can access this by going into
  1180. 32:48their pet inventory and then we're gonna
  1181. 32:50say fine first child and the name of the
  1182. 32:52child is going to be the uuid that the
  1183. 32:54player passed through us so if not pet
  1184. 32:56instance then reach turn and because the
  1185. 32:59player does not actually own that pet we
  1186. 33:01then need to convert the information
  1187. 33:02from the pet instance into a table which
  1188. 33:04we call a pet table now if you don't
  1189. 33:06remember exactly how we do this we can
  1190. 33:08go inside of our script and we want to
  1191. 33:09go inside of the pet Handler because
  1192. 33:11inside of the pet Handler we have this
  1193. 33:12pet instance to table function and this
  1194. 33:14is all we do now since this script is on
  1195. 33:16the client side we can't actually use
  1196. 33:17this on the server side so all we're
  1197. 33:18going to do is we're going to copy this
  1198. 33:19bring it into the trade script paste it
  1199. 33:21just like this and now we can modify it
  1200. 33:23slightly so we need the uuid we need the
  1201. 33:25ID and we need the Rarity but we
  1202. 33:27actually don't care about it being
  1203. 33:28equipped or not and then instead of the
  1204. 33:29pet variable it's actually the pet
  1205. 33:31instance variable so just like that and
  1206. 33:33now we have the uuid the ID and the
  1207. 33:35Rarity all inside of this new pet table
  1208. 33:36right there and we're actually rename
  1209. 33:38this from new pet to pet table there we
  1210. 33:40go and now back at the top of our script
  1211. 33:41we're going to create a new table so
  1212. 33:43that we can track all the pets that the
  1213. 33:44player has selected so we're going to
  1214. 33:46say local trade selected equals blank
  1215. 33:48table now going back inside of the trade
  1216. 33:50select pet function we're gonna say
  1217. 33:51local selected pets equals trade
  1218. 33:54selected and we're gonna index that with
  1219. 33:56the player.userid and now if not not
  1220. 33:58selected pets so if the player doesn't
  1221. 34:00have any selected pets currently what
  1222. 34:01we'll do is we'll create a table for
  1223. 34:03that player so we're going to say trade
  1224. 34:04selected index to have the player die
  1225. 34:06user ID equals a brand new table and
  1226. 34:08then we're going to say table dot insert
  1227. 34:10trade selective indexing that with the
  1228. 34:11player dot user ID and then we'll just
  1229. 34:13add the uuid to this so that we can keep
  1230. 34:15track of all the uuids of the pests that
  1231. 34:17each player has selected when they're
  1232. 34:19traded then finally we'll use the
  1233. 34:20remotes.trade select pet and fire that
  1234. 34:24to the target with the pet table that we
  1235. 34:26just created so that means we have to go
  1236. 34:27back inside of our manager script and
  1237. 34:29listen to whenever this remote event is
  1238. 34:30fired so we're gonna say remotes.trade
  1239. 34:32select pet on client event and now we
  1240. 34:34actually have to do something and what
  1241. 34:36are we gonna do well we'll go under the
  1242. 34:37generate our pets function and we're
  1243. 34:39going to say local function Trader
  1244. 34:41selects pet and what are we gonna get
  1245. 34:43we're gonna get the pet table then what
  1246. 34:45are we gonna do we're actually just
  1247. 34:46gonna call the generate pet function and
  1248. 34:47we're going to pass through the pet
  1249. 34:48table now we need to modify this
  1250. 34:50function slightly the reason for that is
  1251. 34:52because inside of this function we are
  1252. 34:53setting the parent right here to
  1253. 34:54container and of course if a Trader
  1254. 34:56selects the pet we don't want the parent
  1255. 34:57it to be the container we want the
  1256. 34:59parent of that pet to be their offer so
  1257. 35:01what we need to do is inside of the
  1258. 35:03generate pet function we need to accept
  1259. 35:04another argument and that's going to be
  1260. 35:06an instance so now instead of setting
  1261. 35:08the parent to container by default we're
  1262. 35:10going to set that to the parent argument
  1263. 35:11which is passed to this function so now
  1264. 35:13we've got to make sure that every single
  1265. 35:14time we call this function we pass
  1266. 35:15through the correct parent so originally
  1267. 35:17inside of our generate our Pet's
  1268. 35:18function we always wanted this parents
  1269. 35:20into the Container so we're going to
  1270. 35:21make sure that we pass that through in
  1271. 35:22this argument and now inside of the
  1272. 35:24trader selects pet function we're going
  1273. 35:26to need to also pass through a parent as
  1274. 35:27well and that's going to be for their
  1275. 35:28offer so then we're going to go ahead
  1276. 35:30and connect the trade select the pet
  1277. 35:31remote event to this function right here
  1278. 35:33next we have to start scripting the
  1279. 35:34deselect pet on the server side as well
  1280. 35:36so going back to the server we also need
  1281. 35:38to make sure that we point the trade
  1282. 35:39select pet remote event at the trade
  1283. 35:41select pet function that we just created
  1284. 35:42and then we can add the new remote event
  1285. 35:44instead of Select it's deselect pet so
  1286. 35:46then we're going to create another
  1287. 35:47function local function trade deselect
  1288. 35:50pet and that'll accept a player and a
  1289. 35:52uuid as a string and now let's make sure
  1290. 35:55that we hook that event to that function
  1291. 35:57just like that and we're going to copy a
  1292. 35:58lot of the basic variables that we
  1293. 35:59created in our trade select pet function
  1294. 36:01just like that we're also going to get
  1295. 36:03the selected pets as well certain State
  1296. 36:04local selected pets equals trade
  1297. 36:07selected indexed out the player.userid
  1298. 36:09and if we don't find any selected pets
  1299. 36:11from that player then we just wanted to
  1300. 36:13return n because they can't deselect any
  1301. 36:15pets if they don't have any pets
  1302. 36:16selected right so now that we've done
  1303. 36:18that we should be able to actually find
  1304. 36:19the pet so we're gonna say local pet
  1305. 36:21equals and then we're gonna say table
  1306. 36:22dot find and we're going to look inside
  1307. 36:25of the selected pets table and we're
  1308. 36:26going to search for the uuid that the
  1309. 36:28player has passed to us now if we're not
  1310. 36:30able to actually find the pet then we're
  1311. 36:32gonna return and because that means that
  1312. 36:33the player doesn't have that pet
  1313. 36:34selected but if we are able to find that
  1314. 36:36pet then what we're going to do is we're
  1315. 36:38going to say table down remove and we're
  1316. 36:39gonna remove it from the selected pets
  1317. 36:41and we're going to remove pet now that
  1318. 36:42we've removed that pet from that table
  1319. 36:44we also need to notify the other player
  1320. 36:46as well so we're going to say local
  1321. 36:47trade deselect pet fire client Target
  1322. 36:49uuib so we're gonna remove that pet from
  1323. 36:51the server table and we're also going to
  1324. 36:53tell the client so they can remove it
  1325. 36:54from their GUI as well so going back
  1326. 36:56inside of the manager we now need to
  1327. 36:57handle the Trader deselect pet so we're
  1328. 36:59going to go back down here and add the
  1329. 37:00new remote event so trade deselect pet
  1330. 37:03and we'll create a function just below
  1331. 37:05this one right here so Trader select pet
  1332. 37:07to Trader deselect pet and then let's
  1333. 37:09make sure that we set this function to
  1334. 37:11that remote event just like that and now
  1335. 37:12what do we want to do whenever a player
  1336. 37:14deselects a pet well we simply want to
  1337. 37:15delete that pet from their container so
  1338. 37:17we're going to say local instance equals
  1339. 37:19their offer find first child oh and also
  1340. 37:21we get past the uuid to this function so
  1341. 37:24we're going to look for the uuid and if
  1342. 37:26that instance exists then what we're
  1343. 37:27going to do is we're going to destroy it
  1344. 37:30and it's as simple as that so now we can
  1345. 37:31start testing some more complex things
  1346. 37:33and we're actually going to want to
  1347. 37:34launch two different clients so that we
  1348. 37:36can simulate players training with each
  1349. 37:37other because of course you need two
  1350. 37:39players to do that if you guys are
  1351. 37:40unfamiliar with how to launch multiple
  1352. 37:41clients in studio what you're going to
  1353. 37:43do is you're going to go to test and
  1354. 37:44inside of here you have local server
  1355. 37:46make sure you click on that and then you
  1356. 37:48can select the amount of clients that
  1357. 37:49you actually want to launch zero one two
  1358. 37:52three four five all the way up to eight
  1359. 37:54now we're gonna set that to two and then
  1360. 37:56you can either hit F7 or click this
  1361. 37:58start button right here at the hotkeys
  1362. 37:59F7 and then wait for it to fire up now
  1363. 38:01after it does fire up you'll actually
  1364. 38:03have three different screens open this
  1365. 38:05one in front is actually from the server
  1366. 38:06side so we can see two things on the
  1367. 38:08server have been printed while these two
  1368. 38:10are their own client now we're going to
  1369. 38:11go ahead and test out trading with them
  1370. 38:12so we're gonna go ahead and send a trade
  1371. 38:14request and we can see the trade request
  1372. 38:16does actually appear there is no
  1373. 38:17thumbnail though because these players
  1374. 38:19aren't actually real players as we can
  1375. 38:21see their name is player so whenever we
  1376. 38:23use the get thumbnail method on it these
  1377. 38:25players don't actually exist so they
  1378. 38:26don't have a thumbnail anyways though we
  1379. 38:28can go ahead and accept that trade now
  1380. 38:29we can see that we are trading with that
  1381. 38:31player and then let's go ahead and
  1382. 38:32select the pet and when we select the
  1383. 38:34pet on this side we can see that it's
  1384. 38:35also selected on this side as well which
  1385. 38:37means that this is working perfectly now
  1386. 38:39if we deselect this pet we can also see
  1387. 38:41that it's removed on this player's side
  1388. 38:42of the screen as well so we are seeing
  1389. 38:44accurate trades working in action which
  1390. 38:46means this is working perfectly
  1391. 38:48currently the next thing we need to work
  1392. 38:49on is accepting a trade so let's go
  1393. 38:51towards the bottom of our script right
  1394. 38:52where we listen for the decline button
  1395. 38:54to be clicked let's go ahead and listen
  1396. 38:55for the accept button to be clicked now
  1397. 38:57the except one actually needs to perform
  1398. 38:58of one of two actions if we've already
  1399. 39:01clicked the accept button the text will
  1400. 39:03actually change the display unaccept and
  1401. 39:05if the button displays unaccept we need
  1402. 39:06to perform a different action than if it
  1403. 39:08displays except so we'll say if except
  1404. 39:10dot text equals accept then and what do
  1405. 39:13we want to do well we actually need to
  1406. 39:14tell the server that we accept this
  1407. 39:16trade so what we're going to do is we're
  1408. 39:17going to create a new remote event and
  1409. 39:19we're going to say trade accept so then
  1410. 39:21we're going to say remotes dot trade
  1411. 39:23accept fire server and then we're gonna
  1412. 39:25need to modify our accepted which is
  1413. 39:27going to be a text label and we need to
  1414. 39:29set the visibility of that to true we
  1415. 39:30also want to modify the text of the
  1416. 39:32accept button from accept to unaccept
  1417. 39:34and now that's all good now what happens
  1418. 39:36if we actually want to unaccept the
  1419. 39:38trade well we'll basically do the
  1420. 39:39opposite of what we just did here so we
  1421. 39:41can go ahead and copy and paste this and
  1422. 39:43we're also going to create another
  1423. 39:44remote instead of saying trade except
  1424. 39:45we're going to say trade on accept so
  1425. 39:47we'll fire off the trade on accept
  1426. 39:49remote just like that we'll set the
  1427. 39:51visibility of our accepted to false and
  1428. 39:53then the text should not be unaccept it
  1429. 39:55should Now display except now one thing
  1430. 39:57that we also need to consider is imagine
  1431. 39:58if two players put a bunch of pets up
  1432. 40:00for trade and one of them clicked accept
  1433. 40:02now imagine if the other player who
  1434. 40:04hasn't accepted yet starts taking out
  1435. 40:05all of their pets that they were about
  1436. 40:07to trade and quickly clicks the accept
  1437. 40:09button before the other player realizes
  1438. 40:10this well then the other player would
  1439. 40:12basically feel scammed because they
  1440. 40:13didn't realize that another player could
  1441. 40:15remove all their pets and they would
  1442. 40:16still be accepted so what I'm
  1443. 40:18effectively saying is that anytime we
  1444. 40:19actually select or deselect a pet we
  1445. 40:21most likely want to perform this
  1446. 40:23unaccept action right here as well so
  1447. 40:25what we're going to do is we're actually
  1448. 40:26going to make this into its own function
  1449. 40:28so we're going to take these actions and
  1450. 40:29we're going to put it towards the top of
  1451. 40:31our script so above cleanup we're going
  1452. 40:33to say local function unaccept trade and
  1453. 40:35then we'll just paste those actions
  1454. 40:36right there now inside of the generate
  1455. 40:38our pets function whenever we listen for
  1456. 40:40the pet to be clicked on we want to call
  1457. 40:41the unaccept trade function as well just
  1458. 40:43like that and then back inside of when
  1459. 40:45we're listening for the accept button to
  1460. 40:46be clicked on we want to call the
  1461. 40:47unaccept trade and the else statement as
  1462. 40:49well so with all that being done we can
  1463. 40:51hop directly into the game and start
  1464. 40:52testing out our trading system we can
  1465. 40:53click on the trade button send the other
  1466. 40:55player trade request we can go over here
  1467. 40:56and accept that request and now we have
  1468. 40:58this GUI popping up and then we can
  1469. 41:00select any of our pets that we want to
  1470. 41:01throw up for trading and we can select
  1471. 41:03some from over here as well and then if
  1472. 41:05we both hit on accept we can see that
  1473. 41:07the trade has been accepted so it looks
  1474. 41:08like this whole system is working
  1475. 41:10perfectly with all that being said we
  1476. 41:12now have a functioning trade system in
  1477. 41:14our game now as always if this video to
  1478. 41:15help you guys out or you did enjoy it
  1479. 41:16make sure you smash like button also
  1480. 41:18subscribe button and turn post
  1481. 41:19notifications on if you want to get
  1482. 41:20notified we'll upload more of Roblox on
  1483. 41:21my content of course I have a patreon if
  1484. 41:23you guys like swarmy and again access
  1485. 41:24all the games and the scripts that I
  1486. 41:25made during this video there's a link
  1487. 41:26down below description and you guys are
  1488. 41:28going to check that out with that being
  1489. 41:29said I'll see you guys in the next
  1490. 41:30episode
  1491. 41:32foreign
  1492. 41:33[Music]

About this transcript

This page contains the full transcript of Creating a Trading System! Roblox Studio Simulator Guide Episode 32 by MonzterDEV, generated from the public captions YouTube serves with the video. The transcript has 11,184 words across 1,492 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.