Creating a Trading System! Roblox Studio Simulator Guide Episode 32 — Transcript
Full transcript
- 0:00in today's episode of the simulator
- 0:01series we are going to be scripting the
- 0:03trading system and as always if you guys
- 0:04do enjoy the video where it does help
- 0:05you out make sure you smash like button
- 0:06also subscribe button and turn post
- 0:07notifications on if you want to get
- 0:09notified when I upload more of Roblox
- 0:10with all my content additionally I have
- 0:11a patreon it feels like to support me
- 0:12and gain access to all the scripts and
- 0:14the game file that I made during this
- 0:15episode there's a link down below
- 0:16description and you guys can go and
- 0:17check that out with that being said
- 0:18let's get into it we're first gonna
- 0:19start off by scripting the box so inside
- 0:21of the trade menu GUI we're gonna open
- 0:22up the bond script right here and now
- 0:24for the settings bun variable we're
- 0:25going to change around the bunch UI dot
- 0:27left buttons holder and then we can get
- 0:29the trade button right from there and of
- 0:31course we'll rename this variable from
- 0:32settings button to trade button and now
- 0:34that's all done this will open up the
- 0:35trade menu GUI so that you can send
- 0:37requests to other players for trading
- 0:39the next thing we'll be scripting is the
- 0:40trade menu what we're going to do is
- 0:41we're actually going to delete the
- 0:42button and Handler script and then we're
- 0:44going to go directly into the manager
- 0:45script and start cleaning this up a
- 0:46little bit first of all we don't need
- 0:47that settings variable and we don't need
- 0:49the exit variable neither we can also
- 0:51delete this for Loop and this exit Mouse
- 0:53button one click right there and this is
- 0:55all good now we'll start modifying the
- 0:56on and off color variable that we have
- 0:58and what we're actually going to be
- 0:59changing them too is this little button
- 1:01right here so the background color for
- 1:03this bun right here just remember
- 1:04whenever we send out a tray request to a
- 1:06player we want to modify this button to
- 1:07say pending and have a purple background
- 1:09color but if it's not pending then we
- 1:11want it to be green and have the text
- 1:12send on it so we'll rename the first
- 1:14variable from on color to send
- 1:16underscore color and then from off color
- 1:18to pending color now the pending color
- 1:20is not going to be red it's actually
- 1:21going to be a purple so something like
- 1:23that now next thing we're going to do is
- 1:24we're going to rename the generate
- 1:25setting function to generate player and
- 1:27instead of accepting a setting which is
- 1:29going to be a table it'll actually
- 1:30accept a player which is of course a
- 1:32player now let's go inside of the frame
- 1:33inside of the container and inside of
- 1:35the template right here so that we can
- 1:37see all the components it has and what
- 1:38we're going to do is we're just going to
- 1:39delete most of the stuff in this
- 1:41function right here besides cloning and
- 1:42then also parenting it to The Container
- 1:44next what we're going to do is we're
- 1:45going to set the Clone dot name so the
- 1:48player.user ID so that we can easily
- 1:50identify this template then what we want
- 1:51to do is we want to modify the player
- 1:53which is actually a text label so we're
- 1:54going to set the text of the player text
- 1:56label to the player dot display name
- 1:58next inside of the Clone we have have
- 2:00the Avatar which is an image label and
- 2:02we're actually going to set this image
- 2:03to a thumbnail of the player which
- 2:05Roblox will actually provide for us so
- 2:06the way we get this is by using the
- 2:08player's service and then we call a
- 2:09method called get user thumbnail async
- 2:12and what we need to pass through here is
- 2:13the player's user ID and we also need to
- 2:15pass through an enum. thumbnail type so
- 2:18that we have to say the type of
- 2:19thumbnail that we want and we actually
- 2:20want the headshot for this and we also
- 2:22have to pass through a thumbnail size to
- 2:24this as well and for this because it is
- 2:26relatively small we'll probably want a
- 2:2748 by 48 but if you want something
- 2:29larger you can request that as well and
- 2:30then we also need to make sure that the
- 2:32visibility is set to true so we can
- 2:33actually see this and then we need to
- 2:35listen for whenever the button on this
- 2:36is actually clicked so clone dot button
- 2:38dot mouse Bond one click and connect
- 2:40that to an anonymous function now let's
- 2:42consider when we actually want to call
- 2:43this function we want to call this
- 2:44function whenever a player joins the
- 2:45game or leaves the game but also when we
- 2:48first join the game we also need to get
- 2:50all the players that are in our game
- 2:51currently and we need to generate a
- 2:52template for them so we can first start
- 2:54off by using a for Loop and we can say
- 2:56four underscore player in I pairs
- 2:58players get children do and all we have
- 3:00to do is call the generate player
- 3:01function and pass through the player and
- 3:03we also want to do this whenever a
- 3:04player is added to the game so
- 3:05players.player added connect function
- 3:07player and then we're just going to call
- 3:08the function once again and pass through
- 3:10the player now whenever a player leaves
- 3:11the game we actually want to delete the
- 3:13template so players that player removing
- 3:15connect function player and now we need
- 3:18to remove it from the container so the
- 3:19way we can do this is by saying local
- 3:20player template equals container fine
- 3:23first child
- 3:25player.userides if we find that template
- 3:27then what we'll do is we'll say player
- 3:30template destroy and I spelled that
- 3:31wrong but there we go now one thing is
- 3:33that we already have a variable called
- 3:34player in lowercase so what we're going
- 3:36to do is we're actually going to rename
- 3:37that variable from player to local
- 3:39player so we have no confusion with this
- 3:41variable right here and the variable
- 3:42called player inside of our other
- 3:44functions as well now the only time that
- 3:45we actually really need to use the local
- 3:47player is when we call this function and
- 3:49what we're going to do is we're going to
- 3:49say if player equals local player then
- 3:52return n and the reason we're doing that
- 3:54is because we don't want to actually
- 3:56generate a template for our own player
- 3:58because obviously we can't trade with
- 3:59ourselves elves so whenever this
- 4:00function is called if it is our player
- 4:02then we don't want to generate anything
- 4:03so we'll just return otherwise it'll
- 4:05generate a template for whatever player
- 4:06we pass through to here now whenever the
- 4:07button is clicked inside of the template
- 4:09we want to actually do something and
- 4:10what we want to do is we want to send a
- 4:11request to another player to see if they
- 4:13would like to trade with us now the way
- 4:14we're going to do this is by actually
- 4:15using a remote function so let's go
- 4:17inside of the replicated storage inside
- 4:19of the remotes folder and create a new
- 4:20remote function this remote function is
- 4:22going to be called send trade requests
- 4:25and then inside of where we're listening
- 4:26for the mouse button one click we're
- 4:27going to create a new variable called
- 4:29result and we're going to set that equal
- 4:30to the remotes dot send trade request
- 4:32invoke server and now the argument that
- 4:34we're going to pass through to here is
- 4:35the player.userid and remember this is
- 4:38referring to the player whose template
- 4:40this is not our own player now the only
- 4:42possible result that we're ever going to
- 4:43get back from this is either true or
- 4:44false so what we're going to do is we're
- 4:46going to say if result then and now we
- 4:47want to do something if there is a
- 4:49result what we want to do is we want to
- 4:50set the button so the button that we're
- 4:52listening to right now we want to
- 4:53actually set the background color 3 to
- 4:55the pending color and we want to modify
- 4:57the text of the button as well so
- 4:58button.test equals pending and now this
- 5:01is basically going to tell our player
- 5:02that the player we sent the trade
- 5:03request to is currently on cooldown so
- 5:05while it's pending we have to wait and
- 5:07we cannot send another invite so then
- 5:09what we're going to do is we're going to
- 5:10say task dot delay and this is basically
- 5:12going to say in 30 seconds reset this
- 5:14button so it no longer displays a
- 5:16cooldown now for the duration you can
- 5:17obviously pick however long you want
- 5:19players to have a trade request cooldown
- 5:20for I'm just going to say 15 and then
- 5:22we'll create Anonymous function inside
- 5:23of here and then we'll basically just do
- 5:24the opposite of what we just did so
- 5:26instead of setting the background color
- 5:27to the pending we'll set it to the send
- 5:29color because they're now able to send a
- 5:30request again and we'll set the text to
- 5:31send as well so whenever a player sends
- 5:33a trade request the button will display
- 5:34pending and then after the cooldown has
- 5:36expired then it will be reset to what it
- 5:38was originally so now that we have that
- 5:39all set up let's go ahead and actually
- 5:41comment out this if statement that we
- 5:43created right here and start up our game
- 5:44so that we can kind of see what a
- 5:46generated template looks like in case we
- 5:47missed anything and I don't know how I
- 5:49missed this Handler requirement
- 5:50statement up there but go ahead and
- 5:52delete that and then when you start your
- 5:53game you should have no issues so we can
- 5:55see when we pop into our game our Avatar
- 5:56has been generated our names right there
- 5:58and the button is set to send as as it
- 6:00should be by default we can't test the
- 6:01button clicking because we actually need
- 6:03a server to respond to a remote function
- 6:05otherwise we would test that right now
- 6:06as well now the next thing we're going
- 6:07to do is we're actually going to insert
- 6:09a local script inside of the trade
- 6:11request and we'll rename this to manager
- 6:12then what we can do is we can actually
- 6:14copy some of our standard variables from
- 6:15our other manager scripts like these
- 6:17ones right here for instance so we're
- 6:18going to get to player service we'll get
- 6:19the replicated storage we'll get the
- 6:20remotes folder and then we'll also get
- 6:22the player variable as well we also want
- 6:24to get the screen GUI and the frame we
- 6:25don't actually need a container but we
- 6:27will actually create a few more
- 6:28variables for this specific GUI Right
- 6:30Here Local message equals frame Dot
- 6:33message local Avatar equals frame dot
- 6:36Avatar local accept equals frame dot
- 6:40accept and local exit button equals
- 6:43frame dot exit button so then what we'll
- 6:45do is we'll create a new function called
- 6:46display GUI and this will accept the
- 6:48trader which is actually going to be a
- 6:50player so not us it'll be the opposite
- 6:52player and remember the GUI that we're
- 6:54working on is this one right here so a
- 6:55little notification will pop up whenever
- 6:57a player sends us a trade request so
- 6:58what we want to do first is we want to
- 7:00enable the screen GUI so the player sees
- 7:02the notification then we need to set the
- 7:03Avatar's image to the player's thumbnail
- 7:05and remember we do this by using a
- 7:07method inside of the player's service
- 7:09and the method is get user thumbnail
- 7:11async and then we'll pass through that
- 7:12player's user ID so trader.user ID enum
- 7:15headshot as the type and 100 by 100 as
- 7:18the size then for the text of the
- 7:20message we want to display the player
- 7:22dot display name and we want to combine
- 7:24that with another string and that's
- 7:25going to be has sent you a trade request
- 7:28exclamation mark and now we're actually
- 7:30going to take this string and we're
- 7:32going to turn that into a static
- 7:33variable so local message template
- 7:35equals has sent you a trade request and
- 7:37then we can put the variable right there
- 7:38just in case we come back in the future
- 7:39we can easily see what the string is for
- 7:41and we can modify now that we have this
- 7:42function set up we have to think about
- 7:43when do we actually want to call this
- 7:45we're going to be using a remote event
- 7:46and from the server whenever a player
- 7:48sends out a trade request is going to go
- 7:50to the server and then the server is
- 7:51going to see if the player can send a
- 7:53trade request to the other player and
- 7:54then we'll fire a remote event telling
- 7:56the other player that they have just
- 7:57received a request so we're going to
- 7:59create a new remote event in the remotes
- 8:00folder and we're going to rename this to
- 8:02trade request received so inside of the
- 8:05manager script we're going to listen to
- 8:06the trade request received event then
- 8:08that on client event and we're going to
- 8:09connect that to the display GUI function
- 8:11next we'll script the exabon so exabun
- 8:13dot mouse button one click connect
- 8:15function screen GUI dot enabled equals
- 8:18false simple as that now we also need to
- 8:20script the accept Button as well so
- 8:21we'll say accept.mousebond one click
- 8:23connect function and what this would do
- 8:25is whenever we click this button we of
- 8:26course want the screen's UI to not be
- 8:28enabled anywhere because we don't want
- 8:30to be able to see it and then what we
- 8:31also need to do is we need to use a
- 8:32remote event to tell the server that
- 8:34we've just accepted this trade request
- 8:35so we'll go ahead and add a new remote
- 8:36inside of here and this one's going to
- 8:38be called accept trade request so back
- 8:41inside whenever we click the accept
- 8:42button what we're going to do is we're
- 8:43going to say remote stop accept tray
- 8:45request fire server now one thing is is
- 8:48that we want to actually tell the server
- 8:49who we want to trade with or whose
- 8:51request we actually accepted in case
- 8:52multiple people senders request at once
- 8:54so what we're going to do is we're going
- 8:55to create a variable up here and that's
- 8:56going to be called Trader we're not
- 8:58going to set that to anything yet but
- 8:59whenever we call the display GUI
- 9:01function we're actually going to set the
- 9:02trader variable to the player that's
- 9:04sent to this function as well and the
- 9:05reason that we're doing that is because
- 9:06we can then easily tell the server whose
- 9:08trade request we're accepting so now
- 9:10that we've done all that work what we
- 9:11now need to do is we actually need to
- 9:12create a server script for working with
- 9:13some of these remote events so we're
- 9:15going to go into the server script
- 9:16service and we'll add a brand new script
- 9:17to this we'll rename this script to
- 9:19trade and of course inside of here we're
- 9:20going to do the player service so game
- 9:22get service players we're also going to
- 9:24the replicated storage as well and then
- 9:25from the replicated storage we're going
- 9:27to get the remotes so we'll say local
- 9:28remotes equals replicated storage dot
- 9:31remotes now remember that send trade
- 9:32request remote function that we made
- 9:33that's the first thing that we're going
- 9:35to work with so we're going to say
- 9:35remote it's not send trade request dot
- 9:38on server invoke and now we need to
- 9:39create a function up here so that
- 9:40function is going to be called send
- 9:42trade requests and that's of course
- 9:44going to accept a player this is going
- 9:46to be the person who sent it and then
- 9:47the target ID so a player's user ID that
- 9:50this player wants to trade with so then
- 9:52we can set the on invoke to the send
- 9:54trade request function just like that
- 9:55now inside this function we'll create a
- 9:57variable for the actual Target player
- 9:59and we can get the player by using the
- 10:00player's service and saying get player
- 10:02by user ID so that we can pass through
- 10:04the target ID to that method and then we
- 10:05should get returned a player but if we
- 10:07don't get returned to player then we
- 10:08just run a return end and stop the
- 10:10function right there now the next thing
- 10:11we need to do is we need to check if the
- 10:12player is on cooldown because we don't
- 10:14want our players to get spammed with
- 10:16trade requests so what we're going to do
- 10:17is we're going to create a new function
- 10:18and that's going to be called is on
- 10:20cooldown and that'll accept the player
- 10:22ID and the target ID as well which both
- 10:25will be numbers now we're actually going
- 10:26to create a table so that we can easily
- 10:28keep track of what players have sent out
- 10:30requests to others and if they're
- 10:31trading and other things like that so
- 10:33we'll create a new table and this is
- 10:34going to be called trade requests and
- 10:36that's going to be set to an empty table
- 10:37then we're going to do is inside of the
- 10:38is on cooldown function is we're going
- 10:40to create a variable called requests and
- 10:42we're going to set that equal to trade
- 10:43requests and we're going to index that
- 10:45with the player ID now if this table
- 10:46doesn't actually exist then we just want
- 10:48to return end because that would mean
- 10:50that the player is not on cooldown and
- 10:52now if the table does exist we actually
- 10:53need to find the targets ID inside of
- 10:56that table so we're going to say return
- 10:57table dot find quests and then we're
- 11:00going to look for the Target ID so if
- 11:02the target ID is found then something
- 11:03will be returned through this function
- 11:05so then we're going to say if is on
- 11:07cooldown and we'll pass through the
- 11:08player.userid and the target ID then
- 11:11return end so now that we know the
- 11:13player is not on cooldown we can
- 11:15actually send out the invite and what we
- 11:16need to do is we need to get the
- 11:18player's trade request table similar to
- 11:19how he did and is on cooldown so we're
- 11:22going to say local player table equals
- 11:24trade request and index that with the
- 11:26player dot user ID now the reason that
- 11:28we need to do this is because we need to
- 11:29check if this first exists before we can
- 11:31actually add a player to this table so
- 11:33then we're going to say if not player
- 11:34table so if this table doesn't exist
- 11:36then we actually just want to create the
- 11:37table so we're just going to set that to
- 11:39a blank table and then after we create
- 11:40that table we then want to set the
- 11:42player table variable to this table
- 11:43right here then after we've done that
- 11:45we're actually going to fire off our
- 11:46trade request received remote so we're
- 11:48going to say fire client and who are
- 11:50going to fire this to is the Target and
- 11:52we're going to pass through the player
- 11:53who is saying to that client that they
- 11:55want to trade with them now we of course
- 11:56need to add this target to the player's
- 11:58list so that they can be on cooldown so
- 12:00we're going to say table dot insert
- 12:01player table and we're going to pass
- 12:02through the target ID and then we'll
- 12:04remove the player from the cooldown by
- 12:05saying task dot delay and of course
- 12:07remember set this to however long your
- 12:08cooldown is we'll just say 15 seconds
- 12:10and we'll create an anonymous function
- 12:12inside of here and then what we're going
- 12:13to do is we're going to say local index
- 12:14equals table dot find trade requests and
- 12:18index that with the player.user ID and
- 12:20we're going to try to find the target ID
- 12:21and then we'll say table dot remove
- 12:23trade requests pass on the same variable
- 12:25so tray request player.userid and we're
- 12:27going to remove the index from that
- 12:29table so this will add their cooldown
- 12:31and then after the cooldown ends they
- 12:32will be removed from it and then finally
- 12:34we just need to return true so that we
- 12:36know this trade has been sent out
- 12:37successfully one thing we have to touch
- 12:38up real quick inside of the trade Crypt
- 12:40inside of the is on cooldown function we
- 12:42have to say actually if not requests so
- 12:44if the player doesn't have any requests
- 12:46sent out then we're going to return
- 12:47false because the player obviously
- 12:48wouldn't be on cooldown we can then
- 12:50start this up go into the trade menu and
- 12:51we can see that we can send ourselves a
- 12:53trade request and we can even see that
- 12:54the notification popped up down here as
- 12:56well so now that we have all that
- 12:57working the next thing that we actually
- 12:58need do is start scripting up accepting
- 13:01the trade requests and what I mean by
- 13:02this is inside of the manager anytime we
- 13:04click on the accept button we fire this
- 13:06accept trade request remote event right
- 13:08there so what we're going to do is back
- 13:09inside of the trade script we're going
- 13:10to say remotes dot accepttraderequest
- 13:12dot on server event connect and now
- 13:14we're going to create a new function for
- 13:15this so we'll say local function except
- 13:18trade requests and that's going to
- 13:20accept a player and a Target ID as well
- 13:23which is going to be a number so then we
- 13:24can connect that event to that function
- 13:26right there and what are we going to do
- 13:27inside this function well something
- 13:28similar we're actually going to get the
- 13:30target again from the target ID just to
- 13:32make sure that player exists and is
- 13:33inside of our game now what we need to
- 13:35do is we actually need to check if
- 13:36either of the players are currently
- 13:37trading with another player because if
- 13:39they are then we don't want to start up
- 13:40any more trades or else things can go
- 13:42bad so what we're going to do is we're
- 13:43going to create a new function up here
- 13:44and that'll be the top function and it's
- 13:46going to be called is trading and it'll
- 13:48accept the player that we want to check
- 13:49if they're trading now the way that
- 13:50we're actually going to track if players
- 13:52are trading or not is by once again
- 13:53using another table so we're going to
- 13:55say local trading equals and then a
- 13:57blank table and now what we're going to
- 13:58do is to check if a player is trading is
- 14:01return trading and the player's user ID
- 14:03so this will return if the player is in
- 14:05this table or not so going back inside
- 14:07of the accept trade request function
- 14:08we're going to say if is trading and
- 14:10we'll pass through the player and the
- 14:11target so if either of the players are
- 14:13trading then what we want to do is we
- 14:15just run return end and stop the
- 14:16function right there the next thing we
- 14:17need to check is if the player we're
- 14:19trying to trade with has sent us a
- 14:21request for us to accept and the reason
- 14:23that we need to do this is because of
- 14:24exploiters exploiters could tell us that
- 14:26they're accepting another player's trade
- 14:28but that player never actually sent them
- 14:29a trade request and if we let that go
- 14:31through then exploiters would be able to
- 14:33forcefully trade with all the other
- 14:34players on our server so what we're
- 14:36going to do is create another function
- 14:37and that's going to be called has sent
- 14:39request and what this is going to do is
- 14:41it's going to accept the acceptors ID
- 14:44which is of course going to be a number
- 14:45and then the sender's ID which is of
- 14:48course going to be a number as well the
- 14:49way we're going to be finding if a
- 14:51player has sent a requests or not is
- 14:52going to be by using that trade request
- 14:53table that we created earlier so what
- 14:55we're going to do is we're going to say
- 14:56local request table equals trade
- 14:58requests and we're gonna index that with
- 15:00the sender ID now if the request table
- 15:02doesn't actually exist then we're just
- 15:04going to return end right here because
- 15:05that would mean that the player has not
- 15:07sent any requests but now if that table
- 15:08does exist then what we're going to do
- 15:10is we're going to say table dot find and
- 15:11inside of the request table we're going
- 15:13to look for the acceptors ID and now if
- 15:15we're able to find that then we're going
- 15:16to return true otherwise if we get to
- 15:18the end of the function we're just going
- 15:19to return false so if we are able to
- 15:21find that player's name inside of the
- 15:22other player's request table then we
- 15:23know that the request has been sent so
- 15:25going back into our accept trade request
- 15:27function we're going to say if not has
- 15:29set request passed through the
- 15:30player.user ID and the target ID then
- 15:33return end so now if the target exists
- 15:36if neither of the players are trading
- 15:38and as long as the player has sent a
- 15:39request to the acceptor then we can
- 15:41actually get these two players trading
- 15:43so now let's add both of these players
- 15:44to the trading table so we're going to
- 15:46say player.userid equals Target ID and
- 15:49then we'll also do the opposite for the
- 15:50Target ID so we're going to say trading
- 15:52equals Target ID and this equals
- 15:54player.userid and now we're also going
- 15:56to create another remote event and we'll
- 15:57rename this one to trade B again and now
- 16:00we're gonna fire this remote event to
- 16:01both the clients so that we can activate
- 16:02their GUI and get them into the trading
- 16:04mode so we'll say remotes dot trade
- 16:06began fire clients so we're gonna pass
- 16:08through the player and we're also going
- 16:09to give that player the target ID and
- 16:11then we also need to call this for the
- 16:12Target and pass through the players user
- 16:14ID just like that and now this function
- 16:16is all done now that we've done all that
- 16:17we can actually begin scripting the
- 16:19trade GUI itself we're gonna go into the
- 16:21trade GUI and we're going to delete both
- 16:23the button and the Handler scripts and
- 16:24we'll only be left with the manager
- 16:26script now once inside of here we have
- 16:27to clean this up a little bit so of
- 16:29course we don't need the pets config or
- 16:30the pets model nor do we need the
- 16:31Handler we will actually keep the
- 16:33container variable but for the rest of
- 16:35the stuff we can pretty much delete it
- 16:36all except for the template variable and
- 16:38then we'll also delete all of this down
- 16:40here as well now once we're left with
- 16:42all these variables we have to create
- 16:43some more for a lot of the different
- 16:44components that are inside of our GUI so
- 16:46we'll start off by making a variable for
- 16:48our title now it's going to be set to
- 16:49frame dot title then we need to start
- 16:51making variables for all of the hour
- 16:52components so like our accepted our
- 16:55label our offer our Avatar and then
- 16:57we'll also do the same for their their
- 16:59components as well so say local our
- 17:01Avatar equals frame dot our Avatar then
- 17:05our label and we'll change the variable
- 17:07name once we get all the hours out of
- 17:09the way then our offer and finally our
- 17:11acceptance now we'll just quickly rename
- 17:13the variables once we got all four of
- 17:15our variables then we need to copy these
- 17:17and we will replace our with there so
- 17:19their Avatar their label and their offer
- 17:21and now we also need to rename the
- 17:23variables as well and there we go we've
- 17:24now got all of our and their components
- 17:26and variables as well we could also put
- 17:28a little space between those two so that
- 17:29we can easily differentiate them now
- 17:31that we have all those components we
- 17:32also need to get the accept button so
- 17:34local accept equals frame dot accept and
- 17:37we also need to get the decline Button
- 17:39as well so frame dot decline now
- 17:41remember in our trade script we fired
- 17:43the trade began remote whenever we want
- 17:45both of the players to begin trading so
- 17:47we're going to actually listen to this
- 17:48remote in our script right here so that
- 17:50we can display the GUI and start doing
- 17:52the trading stuff so we're going to say
- 17:54remotes.tradebegan dot on client event
- 17:56connect and we'll actually create a
- 17:58function for handling this so we're
- 17:59going to say local function begin trade
- 18:01and that's going to accept the target ID
- 18:03which is a number so now whenever this
- 18:05function is called we want to get the
- 18:07target from their target ID so we're
- 18:08going to say local Target equals players
- 18:10get player by user ID Target ID and of
- 18:13course if not Target then return and and
- 18:16now if we are able to get a Target what
- 18:17do we want to do we want to enable the
- 18:19screen GUI so we can actually see that
- 18:21we're going to trade with another player
- 18:22now before we enable the screen gy we
- 18:24also want to do a couple of things to
- 18:26sort of prepare our GUI as well but what
- 18:28we can do first is we can connect the
- 18:29trade began remote to this function
- 18:31right here and then we can create
- 18:32another function above this function and
- 18:33this function will be called update
- 18:35labels and that's going to accept a
- 18:37player which is going to be our Target
- 18:38now inside of this function we want to
- 18:40modify the text of the title and what do
- 18:42we want the title to say we wanted to
- 18:44say trade with and then we want it to
- 18:46display the players or the targets
- 18:48display name just like that but of
- 18:50course we have to join them together
- 18:51using two periods and now that will be
- 18:53good additionally we need to modify some
- 18:55of their variables as well so such as
- 18:57their label dot text we we want that to
- 18:59be set to target.display name and we
- 19:02want to join that with apostrophe s
- 19:03offer so this will display something
- 19:05like monster devs offer or something
- 19:07similar to that and we also need to
- 19:08update their Avatar image as well and
- 19:10what are we going to set that to well
- 19:12we're going to use the player service to
- 19:13get the player's thumbnail and we're
- 19:15going to pass through the target.user ID
- 19:16headshot as a thumbnail type and we'll
- 19:18say 60 by 60 for the size so now all
- 19:20that is good what we then want to do is
- 19:22we want to call this function inside of
- 19:24our begin trade function and pass
- 19:25through the Target now another thing
- 19:27that we need to consider is we need to
- 19:28actually modify or update our own labels
- 19:30as well when the script starts up so
- 19:32that it displays our own name our Avatar
- 19:34picture and stuff like that so what
- 19:35we'll do is towards the bottom of the
- 19:36script we're going to create a new
- 19:37function and this is going to be called
- 19:39startup and inside of here we're
- 19:40basically going to do the exact same
- 19:41thing that we did with their variables
- 19:43so we're literally going to copy and
- 19:45paste these right down here inside of
- 19:46their label we'll say our label instead
- 19:48of their Avatar will say our Avatar and
- 19:50now of course we're going to use
- 19:51player.user ID because we're our own
- 19:53player and then player dot display name
- 19:55just like that and we'll just call the
- 19:56starter function right below it because
- 19:58we want the function run as soon as the
- 19:59script starts up and we only ever have
- 20:01to call this function one time now
- 20:02another thing that we want to do is
- 20:03whenever we actually accept a trade and
- 20:05whenever our GUI is open we want to
- 20:07generate all the pets that are in our
- 20:09pet inventory and display them in our
- 20:11trade inventory as well now we're
- 20:12actually going to go look back inside of
- 20:14our pet inventory GUI and look inside of
- 20:16the manager and the Handler and we're
- 20:18going to use some of the components from
- 20:19here to do exactly that so for instance
- 20:21if we look inside of the Handler inside
- 20:23of the pet inventory we have this
- 20:25module.generate pet function right here
- 20:27which accepts a pet table and then this
- 20:29is the function that we actually use to
- 20:30modify the template into a pet inside of
- 20:32our pet inventory so now rather than
- 20:34making modifications to this specific
- 20:36method right here we're just going to
- 20:37copy some of the main things that this
- 20:39function does like all this stuff right
- 20:40here then back inside of the manager of
- 20:42our trade GUI we'll create a brand new
- 20:43function called generate pet and this
- 20:46will accept a pet table which is of
- 20:48course going to be a pet table we'll
- 20:49then paste the code from a pet Handler
- 20:50script into here so that we can start
- 20:52working with it now the template that
- 20:53we're cloning is the template that is
- 20:55inside of our container right here and
- 20:57this template is literally the exact
- 20:59same template that is inside of the pet
- 21:01inventory so all the children that are
- 21:02inside of this template are literally
- 21:04the exact same so we really don't have
- 21:06to modify too many things here what we
- 21:07do need to change is we need to replace
- 21:09pet with pet table as that's the new
- 21:11variable name so we're just going to do
- 21:12that we also need to create a pet models
- 21:14variable and we can grab that directly
- 21:16from this Handler right here so let's go
- 21:18ahead and grab that variable and we'll
- 21:19put it right here and now we also need
- 21:21to get this module as well which we
- 21:23actually can do that easily we'll go
- 21:25back to the topic we'll say local pets
- 21:27Handler equals require script dot parent
- 21:29dot parent dot pet inventory dot Handler
- 21:32and now we can actually use that when we
- 21:34need to so we'll go back down to the
- 21:35function and instead of saying
- 21:36module.generate viewport frame we'll say
- 21:38pet handler.generate viewport frame and
- 21:40finally at the end of this function we
- 21:41want to return the Clone so that we can
- 21:43make some more modifications to it in
- 21:45another function now we'll call this
- 21:46function every single time we want to
- 21:47generate a brand new pet if that's for
- 21:49our own pets or for the player that
- 21:50we're trading with as well now the next
- 21:52thing we need to do is we need to
- 21:53actually generate all of our own pets so
- 21:55what we're going to do is we're going to
- 21:56create a function local function
- 21:58generate pets and this is referring to
- 22:00our own pets so you could even rename
- 22:01this to generate our pets or something
- 22:03like that that'll probably make it less
- 22:05confusing now if we look at the manager
- 22:06that's inside of her pet inventory how
- 22:08do we generate our own pets well we go
- 22:10inside of the player inside of the
- 22:11player's pet inventory folder and then
- 22:13we get the children and then we use this
- 22:15pet instance to table method and we get
- 22:17a pet table and then we also call the
- 22:18generate pet method from the Handler as
- 22:20well and that's how we generate the pet
- 22:22for our pet inventory GUI so we can
- 22:24literally just copy this go back inside
- 22:26of our manager inside of this function
- 22:27so four underscore pet in ipears
- 22:29player.pet in Missouri get children do
- 22:31then we'll create a variable for the pet
- 22:33table instead of saying Handler we'll
- 22:34say pets Handler and then we've got the
- 22:36pet table from the instance and now
- 22:38instead of calling pet handler.genery
- 22:40pet we'll call the generate pet function
- 22:41that we just created now one thing that
- 22:43we also need to do inside the generate
- 22:44pet function is we actually need to
- 22:45return the Clones so that we can use
- 22:48that clone inside of this function right
- 22:49here so say local pet equals generate
- 22:52pet pass through the pet Table and there
- 22:53we go and now the reason that we're
- 22:55getting the pet return back to us is
- 22:56because we actually need to listen for
- 22:58when the is actually clicked on because
- 23:00then we need to perform a specific
- 23:01action on it we're not going to get too
- 23:03deep into that yet I just wanted to make
- 23:04sure that we generate all the pets so
- 23:06that it populates our trade GUI and we
- 23:08can actually see all the pets that we're
- 23:09able to trade with anyways we need to
- 23:11make sure that we call to generate our
- 23:12pets function whenever we actually start
- 23:14the trade so before we even update the
- 23:16labels we will generate our pets just
- 23:18like that now another thing that we need
- 23:19to think about is that considering we're
- 23:21generating the pets every single time we
- 23:22start a trade we need to make sure that
- 23:24we actually clear out all the pets
- 23:26anytime the trade ends otherwise we're
- 23:28gonna have a memory leak and we're gonna
- 23:29have way more pests displayed inside of
- 23:31our trade gy than we actually own so
- 23:33let's go ahead and create a brand new
- 23:34function towards the top of our script
- 23:35and this is going to be called cleanup
- 23:37and what we're going to do with this is
- 23:38we're going to Loop through each of the
- 23:39different containers so for instance
- 23:41we're going to Loop through this
- 23:42container we're going to Loop through
- 23:43our offer and their offer because all
- 23:45three of these variables are places that
- 23:47pets can be stored because remember by
- 23:49default our pets are in our container
- 23:51whenever we click on a pet inside of our
- 23:53container we want to put that into our
- 23:54offer so the player sees the pet and if
- 23:56a player selects any of their pets for
- 23:58trade it'll go into their offer as well
- 24:00and every single time a trade ends we
- 24:02always want to make sure that we clean
- 24:03all these containers out and there's no
- 24:04pets in there so that when the GUI is
- 24:06displayed next time we won't see any
- 24:08pets there so we'll first Loop through
- 24:09our offers so we're gonna say for
- 24:10underscore child in high pairs our offer
- 24:13get children do and then we want to
- 24:15check if the child is a text Bun then
- 24:17we're just gonna destroy it so then we
- 24:18can copy and paste this for Loop and
- 24:20we're gonna do the exact same thing for
- 24:22their offer then we can copy and paste
- 24:23it again and we're going to do something
- 24:25similar for the container but we have to
- 24:26modify it slightly so we're gonna check
- 24:28if the child is a text button and
- 24:31child.visible so this would mean that
- 24:33the child is visible and the reason that
- 24:35we're doing that is because we don't
- 24:36want to delete this default template
- 24:37right here because we need that so that
- 24:39we can create clones of it and this
- 24:41template should never be visible anyway
- 24:42because it's the default one so going
- 24:44through this Loop it'll only delete all
- 24:46the visible ones which would be the
- 24:47Clones of it now the next thing to think
- 24:49about is when are we actually going to
- 24:50call this cleanup function well we would
- 24:52call it whenever we actually click on
- 24:54the decline button so we're gonna go
- 24:55towards the bottom of our script and
- 24:56we'll add the decline button so d
- 24:58decline.mousebond one click connect
- 25:00function and now whenever we click the
- 25:02decline button we should actually use a
- 25:03remote event to communicate with the
- 25:05server and tell the server hey we no
- 25:06longer want to trade we've just declined
- 25:08the trade so we're going to go back into
- 25:09our replicated storage inside a remote
- 25:11folder and add a brand new remote event
- 25:13and we'll rename this to trade declined
- 25:15So then whenever we click on the decline
- 25:17Bond we're going to say remotes.trade
- 25:18declined by your server and now that
- 25:20we've declined it we'll then call our
- 25:22cleanup function and we'll disable the
- 25:23screen GUI so screen GUI dot enabled
- 25:26equals false so that we no longer see it
- 25:28now before we're actually able to start
- 25:29testing some of this stuff out we need
- 25:31to go back inside of the trader Quest UI
- 25:33inside of the manager and when we fire
- 25:34the accept trade request for mode event
- 25:36instead of passing through the trader we
- 25:38actually need to pass through the
- 25:39trader.user ID because inside of our
- 25:41trade script we are looking for the
- 25:42Target ID and not actually the target
- 25:44player and now that we have that all
- 25:46good we should be able to test this out
- 25:47so sending a trade with myself and then
- 25:49accepting it we can see that this pops
- 25:51up and we are trading with ourselves so
- 25:53we have two of my avatars right here and
- 25:55two of my names displayed with offer as
- 25:57well now no pets are being displayed in
- 25:59the GUI but I'm pretty sure that's
- 26:00because we don't have any pets as is so
- 26:03what I'll do real quick is I'll hatch a
- 26:04couple of pets so now that we have pets
- 26:06when we actually go to trade with
- 26:07ourselves and we accept the trade our
- 26:09pets are displayed now it clearly shows
- 26:11two times the normal amount of pets that
- 26:13we actually have the reason for this is
- 26:15because we're trading with ourselves so
- 26:16inside of our trade script when we fire
- 26:18off the trade began remote event we're
- 26:20firing this to ourselves twice because
- 26:22we're both the player and the target
- 26:23that's why there are two rows of these
- 26:25pets so remember this is all just for
- 26:27testing but this means that it is
- 26:29working as it should be which is good
- 26:31additionally we've partially scripted
- 26:32decline button so let's go ahead and
- 26:33click that and see if that works and it
- 26:35seems like it does work it most likely
- 26:36fires off the remote event that we
- 26:38haven't actually tracked on the server
- 26:39yet and it also does hide the GUI as
- 26:41well which means that things are going
- 26:42good now with that trade decliner remote
- 26:44event that we just created let's go back
- 26:45over the server and start scripting this
- 26:47remote event out first things first down
- 26:49here we actually need to get the remote
- 26:50so remotes.trade decline dot on server
- 26:53event connect and then we'll connect
- 26:55that to a function let's go ahead and
- 26:56create that brand new function up here
- 26:58so look function trade declined and this
- 27:01is going to accept a player so then we
- 27:03can connect that event to that function
- 27:04right there and now looking back at her
- 27:06is trading function is trading will
- 27:08actually return a value which will be
- 27:10the Target that the player is currently
- 27:11trading with if they're trading with any
- 27:13player at all and that's how you can
- 27:15actually get who a player is trading
- 27:17with by just their player alone so
- 27:18there's a local Target ID equals is
- 27:21trading and we're gonna pass through our
- 27:22player now if we don't get any Target ID
- 27:24return to us then that means that this
- 27:26player isn't actually trading with
- 27:27anybody so we'll just stop the function
- 27:29right there but if we do get a Target ID
- 27:30return to us then we also want to get
- 27:32that specific player as well so local
- 27:34Target equals players get player by user
- 27:36ID and pass through the target ID now we
- 27:39need to index the trading table with the
- 27:41player.user ID and we're going to set
- 27:43that to nil so that we know that this
- 27:44player is no longer trading with anybody
- 27:46and we also need to do the same thing as
- 27:47well for the Target so we're going to
- 27:49say Target ID and then set that to nil
- 27:51as well and then finally we'll use the
- 27:53trade decline remote and we're going to
- 27:55actually fire client and we're gonna
- 27:56send that to the Target and we'll pass
- 27:58through the player.user ID to that
- 28:00remote as well and also we're only going
- 28:01to do this if we actually find the
- 28:03target so if Target then then we will
- 28:05call that remote just like that so now
- 28:07we need to go back inside of our manager
- 28:09script right here and we need to listen
- 28:10for that remote event to be fired so
- 28:11we're going to say remote Stop Trade
- 28:13declined dot on client event connect and
- 28:16we're going to connect that to a brand
- 28:17new function so let's go ahead and
- 28:18create that function right here so local
- 28:20function Decline and inside of this
- 28:21function we're actually going to call
- 28:22the cleanup function and we're going to
- 28:24say screen GUI dot enabled equals false
- 28:26because we don't want them to see it
- 28:28anymore similar to exactly like what we
- 28:29do down here so we could delete these
- 28:31two lines of code and we can just call
- 28:32this decline function right here just
- 28:34like that and also whenever this trade
- 28:35decline remote is fired we can also call
- 28:37the decline function as well just like
- 28:39that so now whenever we decline a trade
- 28:41we're going to tell the server hey we
- 28:42just declined a trade and then we're
- 28:43going to clean this up and hide the GUI
- 28:45and now from the server side if a player
- 28:47that we're trading with declines the
- 28:48trade then they're going to tell the
- 28:49server and then the server is going to
- 28:51tell us so then if we're told that the
- 28:53other player has declined the trade then
- 28:54we're going to call this decline
- 28:55function right here clean up the GUI and
- 28:57then hide it as well the next thing
- 28:58we're going to be working on is
- 28:59selecting specific pets whenever we
- 29:01click on the pet that is stored with
- 29:02inside of the container variable we want
- 29:03to put that pet inside of our offer
- 29:05which is another container and now if we
- 29:07click on a pet that's already inside of
- 29:08our offer we want to put that pet back
- 29:10inside of the container because we would
- 29:12effectively be unselecting or selecting
- 29:14the pet based on their location so we're
- 29:16going to go back to where generate our
- 29:17pets function now inside of here we have
- 29:19a variable which is pet and that's
- 29:20actually the clone of the template
- 29:21returned back to us so that would be a
- 29:23text button now what we're going to do
- 29:24is we're actually going to use a set
- 29:26attribute method on this text button and
- 29:28that should be that we're going to be
- 29:29setting is called selected and by
- 29:31default we're going to set that to false
- 29:32because when we first generate the pets
- 29:34they're not automatically selected by
- 29:36default now whenever we click on a pet
- 29:37we can then use this attribute to see if
- 29:40this pet is selected and if it is
- 29:41selected then we obviously want to
- 29:43unselect it or if it's not selected then
- 29:44we obviously want to select it so we're
- 29:46going to say if pet get attribute
- 29:48selected then so if the pet is selected
- 29:50then what do we want to do well we want
- 29:52to deselect it else select it so what
- 29:54does deselecting and selecting entail
- 29:56let's first work on selecting a pen when
- 29:58we select the pet what do we want to do
- 29:59well we want to actually set its
- 30:01attribute of selected to be true so that
- 30:04we know that it's selected now and we
- 30:05also want to set the parent of this text
- 30:07button to our offer because this is a
- 30:09pet that we've now selected to be traded
- 30:11to the other player additionally we're
- 30:13going to need to use another remote
- 30:14event so we'll go inside the remotes
- 30:16folder and this remote event is going to
- 30:17be called trade select pet so we'll then
- 30:19call that remote so trade select pet
- 30:21fire server and we're going to fire it
- 30:23with the pettable dot uuid so we're
- 30:25going to tell the server hey this is the
- 30:27uuid of one of our pets that we want to
- 30:29trade and then from the server we're
- 30:30going to tell the other player that this
- 30:32is the pet that the other player wants
- 30:33to trade with you now we finished
- 30:34everything for the selecting so that's
- 30:36all handled perfectly what we can do to
- 30:38make this a little bit easier to
- 30:39understand later in the future if we
- 30:40ever come back to this script is we'll
- 30:42actually create a function for this so
- 30:43we'll say local function select pet and
- 30:45the arguments that we need to get passed
- 30:47through to here is the button which is
- 30:48of course going to be a text button and
- 30:50the pet table which is of course going
- 30:51to be that pet table that we've normally
- 30:53been getting and I will just take all of
- 30:54this and put that directly into here and
- 30:56now instead of saying pet we are going
- 30:58going to say button button just like
- 31:00that and we'll use the pet table as
- 31:01normal so then we just call the select
- 31:02pet function right here and we can pass
- 31:04through the pet which is going to be the
- 31:05text button and the pet table which is
- 31:07of course going to be the pet table just
- 31:08like that so now we can basically copy
- 31:10and paste this select pet function and
- 31:12we can rename this from select pet to
- 31:14deselect pet now we're basically just
- 31:16going to do the opposite so instead of
- 31:17setting this attribute to true we're
- 31:18actually going to set the false instead
- 31:20of setting the parent of the button to
- 31:22our offer we're going to set it back to
- 31:23the container and then instead of firing
- 31:25off this remote we're actually going to
- 31:26create another remote and we're going to
- 31:28rename this from select to deselect so
- 31:30then we'll fire off the trade deselect
- 31:32pet and that'll once again be with the
- 31:34uuid and now we have to actually call
- 31:36this function right here so deselect pet
- 31:38with the pet and the pet table just like
- 31:40that now we can go into our game and
- 31:42test this out so we're going to go ahead
- 31:43and accept our own trade and then let's
- 31:45go ahead and say for instance click on
- 31:47the bunny now that we see that we've
- 31:48clicked on the bunny the bunny is now
- 31:49inside of our offer if we click on that
- 31:51bunny again it is now removed from our
- 31:53offer exactly as it should be so we can
- 31:55click a couple of different pets we can
- 31:56see they fill inside of this contain
- 31:58enter if we click them again we are
- 31:59unselecting them which means this is
- 32:01working perfectly now let's go back over
- 32:03to the server script and start working
- 32:04on the server side of this stuff now we
- 32:06have another remote to work with so
- 32:07we're going to go ahead and copy this
- 32:09and we're going to say trade select pet
- 32:11instead of the trade Decline and we're
- 32:12going to connect that to a brand new
- 32:14function and let's go ahead and start
- 32:15creating this function so local function
- 32:17trade select pet of course the first
- 32:19argument is going to be a player and the
- 32:21second one is going to be the uuid of
- 32:22the pet which is going to be a strength
- 32:24now we're going to copy some of our
- 32:25standard variables like this target ID
- 32:27right here and then also getting the
- 32:28target so first of all we need to check
- 32:30if the player is trading with somebody
- 32:31and then also get the target as well and
- 32:33if we're not able to find a Target then
- 32:35we just want to return end because that
- 32:37player is no longer in the game the next
- 32:38thing we need to do is we need to make
- 32:39sure that the player actually owns the
- 32:41pet of the uuid that they're telling us
- 32:43that they want to trade so we're going
- 32:44to say local pet instance equals player
- 32:47and we can access this by going into
- 32:48their pet inventory and then we're gonna
- 32:50say fine first child and the name of the
- 32:52child is going to be the uuid that the
- 32:54player passed through us so if not pet
- 32:56instance then reach turn and because the
- 32:59player does not actually own that pet we
- 33:01then need to convert the information
- 33:02from the pet instance into a table which
- 33:04we call a pet table now if you don't
- 33:06remember exactly how we do this we can
- 33:08go inside of our script and we want to
- 33:09go inside of the pet Handler because
- 33:11inside of the pet Handler we have this
- 33:12pet instance to table function and this
- 33:14is all we do now since this script is on
- 33:16the client side we can't actually use
- 33:17this on the server side so all we're
- 33:18going to do is we're going to copy this
- 33:19bring it into the trade script paste it
- 33:21just like this and now we can modify it
- 33:23slightly so we need the uuid we need the
- 33:25ID and we need the Rarity but we
- 33:27actually don't care about it being
- 33:28equipped or not and then instead of the
- 33:29pet variable it's actually the pet
- 33:31instance variable so just like that and
- 33:33now we have the uuid the ID and the
- 33:35Rarity all inside of this new pet table
- 33:36right there and we're actually rename
- 33:38this from new pet to pet table there we
- 33:40go and now back at the top of our script
- 33:41we're going to create a new table so
- 33:43that we can track all the pets that the
- 33:44player has selected so we're going to
- 33:46say local trade selected equals blank
- 33:48table now going back inside of the trade
- 33:50select pet function we're gonna say
- 33:51local selected pets equals trade
- 33:54selected and we're gonna index that with
- 33:56the player.userid and now if not not
- 33:58selected pets so if the player doesn't
- 34:00have any selected pets currently what
- 34:01we'll do is we'll create a table for
- 34:03that player so we're going to say trade
- 34:04selected index to have the player die
- 34:06user ID equals a brand new table and
- 34:08then we're going to say table dot insert
- 34:10trade selective indexing that with the
- 34:11player dot user ID and then we'll just
- 34:13add the uuid to this so that we can keep
- 34:15track of all the uuids of the pests that
- 34:17each player has selected when they're
- 34:19traded then finally we'll use the
- 34:20remotes.trade select pet and fire that
- 34:24to the target with the pet table that we
- 34:26just created so that means we have to go
- 34:27back inside of our manager script and
- 34:29listen to whenever this remote event is
- 34:30fired so we're gonna say remotes.trade
- 34:32select pet on client event and now we
- 34:34actually have to do something and what
- 34:36are we gonna do well we'll go under the
- 34:37generate our pets function and we're
- 34:39going to say local function Trader
- 34:41selects pet and what are we gonna get
- 34:43we're gonna get the pet table then what
- 34:45are we gonna do we're actually just
- 34:46gonna call the generate pet function and
- 34:47we're going to pass through the pet
- 34:48table now we need to modify this
- 34:50function slightly the reason for that is
- 34:52because inside of this function we are
- 34:53setting the parent right here to
- 34:54container and of course if a Trader
- 34:56selects the pet we don't want the parent
- 34:57it to be the container we want the
- 34:59parent of that pet to be their offer so
- 35:01what we need to do is inside of the
- 35:03generate pet function we need to accept
- 35:04another argument and that's going to be
- 35:06an instance so now instead of setting
- 35:08the parent to container by default we're
- 35:10going to set that to the parent argument
- 35:11which is passed to this function so now
- 35:13we've got to make sure that every single
- 35:14time we call this function we pass
- 35:15through the correct parent so originally
- 35:17inside of our generate our Pet's
- 35:18function we always wanted this parents
- 35:20into the Container so we're going to
- 35:21make sure that we pass that through in
- 35:22this argument and now inside of the
- 35:24trader selects pet function we're going
- 35:26to need to also pass through a parent as
- 35:27well and that's going to be for their
- 35:28offer so then we're going to go ahead
- 35:30and connect the trade select the pet
- 35:31remote event to this function right here
- 35:33next we have to start scripting the
- 35:34deselect pet on the server side as well
- 35:36so going back to the server we also need
- 35:38to make sure that we point the trade
- 35:39select pet remote event at the trade
- 35:41select pet function that we just created
- 35:42and then we can add the new remote event
- 35:44instead of Select it's deselect pet so
- 35:46then we're going to create another
- 35:47function local function trade deselect
- 35:50pet and that'll accept a player and a
- 35:52uuid as a string and now let's make sure
- 35:55that we hook that event to that function
- 35:57just like that and we're going to copy a
- 35:58lot of the basic variables that we
- 35:59created in our trade select pet function
- 36:01just like that we're also going to get
- 36:03the selected pets as well certain State
- 36:04local selected pets equals trade
- 36:07selected indexed out the player.userid
- 36:09and if we don't find any selected pets
- 36:11from that player then we just wanted to
- 36:13return n because they can't deselect any
- 36:15pets if they don't have any pets
- 36:16selected right so now that we've done
- 36:18that we should be able to actually find
- 36:19the pet so we're gonna say local pet
- 36:21equals and then we're gonna say table
- 36:22dot find and we're going to look inside
- 36:25of the selected pets table and we're
- 36:26going to search for the uuid that the
- 36:28player has passed to us now if we're not
- 36:30able to actually find the pet then we're
- 36:32gonna return and because that means that
- 36:33the player doesn't have that pet
- 36:34selected but if we are able to find that
- 36:36pet then what we're going to do is we're
- 36:38going to say table down remove and we're
- 36:39gonna remove it from the selected pets
- 36:41and we're going to remove pet now that
- 36:42we've removed that pet from that table
- 36:44we also need to notify the other player
- 36:46as well so we're going to say local
- 36:47trade deselect pet fire client Target
- 36:49uuib so we're gonna remove that pet from
- 36:51the server table and we're also going to
- 36:53tell the client so they can remove it
- 36:54from their GUI as well so going back
- 36:56inside of the manager we now need to
- 36:57handle the Trader deselect pet so we're
- 36:59going to go back down here and add the
- 37:00new remote event so trade deselect pet
- 37:03and we'll create a function just below
- 37:05this one right here so Trader select pet
- 37:07to Trader deselect pet and then let's
- 37:09make sure that we set this function to
- 37:11that remote event just like that and now
- 37:12what do we want to do whenever a player
- 37:14deselects a pet well we simply want to
- 37:15delete that pet from their container so
- 37:17we're going to say local instance equals
- 37:19their offer find first child oh and also
- 37:21we get past the uuid to this function so
- 37:24we're going to look for the uuid and if
- 37:26that instance exists then what we're
- 37:27going to do is we're going to destroy it
- 37:30and it's as simple as that so now we can
- 37:31start testing some more complex things
- 37:33and we're actually going to want to
- 37:34launch two different clients so that we
- 37:36can simulate players training with each
- 37:37other because of course you need two
- 37:39players to do that if you guys are
- 37:40unfamiliar with how to launch multiple
- 37:41clients in studio what you're going to
- 37:43do is you're going to go to test and
- 37:44inside of here you have local server
- 37:46make sure you click on that and then you
- 37:48can select the amount of clients that
- 37:49you actually want to launch zero one two
- 37:52three four five all the way up to eight
- 37:54now we're gonna set that to two and then
- 37:56you can either hit F7 or click this
- 37:58start button right here at the hotkeys
- 37:59F7 and then wait for it to fire up now
- 38:01after it does fire up you'll actually
- 38:03have three different screens open this
- 38:05one in front is actually from the server
- 38:06side so we can see two things on the
- 38:08server have been printed while these two
- 38:10are their own client now we're going to
- 38:11go ahead and test out trading with them
- 38:12so we're gonna go ahead and send a trade
- 38:14request and we can see the trade request
- 38:16does actually appear there is no
- 38:17thumbnail though because these players
- 38:19aren't actually real players as we can
- 38:21see their name is player so whenever we
- 38:23use the get thumbnail method on it these
- 38:25players don't actually exist so they
- 38:26don't have a thumbnail anyways though we
- 38:28can go ahead and accept that trade now
- 38:29we can see that we are trading with that
- 38:31player and then let's go ahead and
- 38:32select the pet and when we select the
- 38:34pet on this side we can see that it's
- 38:35also selected on this side as well which
- 38:37means that this is working perfectly now
- 38:39if we deselect this pet we can also see
- 38:41that it's removed on this player's side
- 38:42of the screen as well so we are seeing
- 38:44accurate trades working in action which
- 38:46means this is working perfectly
- 38:48currently the next thing we need to work
- 38:49on is accepting a trade so let's go
- 38:51towards the bottom of our script right
- 38:52where we listen for the decline button
- 38:54to be clicked let's go ahead and listen
- 38:55for the accept button to be clicked now
- 38:57the except one actually needs to perform
- 38:58of one of two actions if we've already
- 39:01clicked the accept button the text will
- 39:03actually change the display unaccept and
- 39:05if the button displays unaccept we need
- 39:06to perform a different action than if it
- 39:08displays except so we'll say if except
- 39:10dot text equals accept then and what do
- 39:13we want to do well we actually need to
- 39:14tell the server that we accept this
- 39:16trade so what we're going to do is we're
- 39:17going to create a new remote event and
- 39:19we're going to say trade accept so then
- 39:21we're going to say remotes dot trade
- 39:23accept fire server and then we're gonna
- 39:25need to modify our accepted which is
- 39:27going to be a text label and we need to
- 39:29set the visibility of that to true we
- 39:30also want to modify the text of the
- 39:32accept button from accept to unaccept
- 39:34and now that's all good now what happens
- 39:36if we actually want to unaccept the
- 39:38trade well we'll basically do the
- 39:39opposite of what we just did here so we
- 39:41can go ahead and copy and paste this and
- 39:43we're also going to create another
- 39:44remote instead of saying trade except
- 39:45we're going to say trade on accept so
- 39:47we'll fire off the trade on accept
- 39:49remote just like that we'll set the
- 39:51visibility of our accepted to false and
- 39:53then the text should not be unaccept it
- 39:55should Now display except now one thing
- 39:57that we also need to consider is imagine
- 39:58if two players put a bunch of pets up
- 40:00for trade and one of them clicked accept
- 40:02now imagine if the other player who
- 40:04hasn't accepted yet starts taking out
- 40:05all of their pets that they were about
- 40:07to trade and quickly clicks the accept
- 40:09button before the other player realizes
- 40:10this well then the other player would
- 40:12basically feel scammed because they
- 40:13didn't realize that another player could
- 40:15remove all their pets and they would
- 40:16still be accepted so what I'm
- 40:18effectively saying is that anytime we
- 40:19actually select or deselect a pet we
- 40:21most likely want to perform this
- 40:23unaccept action right here as well so
- 40:25what we're going to do is we're actually
- 40:26going to make this into its own function
- 40:28so we're going to take these actions and
- 40:29we're going to put it towards the top of
- 40:31our script so above cleanup we're going
- 40:33to say local function unaccept trade and
- 40:35then we'll just paste those actions
- 40:36right there now inside of the generate
- 40:38our pets function whenever we listen for
- 40:40the pet to be clicked on we want to call
- 40:41the unaccept trade function as well just
- 40:43like that and then back inside of when
- 40:45we're listening for the accept button to
- 40:46be clicked on we want to call the
- 40:47unaccept trade and the else statement as
- 40:49well so with all that being done we can
- 40:51hop directly into the game and start
- 40:52testing out our trading system we can
- 40:53click on the trade button send the other
- 40:55player trade request we can go over here
- 40:56and accept that request and now we have
- 40:58this GUI popping up and then we can
- 41:00select any of our pets that we want to
- 41:01throw up for trading and we can select
- 41:03some from over here as well and then if
- 41:05we both hit on accept we can see that
- 41:07the trade has been accepted so it looks
- 41:08like this whole system is working
- 41:10perfectly with all that being said we
- 41:12now have a functioning trade system in
- 41:14our game now as always if this video to
- 41:15help you guys out or you did enjoy it
- 41:16make sure you smash like button also
- 41:18subscribe button and turn post
- 41:19notifications on if you want to get
- 41:20notified we'll upload more of Roblox on
- 41:21my content of course I have a patreon if
- 41:23you guys like swarmy and again access
- 41:24all the games and the scripts that I
- 41:25made during this video there's a link
- 41:26down below description and you guys are
- 41:28going to check that out with that being
- 41:29said I'll see you guys in the next
- 41:30episode
- 41:32foreign
- 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.