Remote Functions (Two-Way Communication) - Roblox Advanced Scripting #6 — Transcript
Full transcript
- 0:00hey thanks for stopping by we're going to be adding more security to our Roblox scripts
- 0:04using a feature that further expands what we learned upon in the last episode about remote
- 0:09events this one is going to be about remote functions which have its own use cases that
- 0:14make it different from remote events and in this episode I'm going to be showing you what these
- 0:19are and how to use them and this example that I have here uses remote functions so I have a
- 0:24leader set with 50 coins and I have a prompt here that says would you like to buy this button for
- 0:2950 coins coins if I click on it then it then the server should have deducted 50 coins from
- 0:35my leader stats and that and that message should have been sent back to the client to then update
- 0:41this GUI to say purchase has been successful so this had a two-way communication system that was
- 0:48happening rather than one-way communication and that is essentially the difference between the
- 0:53two and that's what we're going to be discussing about inside of this episode but before we start
- 0:58implementing let me go back to our diagram that I've showed you in previous episodes all right
- 1:05let's think about this for a minute so we're back on this diagram that shows us the server
- 1:10and client model yet again I think you've seen this before that's located in the Roblox developer
- 1:14Hub link is in the description for that in the last episode we covered remote events which act
- 1:19as one-way communication between the server and the client and like I mentioned remote events
- 1:25act as one-way communication and there's three ways of communicating with remote events so so
- 1:30the first one is from one client to the server the other way is from the server to one of the
- 1:37clients and then there was the server to all of the clients but what if we wanted to do a
- 1:45communication that sends information to one party and then with that it will send results back to
- 1:53the the caller what I mean is what if we had this player communicate to the server and then with
- 2:00that communication to the server the server then communicates back to the player with results to
- 2:07give us more information or to let us know that the call was successful this is essentially what
- 2:13remote functions do that remote events can't do the very important thing to know here is that what
- 2:19we're going to be talking about in this episode will act as two-way communication rather than
- 2:24one-way communication so hopefully you were able to understand this from my explanation now that we
- 2:29understand let's Implement these remote functions so I went ahead and created a leader stat script
- 2:34inside of server script service with our player added event and we're given 50 coins whenever we
- 2:41join into the game so and then for the setup what we're going to do is we're going to make a prompt
- 2:47purchase with the coins that we have when we load into the game so in starter GUI we're going to
- 2:53create a screen GUI and then inside of the screen GUI we're going to create a text label and we're
- 2:59going going to rename this to prompt and let's move this over here let's drag it a little bit
- 3:06let's Center it let's go down here text scale it and then let's change the text to would you like
- 3:13to buy this button next what we'll do is we're going to we're going to add a text button to the
- 3:20screen gy so it's going to look like this and then we're going to rename this to buy button
- 3:27let's move this down here let's scale it up just a little bit Center it go down here text scale it
- 3:35and then change the text to buy button and then in parenthesis we're going to say 50 coins to let
- 3:43the player know that this is going to cost 50 coins let's just scale this a little bit here
- 3:48so that it looks nicer and then Center it back again okay and now what we'll do is we'll create
- 3:53a local script inside of our screen GUI so let's just create a local script here go back into our
- 3:58game and now now that we have our setup let's create our remote function and so because this
- 4:04requires server and client communication we want to be able to access this remote function from the
- 4:09server and the client much like remote events and one of the best places that the server and
- 4:16client can access is replicated storage so we're going to add this remote function inside of here
- 4:22in replicated storage so we'll hit the plus sign and let's add a remote function remember
- 4:28it's remote function and let's rename this to BU button that's what we're going to call this remote
- 4:37function and now what we'll do is we're going to open up our local script and let's delete this and
- 4:44first make reference to our remote function so we'll say local bu button and just to make this
- 4:52easier we're going to call this by button remote so that we know that this is a remote function
- 4:58equals game . replicated storage. bu button and and then what we'll do is make reference to our
- 5:06GUI make reference to our prompt and also make reference to our buy button so first we're going
- 5:11to make reference to the GUI which is located in the scripts parent and then we will locate The
- 5:18Prompt which will be in the GUI itself so guy. prompt and then finally the buy button so buy
- 5:27button equals GUI bu button and now what we'll do is we will make this button active when we
- 5:34press it so that we can send the message to the server using our remote function so in here let's
- 5:40create a mouse click event so we'll say by button. Mouse button oneclick colon connect function open
- 5:52parentheses close parentheses enter so here's the really interesting thing about remote functions
- 5:57that makes them unique compared to remote events so in order to fire the remote function much like
- 6:03remote event all we need to do is use the function provided to to call whatever side of the game we
- 6:09want to fire to so in this case since we're in the client we want to fire to the server so you
- 6:15might say buy button remote colon fire server but fire server is surprisingly not a function you can
- 6:25use for remote functions because fire server is very specifically a function for remote events
- 6:34so in this case what we're going to say instead of fire server is we're actually going to replace
- 6:39fire with invoke so this is the function we need to use in order to make call to make a call to the
- 6:48server and in terms of parameters it's basically the same thing with invoke server and in this
- 6:54case the other one is invoke client where we don't need to pass in a player if we're calling from the
- 7:00client to the server but if we're going to pass in arguments then we're going to need to do that
- 7:06for here so it's basically structured the same way as a remote event and the way we're going to
- 7:12catch this function call is by doing it from the server so let's copy this reference up here with
- 7:18contrl c and then we're going to go to our leader stats script let's drop two lines down here cop
- 7:25paste that and then down here the same sytax here is going to be a little weird like with invoke
- 7:32server not being fire server there's a special Syntax for catching this invoke server call and
- 7:40it's not on server event but instead it's going to be on server invoke so this is what it's going to
- 7:47look like it's going to be by button remote.on server invoke now you might be tempted to say
- 7:58colon connect function open parentheses close parentheses and enter this is actually incorrect
- 8:04this is not the right way to write the Syntax for on server invoke even though this pretty much is
- 8:10what it is like for on server event so you might ask why is it not like on server event why is the
- 8:17syntax incorrect here it's important to understand the difference between a call back and an event I
- 8:22will explain what the difference is once we finish writing our code as of right now what you need to
- 8:28know is that this is not the right way to write the on server invoke syntax because this is not an
- 8:34event this is a callback and the way you structure a call back is that it's basically a function
- 8:40that is structured like this so after on server invoke we say equals function because we create
- 8:47a new function when we call this on server invoke with the open parentheses closed parentheses and
- 8:54the parameters are basically the same way as a on server event event call so you know how over
- 9:01here we have an on server event call for remote events where the first parameter implicitly gives
- 9:09you the player and then you have whatever other arguments you have over here it basically works
- 9:15the same way here where we are given the player when we call on server invoke and then let's hit
- 9:20enter now you might notice is that this end has no end parentheses while on server vent does have
- 9:29an end parentheses so this is something you have to keep aware of is that there are tiny syntax uh
- 9:35differences when it comes to using remote events and remote functions and when you're calling on
- 9:41server invoke you have to set this equal to a new function that implicitly passes in the player much
- 9:47like on server event and then it returns and then it stops this block of code using an end
- 9:53statement without the end parentheses so that's something important to know about here so now
- 9:58what we'll do is is we will make reference to the player's coins and check if the player has enough
- 10:03coins to be able to buy this uh button and then we will subtract 50 currency from the players's
- 10:10inventory so we're going to first make reference to their leader stats by saying local leader stats
- 10:15equals player. leer stats and then we will make reference to the coins by saying leader stats.
- 10:22coins and then we will check if coins. value is greater than equal to 50 then we're going to
- 10:32subtract the coins value by 50 we've now provided one-way communication from the from the client to
- 10:41the server to then tell the server whether the player can buy this button and if they can then
- 10:47we will subtract the currency so right now this is functioning pretty much the same as a remote event
- 10:54but now let's get to the interesting part that makes this stand out from remote events and that
- 11:00is the two-way communication part so I mentioned that we're able to invoke the server by calling
- 11:06invoke server and then telling the server to do what it needs to do here but now let's actually
- 11:11send information back to the client to let us know that the purchase has been successful so that we
- 11:16can let the client do something special when we know that the player was able to buy the button
- 11:22so let's go over here and let's make a Boolean by dropping two lines at the very beginning so we'll
- 11:28say local purchase successful equals false and then let's drop a line down here after subtracting
- 11:3750 coins by saying purchase successful equals true so this basically lets us know that if the player
- 11:47was able to successfully buy the button then we set this purchase successful Boolean to true and
- 11:54now what we'll do is we're going to go down here and use the return key keyword to return
- 12:00the purchase successful uh Boolean so not only are we invoking the server to buy this button
- 12:07and make computations to be able to get rid of 50 coins from the coins value we're also passing back
- 12:13information to the client to let us know that the purchase has been successful and the way
- 12:19we will get the information back is by declaring a Boolean and setting it equal to this function
- 12:24call right here so behind our call let's create a variable we will call this one local purchase
- 12:32successful equals buy button remote invoke server so what's going to happen is that this is going to
- 12:39be essentially a Boolean that is set to whatever this function call returns so when we call invoke
- 12:47server to buy button remote it's going to go to the server and it's going to first create
- 12:53a Boolean called purchase successful and then it'll check whether the player can buy the button
- 12:57if they can then we're going to set purchase successful equal to true and then we're going to
- 13:02return whatever the value of purchase successful is back to the client which is going to be set
- 13:07here so now this will tell the client whether we know that the purchase has been successful
- 13:12and let's do that check down here by dropping two lines and saying if purchase successful
- 13:18then and when we just put in the Boolean variable here this is implicitly saying if this is true or
- 13:23not if this is true then what we will do is we're going to change the prompt text label so this text
- 13:31label right here we're going to change the prompts text to purchase successful so that we can really
- 13:41let the client know that the purchase has been successful and let's even change the background
- 13:45color of it too by saying prompt. background color 3 equals color 3. from RGB let's click this color
- 13:55wheel and let's pick light green and so this will let us know know whether the purchase has been
- 14:00successful or not but we can also make a check to see if it wasn't successful and we will do
- 14:06this by dropping a line down here and saying else and now down here let's just copy this code over
- 14:12here and paste it down here so then we will say purchase failed instead of success instead of it
- 14:19being successful and let's also change this color to Red so now we've successfully made a remote
- 14:26function that basically allows the player to press a button to buy that button by calling the server
- 14:33and telling us whether the player can buy this button or not and if they can then we will send
- 14:38information back to the client to tell us that the purchase was successful so that we can do whatever
- 14:45we want to do after we get this information so let's go into the game and see our results all
- 14:52right so I've loaded into the game let's see if we can buy this button so I have 50 coins up here
- 14:57and this says would you like to by this button let's click it and as you can see the server has
- 15:03success has successfully decremented 50 coins from my leader stats and the text label has updated to
- 15:11say purchase has been successful now let's see what happens if I press it again it'll then tell
- 15:16the server to check whether the player can buy the button or not which in this case we can't because
- 15:22we have zero coins not 50 so then we told the server that this is not possible and we sent the
- 15:29information back to the client by saying it was not possible to so then we could update this text
- 15:34label to say the purchase has failed and so we have successfully used a remote function to make
- 15:42two-way communication with the server in client so that we can pass information around and do what it
- 15:48is we need to do with that two-way communication from the server and client now there is another
- 15:53method for this two-way communication because what we did was go from the client to the server
- 15:59and then back to the client the second method we can do is from the server to the client and then
- 16:05back to the server however what I can say to that is I would actually advise not to do this method
- 16:12from communicating from the server to the client and then back to the server because the server
- 16:17basically has access to all the information that a client already has so there's no need for a server
- 16:24to communicate to the client to get information uh to be able to send it back to the server there's
- 16:30just no need for that what you could do instead is just use a remote event that communicates from the
- 16:36server and then to the client so that the server can tell the client what to do rather than having
- 16:41to fetch information from the client because we don't need information from the client because
- 16:46the the server already knows everything that a client knows so the only use case I can say
- 16:51to remote functions is by communicating from the client to the server and then back to the client
- 16:56that's the only use case I can see with remote functions at least in my opinion from my time
- 17:02developing Roblox games that's the only use I've seen with remote functions now you might
- 17:07ask why does the Syntax for on server invoke look like this where we need to create a new function
- 17:14and not have an end parenthesis by the end here rather than doing it like this with remote events
- 17:20with the colon connect function and end it's important to understand the difference between
- 17:24events and callbacks so events are basically a signal that gets fired when something happens
- 17:32when we call the function of the remote event to then catch that event using colon connect this is
- 17:39specifically for events when they have to catch the the event calls but in the way that a call
- 17:43back is different for ons server invoke is that a call back is basically a function that is passed
- 17:50as an argument to another function and then it is called to uh execute code after a task is
- 17:56complete because the the whole point of a two-way communication is that when we communicate from one
- 18:05side which is the client to the server and we rely on information from the server to be sent back to
- 18:11the client the code does not continue until we get the results from this call back to then continue
- 18:18executing whatever else it is we need to execute down here but in event call can happen multiple
- 18:24times and execute without having to wait for results to happen I hope that makes sense imagine
- 18:30if you had two parts here that have a touched event where if a player constantly stepped on
- 18:37this part then it'll keep on firing events to then to execute whatever it is that we need to execute
- 18:43here but a call back relies on the information to be received back to whatever party called that
- 18:53function in this case we cannot have so many of these invo calls at the same time as we do with
- 19:01events I don't know if that made sense to you I I really hope you kind of get the feel of what
- 19:06I'm trying to say here but that is essentially why the syntax difference matters between a call
- 19:12back versus an event other than that everything else is still pretty much the same it's just the
- 19:17little syntax difference that you need to be able to understand why it is the way it is so for this
- 19:22last part of the video I'm actually going to make this into a challenge and I'm not going
- 19:26to lie to you this challenge is actually going to be very fun at least I think so anyways because
- 19:31essentially what we're going to do is we're going to create a tools shop system using everything we
- 19:37learned up until this point and there's three requirements I have for you to do while you try
- 19:42doing this Challenge on your own first and then coming back to see what I do once we go through
- 19:46this challenge together so the first requirement I have for you is to use a remote function obviously
- 19:53because that's the title of this episode the next requirement is to make at least three buttons or
- 19:58or tools for this shop system you can either go to the toolbx to look at some tools for you to
- 20:07use inside of your game or you can try creating your own tools for your shop but either way works
- 20:12for you the third requirement is I want you to use the Clone function to take the bot tool and put it
- 20:19inside of the player's backpack after the player has bought whichever tool that they purchased in
- 20:25the shop and if you don't remember how to do this I highly encourage you to refer back to my tools
- 20:31episode where I went through a bunch of properties that will definitely help you with this challenge
- 20:35so so just putting that out there before you start this challenge so with that being said pause the
- 20:40video try this challenge out on your own first and then come back and we'll go through this together
- 20:44all right so first thing we're going to do uh for the sake of time we're just going to find three
- 20:50random tools in the toolbox and just use that and just use those as our tools so I think first
- 20:56thing down here I can actually see that there's the linked sword uploaded by Roblox so let's add
- 21:02that in uh let's hit okay put tool in the starter pack yes let's do that okay and let's look for two
- 21:10more so I'm just going to search up tool and let's look for two more things so there's a flashlight
- 21:15tool let's just take this uh and it looks like there's also a knife so we might as well take
- 21:21that as well all right and they all should now be inside of our starter pack let's also rename these
- 21:27tools to make it more simple so we'll call Classic sword sword you'll call flashlight flashlight and
- 21:35then we'll keep knife as it is now we need a place to store these tools so that we can fetch them by
- 21:42the server uh and probably one of the most ideal places is server storage so let's actually take
- 21:49all of these tools and let's put them inside of server storage now what we'll do is create
- 21:55a shop so let's create another screen GUI here let's rename this to shop let's just open up our
- 22:03previous screen GUI and let's copy everything that's into here by hitting control Z and then
- 22:08just pasting it all into here so that we don't so that we can just reuse what we currently have so
- 22:13for our prompt let's change the text to Welcome to my shop and let's just Center this a little
- 22:21bit and let's also make this buy button one of the tools so let's say sword for instance let's
- 22:26rename this to sword and let's change this text to buy sword and ideally we should make all these
- 22:35tools different prices so let's make this sword uh 10 coins let's just move this over here to the
- 22:42left and then let's duplicate it and move this to the center and let's call this one flashlight and
- 22:51let's change the text to Flashlight and let's even change the amount to let's say 15 all right and
- 23:03let's duplicate that again put it over here this one is going to be the knife and we will make this
- 23:09knife 20 coins so let's BR to knife 20 coins so this is now what our shop should look like on the
- 23:17GUI side but now let's open up our script inside of our shop and first thing we will do is instead
- 23:24of make reference to one buy button we need to make references to all three buttons so that
- 23:29we can make all of them functional so what we're going to do is rename by button to sword button
- 23:37and change the reference to sword and let's copy and paste this two more times so that we can get
- 23:45reference so that we can get references to the other buttons so this one will be flashlight so
- 23:50this one's going to say flashlight and this one is going to say knife and this one's going to
- 23:57be a knife and so let's just change this variable here from buy button to sword button and in order
- 24:05to make all these buttons functional we need to copy what we have with our code and just paste it
- 24:10in like two different times so that we can make all of them functional now this already does
- 24:15look pretty messy I would not recommend doing it like this what I would recommend is putting
- 24:21all of these buttons inside of a table and looping through that table so that you can just write this
- 24:28code section only one time you don't need to write it like three different times like I'm doing here
- 24:33and so now we need to figure out how we can pass in this information to the server to know which
- 24:38button is being pressed what we can do inside of the server is create additional parameters to let
- 24:44us know which tool we're trying to look for when we are uh making this purchase so let's actually
- 24:51create two parameters here so the first one is going to be the tool name and then the second
- 24:59parameter is going to be the tool price when we implicitly call player we want to pass in these
- 25:06two parameters as well so that we know which tool we're trying to look at so let's go back into our
- 25:11local script and let's add in our additional arguments now remember player is already
- 25:19implicitly being thrown into here so we can skip throwing in player what we do need to put in is
- 25:24the tool name and the tool price so in quotations we're going to say sword and then we'll space it
- 25:33out with a comma and we will put in the price which is going to be 10 coins and then we will
- 25:38do the same thing down here with the flashlight oh and uh I meant to change these to Flashlight
- 25:45button and then down here to knife button so that we're not doing this like three different
- 25:51times uh and so now inside of our invoke function inside of our invoke server function here let's
- 25:57call this one flashlight and this one is going to be 15 coins so now down here we're going to
- 26:04say knife and this one's going to be 20 coins so now we are effectively passing in uh arguments
- 26:12into this into this callback so that we know which tool we're talking about and so first thing we can
- 26:20do is replace our coins value condition instead of it being 50 all the time let's replace this
- 26:28with the tool price and we will do the same thing down here when we remove values from the coins so
- 26:34now we have that done but now what we will do is we're going to get the tool from server storage
- 26:41use the tool colon clone function and then put it inside of the player backpack the way we're
- 26:47going to do that is by dropping two lines down here or actually three lines um after we after
- 26:53we know that the player has enough to be able to buy this tool we're going going to first check in
- 27:00server storage to see if we're able to find the tool that we're looking for so what we're going
- 27:04to say is local tool equals game. server storage and we need to check if it's here so we're going
- 27:12to say colon find First Child open parentheses close parenthesis and inside of here we're going
- 27:20to pass in our tool name Tool name because our tool name could be sword or flashlight or knife
- 27:27so we're trying to look for this inside of our inside of our server storage and now let's check
- 27:32to see if it actually does exist so we'll say if tool then we're going to create a clone of that
- 27:39tool and put it inside of the player's backpack so we'll say local cloned tool equals tool colon
- 27:47clone open parentheses close parenthesis and then we're going to set the parent of this clone tool
- 27:53so clone tool. parent equals the player players backpack which is going to be player. backpack
- 28:02so now we have successfully allowed the server to check whether this tool exists inside of server
- 28:08storage and if it does like let's say it's a sword then we're going to clone this tool and
- 28:14we're going to place this inside of the player backpack while also decrementing the coins value
- 28:20while also telling the client that this purchase has been successful and we're going to send this
- 28:25information back to the client so we're pretty much done here this was basically our whole script
- 28:31now again there are definitely better ways of handling this code especially this right here when
- 28:37we're duplicating a lot of stuff by using a table and then looping through all those buttons inside
- 28:41of a loop rather than creating three different Mouse button oneclick events but other than that
- 28:46I'd say our system we have is pretty good so let's test it out inside of our game all right
- 28:51so we've loaded into the game I have 50 coins and let's see what happens if I try buying the sword
- 28:57so as you can see first thing that happened was that it checked if we had enough coins for 10 and
- 29:03it did so we decremented 10 coins after buying the sword the second thing that happened was
- 29:10that the server checked server storage to see if it had the sword we were looking for and it did
- 29:15so the server created a clone of this tool and put it inside of our backpack so if we open up
- 29:21players our player and then go to backpack we can see that our sword has successfully been placed
- 29:27inside of our um backpack so that was successful and the last thing that happened was that the
- 29:34server sent the message back to the client to tell us that the purchase has been successful
- 29:40therefore we updated this text label to say the purchase has been successful and we did all of
- 29:45those things using remote functions now let's see what happens if we try buying a flashlight we can
- 29:50see that we now have the flashlight inside of our backpack and we also decremented uh 15 coins from
- 29:58buying this one now let's try buying the knife and we have also successfully bought the knife
- 30:03so it works as intended now let's try buying something else we can't afford anything now so
- 30:08let's try buying something and as you can see the purchase has failed because now we only have five
- 30:12coins and not at least 10 to buy any of these so we're basically done here and what we got out of
- 30:19this was a pretty nice shop system that allows us to be able to buy these tools so I hope you
- 30:24found this challenge to be very interesting uh I definitely had fun explaining how this uh shop
- 30:30system worked and I think we're starting to be able to apply our skills to be able to create
- 30:36actual systems inside of our games so I think this was definitely a fun challenge to to do
- 30:41so that's going to be it for this episode on remote functions I hope you learned a
- 30:45lot from this episode we were able to create a shop system using everything we've learned
- 30:49up until this point and I think we're starting to make more and more progress into creating
- 30:53bigger and better systems for Roblox games and I hope you'll be sticking around for more episodes
- 30:59to come in the future but other than that that's pretty much it for this episode be sure to join
- 31:03my Discord server the link is in the description and I will see you in the next episode take care
About this transcript
This page contains the full transcript of Remote Functions (Two-Way Communication) - Roblox Advanced Scripting #6 by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 5,512 words across 296 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.