Gamepasses (One-Time Robux Pass & DataStore Receipt) - Roblox Advanced Scripting #21 (2024) — Transcript
Full transcript
- 0:00Bro, who does this guy think he is
- 0:02trying to creep up on me? Oh my Wait,
- 0:05dude. I'm trying to get away from this
- 0:06guy, but it's not working. Bro, he keeps
- 0:08following me. What the heck is going on
- 0:11right now, dude? I need to get rid of
- 0:13this guy somehow, cuz Oh my god, he's
- 0:16not going to leave me alone. I noticed
- 0:18that as I was looking at this creep, I
- 0:20see this thing down here that says,
- 0:21"Click me if you're mad." What does this
- 0:23thing actually do? Oh, would you like to
- 0:26buy banan creep
- 0:29for 10 Robux? That's not a bad idea to
- 0:32get rid of this guy. All right, let's
- 0:34see if I can do that. All right,
- 0:35purchase has been successful. Oh, yeah.
- 0:39Got rid of him. Thank god. Wait, what?
- 0:42He's back. What? Pay to win is a lie.
- 0:45Remember that. Oh my god. Are you
- 0:49serious right now, bro? All right, let's
- 0:52think about this for a minute.
- 0:53Unfortunately, I just can't seem to get
- 0:55rid of this guy. This guy's just gonna
- 0:57have to stick with me this whole time, I
- 0:59guess. But anyways, what ended up
- 1:01happening was I saw this button down
- 1:03here that said, "Click me if you're
- 1:05mad." And when I did click on it, uh,
- 1:07Roblox prompted this little GUI that I
- 1:10did not create. And this basically asks
- 1:12me, would I like to buy this item, also
- 1:15known as ban creep, for 10 Robux? And
- 1:17for whatever reason, if you're not
- 1:18familiar with Roblox, it's essentially
- 1:20Roblox's universal currency that's used
- 1:23across the entire platform that can be
- 1:25used to buy these sorts of things. This
- 1:27is essentially called a game pass. And
- 1:29what this does is when you buy a game
- 1:32pass, like right now, uh you buy a game
- 1:34pass and what ends up happening is that
- 1:36the server uh does something to show
- 1:39that you have now bought this game pass.
- 1:41So, this game pass can pretty much be uh
- 1:44getting access to a specific area in the
- 1:47game. Uh it can also just be getting
- 1:49some sort of privileged advantage over
- 1:51other players. Not too big of an
- 1:53advantage, but somewhat of a one-step
- 1:56level advantage from other players. And
- 1:58the thing about game passes is that it's
- 2:00a one-time purchase. So, if you buy a
- 2:03game pass, then you don't have to buy it
- 2:05ever again if it's on the same account.
- 2:08So, let's say I did decide to buy this
- 2:10game pass that says, "Would you like to
- 2:11buy ignore?" I'm going to buy this for
- 2:13one Robux. Uh, the purchase has been
- 2:16successful. And then what should happen
- 2:17in the game is that I'm given this
- 2:20Minecraft diamond sword. Don't don't ask
- 2:22me why, but that's basically what ended
- 2:25up happening. And since I bought it, I
- 2:27don't have to buy it ever again. If I
- 2:29reset my character, then what ends up
- 2:32happening is that uh the game will check
- 2:33if I own this game pass and it will just
- 2:36give me this sword again once I refresh
- 2:38into the game. So, I ended up getting
- 2:40the sword again in my backpack because
- 2:42the game checked if I had this game pass
- 2:44now that I did buy it. And that is
- 2:46basically uh one way of using game
- 2:48passes. It's a way for you to get
- 2:50specialized privileges within a Roblox
- 2:53game that you only have to buy one time.
- 2:56And that's what I'm going to be showing
- 2:57you how to do in this episode, which is
- 2:59creating a game pass, implementing it,
- 3:01and keeping track of our players
- 3:03purchase history using data stores at
- 3:05the end of this video. So, with that
- 3:07being said, let's get straight into
- 3:09implementing game passes inside of
- 3:11Roblox Studio.
- 3:13Oh my god, no way. This diamond sword
- 3:15actually killed the creep. Oh my god,
- 3:17this is incredible. Okay, but anyways,
- 3:19let's get straight into implementing.
- 3:21Now, now that we understand it, let's
- 3:23add game passes into our game. But
- 3:25before we do, we need to make sure that
- 3:26our game is published. So if you're in a
- 3:29local file right now, I want you to go
- 3:31on the top left, hit file, and then hit
- 3:33publish to Roblox as. And you can
- 3:36basically publish this to any game that
- 3:38you choose. Uh my game is already
- 3:40published, so I don't need to do that.
- 3:41But just make sure you do that first
- 3:43before we move on to implementing game
- 3:45passes. But now that we've gotten that
- 3:47taken care of, let's actually create a
- 3:48game pass. And you might ask, where do
- 3:50we create this game pass? Because if you
- 3:52remember from our badges, all we had to
- 3:54do is just go to home, go to game
- 3:56settings, and then go to monetization.
- 3:57But if we come over here, we see that
- 4:00there's no game passes. So, where the
- 4:02heck do we So, where the heck do we
- 4:04create these game passes? We're going to
- 4:06have to go to the Roblox website in the
- 4:09creator hub uh to create our game passes
- 4:12on our creator page. So, that's where
- 4:14we're about to be headed, and I'm going
- 4:15to meet you there. All right, so we're
- 4:17in the homepage of Roblox. In order to
- 4:18access our game passes, we need to go to
- 4:20our creator hub by clicking on this
- 4:22button on the top that says create. This
- 4:24will bring up our creator dashboard. And
- 4:26so let's look for our game that we've
- 4:29just published. In this case, for my
- 4:31game, it's called data store test one.
- 4:32So we're just going to click on the name
- 4:34of it. So this will bring up the entire
- 4:38um overview. This will bring up the
- 4:40entire creator dashboard of this game.
- 4:42So what we want to do is go on the left
- 4:44side, go down to monetization, and then
- 4:46click on passes. This should give us a
- 4:49list of all of our game passes that are
- 4:51within the game currently. Uh just
- 4:53ignore these two that are here right
- 4:55now. Um but this should be blank for you
- 4:57if you've created a new game. So in
- 4:58order to create a game pass, we need to
- 5:00click this button that says create a
- 5:01pass. And then it'll bring up a page
- 5:04where we can give the name of our game
- 5:06pass. We can change the image of it, but
- 5:08Roblox will give us an image by default.
- 5:10So we don't have to worry too much about
- 5:11that. And you can also add a
- 5:12description. The only field that's
- 5:14required is the name. So let's just do
- 5:16that really quick. We're going to call
- 5:17this game pass uh test one just to keep
- 5:21it simple. Uh and then we're going to
- 5:22hit create pass. So once we've created
- 5:25the pass, it should be noted right here.
- 5:28But the thing is is that we've created
- 5:29the pass, but there's no way for us to
- 5:32actually buy it. So what we have to do
- 5:34is click on this and then there should
- 5:37be a section here that says sales. And
- 5:40so now what we can do is turn on item
- 5:43for sale. And we can put whatever price
- 5:45we want in here for the player to
- 5:46purchase. The thing about the Robux
- 5:48amount is that the creator gets 70% of
- 5:52however much the price of this game pass
- 5:54is. So if let's say I priced it at 100
- 5:57Robux, then the creator gets 70 Robux
- 5:59out of that 100 Robux. So you can price
- 6:02this to be whatever you want. I can just
- 6:03price it to be 100 just to keep it
- 6:05simple. So we'll hit save changes. All
- 6:07right. And we have now done that. But
- 6:09here's another thing. If we create a
- 6:10game pass, then we should automatically
- 6:12have this in our inventory. So, we don't
- 6:14need to buy this game pass uh within the
- 6:17game. But for testing purposes, we might
- 6:19want to try and buy this game pass. So,
- 6:22we need to get rid of this game pass
- 6:23from our inventory temporarily so that
- 6:25we can buy it in studio to test it.
- 6:27Okay. So, I'm back on the Roblox page.
- 6:29We're going to go to inventory. And then
- 6:31what we're going to do is go to passes
- 6:34down here. and you should be able to
- 6:37find the path that you just created,
- 6:39test one, and let's click on this. So,
- 6:42what we're going to do is go on the top
- 6:43right. We're going to hit delete from
- 6:45inventory. So, once we do this, then we
- 6:47should be able to test by this uh in
- 6:50Roblox Studio. We also want to make sure
- 6:52we have the asset ID of this game pass
- 6:54saved. So, I'm going to click this
- 6:56button and click on copy asset ID. So,
- 6:59now this should be on our clipboard so
- 7:01that we can add this game pass into our
- 7:04Roblox game. Okay, we have our initial
- 7:06setup done. Now, let's get to scripting
- 7:08this game pass. So, what we're going to
- 7:09do is make a script in server script
- 7:12service really quick. So, we're just
- 7:13going to do this. Uh, we're going to
- 7:15delete these lines here. Okay. So, in
- 7:17order to manage anything related to game
- 7:19passes, we need to use a special service
- 7:22for this. Now, there are two services
- 7:24within the Roblox developer realm that
- 7:26people that people do get confused over.
- 7:29So there's this service called game pass
- 7:32service and there's another service
- 7:34called marketplace service. So you might
- 7:37ask which one of these two services do
- 7:39we use since both of them sound like
- 7:41they could work with handling game
- 7:43passes. I'm going to say this right now.
- 7:45You should not use game pass service
- 7:47because not only is it a deprecated
- 7:49feature but also marketplace service has
- 7:52everything that game pass service does
- 7:54but in a more modern and safe way. So
- 7:57this is what game pass service would
- 7:58look like. So we would say local game
- 8:01pass service equals game colon get
- 8:04service game pass service. So we do not
- 8:09want to use this. In the past I have
- 8:11used game pass service and and this has
- 8:13now been superseded by marketplace
- 8:16service. So this is what it's going to
- 8:17look like. Instead of game pass service
- 8:19we're going to say marketplace service.
- 8:23And we're basically going to do the same
- 8:24thing here with marketplace service. All
- 8:28right. All right. So, now that we know
- 8:29to use marketplace service, let's also
- 8:31make our game pass ID. So, we're going
- 8:33to say local game pass ID equals. And
- 8:37then we're going to paste our ID for the
- 8:40game pass we just created. So, it should
- 8:41look something like this. This script is
- 8:43going to be used to check whether the
- 8:45player has a current game pass or not.
- 8:47But for right now, what we're interested
- 8:49in is prompting the user to buy the game
- 8:52pass. And the way we do this is actually
- 8:54by using a local script instead of a
- 8:57script because we want to be able to
- 8:58display it on the player screen for them
- 9:00to be able to click it and then send it
- 9:02over to the server. So we're also going
- 9:04to create a local script probably in
- 9:06starter character scripts just to test
- 9:08this out. So we're going to hit the plus
- 9:10sign include a local script and we're
- 9:12basically just going to copy the same
- 9:14code that we had here. So we're going to
- 9:16copy this and then delete this, paste it
- 9:18into here. Okay. Okay. And so now what
- 9:20we're going to do is get our local
- 9:22player. We're we're just going to say
- 9:24local player equals game.players.local
- 9:28player. So now let's prompt the player
- 9:30to buy the game pass. What we're going
- 9:32to do is simply just say marketplace
- 9:35service colon prompt game pass purchase.
- 9:41And this takes in two arguments. The
- 9:43first one is going to be the player
- 9:45instance. Now, I now this is another
- 9:47thing people get tripped up about is
- 9:49whether they should pass in the player
- 9:51object itself or the player's user ID.
- 9:54We're just going to be passing in the
- 9:56player, which is our local player. Since
- 9:58we're using a local script and so now
- 10:00the second argument is going to be the
- 10:02game pass ID, which is going to be right
- 10:04here, contained in this variable called
- 10:06game pass ID. So, we're just going to
- 10:07pass in game pass ID. So, this is
- 10:10essentially how you prompt a game pass
- 10:12purchase to the player. If we go into
- 10:14the game, hit test, and hit play, then
- 10:16we should be able to see that as soon as
- 10:17we join into the game, Roblox should
- 10:20send us a prompt to buy the game pass
- 10:22that we just created. All right, so it
- 10:24says, "Would you like to buy test one
- 10:26for 100 Robux?" Now, thankfully in
- 10:28Roblox Studio, it allows us to test the
- 10:31purchase. So, we don't have to actually
- 10:33spend 100 Robux to buy this item. So, we
- 10:36could just simply hit this and we should
- 10:37be able to buy this just normally. So,
- 10:39it says it's been successful, but it
- 10:41doesn't do anything right now because we
- 10:43didn't tell the server to do anything
- 10:45once we bought this thing. So, that's
- 10:46what we're going to do right now. And
- 10:48what we're going to do is set up an
- 10:50event for marketplace service anytime
- 10:53that a game pass purchase has been made.
- 10:55And what we're going to do is say
- 10:56marketplace service
- 11:00prompt gamepass purchase finished. Oh
- 11:04jeez, that is a long name. Okay. Okay.
- 11:06And then we're going to connect this to
- 11:07a function. And this passes back three
- 11:10parameters. So the first one is the
- 11:13player instance. The second one is the
- 11:15ID that was passed through. And then
- 11:17finally a boolean to tell us whether
- 11:19this purchase was successful or not. So
- 11:21we'll just say uh purchase successful.
- 11:24So we're going to be using these three
- 11:27arguments to tell us which player bought
- 11:29what game pass and if it was successful
- 11:31or not. So down here, what we're going
- 11:33to do is make is make an if statement by
- 11:35saying if purchase successful and ID. So
- 11:41this ID right here is set equal to our
- 11:44game pass ID uh for test one. If this is
- 11:48equal to game pass ID, then what we're
- 11:50going to do is just simply print out
- 11:52that um let's say player.name and then
- 11:55we're going to concatenate this by
- 11:56saying has bought a game pass. Okay, so
- 12:01now we've told the server that uh if a
- 12:03game pass purchase has been has been
- 12:04made, then we're going to print to the
- 12:06output saying that this player has
- 12:07bought a game pass. Oo, I don't know how
- 12:09I made this mistake, but this should not
- 12:12be in the local script. This should be
- 12:14in the regular script up here. My
- 12:15apologies for getting confused on that,
- 12:17but it should be working now. You don't
- 12:18want to make that mistake. I'm telling
- 12:20you that right now. Okay, so we're going
- 12:22to buy test one. And what should happen
- 12:23is that as soon as it's done, then it
- 12:25should print down here that that Brawl
- 12:27Battle has bought a game pass. So, it
- 12:29should be working basically as intended.
- 12:31And so, that is how we can check if a
- 12:33specific player has bought a specific
- 12:35game pass. What we're about to do now is
- 12:37check if the player owns a game pass if
- 12:39we join to the game. But before we do
- 12:42that, I want to address some things. So,
- 12:44I'm currently in the games page of the
- 12:46game that we're working with. And
- 12:48in-game purchases for game passes isn't
- 12:50the only way for you to buy game passes
- 12:52because you can also buy game passes
- 12:54within the games page of the game that
- 12:57you're playing. going down here into the
- 12:59store tab. So, if we click this, we can
- 13:01actually see all of the game passes that
- 13:03are available to buy. If you buy one of
- 13:06these game passes, then you can join
- 13:08into the game and the the game can check
- 13:10if the player has this game pass without
- 13:13having them buy the game pass in the
- 13:16game. I want you to be able to
- 13:17understand this and know that there's
- 13:19two ways of of players buying game
- 13:22passes. It can either be through the
- 13:23games page or it can be in the game
- 13:25directly. And you need to be able to
- 13:27handle both of these cases if that does
- 13:29end up happening. All right, so we're
- 13:31back into the game. What we want to do
- 13:33now is check if the player already owns
- 13:35a game pass when they join into the
- 13:37game. So let's go back to our script and
- 13:40on top of here, we're going to make a
- 13:42player added event. So we're going to
- 13:44say game.players.player
- 13:46added connect function open close
- 13:49parenthesis pass in the player. Enter.
- 13:52What we're going to do is check whether
- 13:54the player owns a specific game pass.
- 13:56And here's how we're going to do it.
- 13:57We're going to say local has game pass
- 14:01equals marketplace service
- 14:05colon user owns game pass async. And
- 14:10this takes in two arguments. So you
- 14:12might have noticed that the first
- 14:14argument says user ID. It does not say
- 14:17the player object. But this is
- 14:19specifically saying that it wants the
- 14:21player's user ID. So that's what we're
- 14:23going to do. We're going to pass in the
- 14:24player user ID. And then the second
- 14:27argument is just going to be the game
- 14:28pass ID, which was up here. So we're
- 14:31just going to pass in game pass ID. So
- 14:34this should return a boolean of whether
- 14:36the player owns this game pass or not.
- 14:37So let's check if the player does have
- 14:40the game pass. Then we we could just
- 14:43check this by saying print uh
- 14:45player.name name uh concatenate this
- 14:48with a string by saying has a game pass
- 14:52upon joining.
- 14:54And there we go. That's this is a really
- 14:57basic way for us to test whether the
- 14:58player has a game pass if they join into
- 15:01the game. Uh, if we join into this game
- 15:03right now, um, I'm pretty sure this is
- 15:05not going to fire because we currently
- 15:07don't have this item in our inventory.
- 15:09And buying this through Roblox Studio
- 15:12does not count as adding this into our
- 15:15inventory. So, if we buy this game pass,
- 15:17then nothing should really change here.
- 15:20Uh, I I definitely don't see in the
- 15:23output that I own this game pass because
- 15:25I'm going to have to actually go into
- 15:26the game to buy this. So, let's check.
- 15:28If we don't have the game pass, then it
- 15:31should be able to display um then it
- 15:34should be able to display it properly in
- 15:35the output. So it should say that brawl
- 15:38battle has a game pass upon joining. But
- 15:40in reality we don't because we uh got
- 15:43rid of it from our inventory. Okay, so
- 15:45this has basically been taken care of.
- 15:46But the thing is if we have a function
- 15:49that returns something, we don't want to
- 15:51just call this one time and expect a
- 15:53guaranteed response because things can
- 15:56always go wrong with the network. And so
- 15:58we don't want to rely on this happening
- 15:59only once and something could definitely
- 16:01go wrong and we want to be able to
- 16:03handle it accordingly which is why we
- 16:05want to wrap this in a pall. So if you
- 16:07remember how palls are structured let's
- 16:09just comment this out really quick uh
- 16:11and drop a line down here. What we're
- 16:13going to do is say local success,
- 16:16error message equals pall function open
- 16:20and close parenthesis enter. And so
- 16:22we're basically just going to take our
- 16:24user owns game pass async um method and
- 16:27just paste it down here. So this is a
- 16:30safer way of knowing whether the player
- 16:32has a game pass or not. So instead of
- 16:34saying down here if has game pass, let's
- 16:36actually comment these out and let's uh
- 16:39indent this properly. What we can do
- 16:41instead is just simply check if this was
- 16:43not successful. So if not success then
- 16:48uh we'll write in an error message in
- 16:50we'll write an error message into the
- 16:52output. So instead of saying print we'll
- 16:54just simply put an error here and we're
- 16:56going to pass in our error message. Uh
- 16:59you can use an error or you can use a
- 17:01print statement. It doesn't really
- 17:02matter which one you use uh as long as
- 17:04you can see it in the output. Basically
- 17:07the difference here is that if we use an
- 17:08error message it's going to print it in
- 17:10red instead of white. So that's the only
- 17:13difference. So yeah, now that we've
- 17:14checked this, if this was not
- 17:15successful, then then we're going to
- 17:16print the error message. And we can also
- 17:18return this too, so that we don't want
- 17:20this to do any other weird things down
- 17:22here uh if it doesn't go well. Oh, and
- 17:25there's one thing we forgot to do, and
- 17:26that is incorporate the has game pass
- 17:28boolean. The reason we need to do this
- 17:30is because this can be successful, but
- 17:34it could say that the player does not
- 17:36have the game pass or the player does
- 17:37have the game pass, which is why we need
- 17:39to have the success variable, but also
- 17:41have a has game pass variable at the
- 17:44same time so that we can make the script
- 17:45do what it needs to do if the player
- 17:47actually does have the game pass. So,
- 17:49we're just going to drop a line down
- 17:50here. Say local has game pass equals
- 17:53false. And let's throw it down here by
- 17:56saying has game pass equals whatever
- 18:00this is going to give back to us. And so
- 18:02if this was not successful then throw an
- 18:04error message. And we can say down here
- 18:07again if has game pass then do whatever
- 18:10we we need to do down here. So we
- 18:11actually did need this all along. So
- 18:13this is the secure way of uh adding
- 18:16palls to our game passes when we
- 18:19implement them inside of our scripts.
- 18:21All right. Now, we're about to do
- 18:22something interesting, which is uh
- 18:25giving the player a tool if they own a
- 18:27certain game pass. Whether they join
- 18:29into the game after buying a game pass
- 18:31or if they have bought the game pass in
- 18:33the game, we want to be able to give
- 18:35this tool to the player and keep it on
- 18:38them even if they reset their character.
- 18:40So, what tool are we going to use? Um,
- 18:42we can basically just go to the toolbox
- 18:44and find whatever tool that we're going
- 18:45to use. Uh, maybe we can use that
- 18:47diamond sword that I showed you in the
- 18:48beginning in the beginning of the video.
- 18:50Uh, so let's just go to models and
- 18:52search up sword. Uh, and I think it
- 18:55should be right here. Yeah, Minecraft
- 18:57diamond sword. Let's just take this. Hit
- 18:59okay. So, we're going to take our
- 19:02Minecraft diamond sword. We're going to
- 19:03move this inside of server storage so
- 19:05that we can uh use this to put it inside
- 19:08of the player's backpack uh if they do
- 19:11own the game pass. Uh, we're also going
- 19:12to rename this to uh let's just say
- 19:15diamond sword with no spaces just to
- 19:17make it simple. Okay, so now that we
- 19:19have that taken care of, let's go back
- 19:21to our script. And what we want to do
- 19:24first is check if the player has
- 19:27successfully bought this game pass so
- 19:28that we can actually give the tool to
- 19:31the player uh once they buy the game
- 19:34pass. So first thing we're going to do
- 19:36is locate our diamond sword. We're going
- 19:37to say local sword equals game.server
- 19:41server storage
- 19:44colon find first child open and close
- 19:47parenthesis diamond sword. And if the
- 19:50sword does exist, because there's a
- 19:53chance that it might not, then we're
- 19:54going to uh drop a line down here. And
- 19:56then we're going to create a clone of
- 19:58the sword and then put it inside of the
- 20:00player's backpack. So, what we're going
- 20:02to do is say local cloned sword equals
- 20:06sword colon clone open and close
- 20:07parenthesis. And then we're going to set
- 20:09the parent of the cloned sword.
- 20:12Equals uh our player backpack. Okay, so
- 20:17now we're going to be able to give the
- 20:18player this sword into their backpack
- 20:20once they buy the game pass. However,
- 20:22we're not done here because let me show
- 20:24you what happens if we buy this game
- 20:26pass right now. If we decide to buy test
- 20:29one right now, then we hit okay. What
- 20:31should happen is in the output it says
- 20:33our diamond sword is in our backpack.
- 20:35Sweet. But what happens if we reset our
- 20:37character? If we reset our character,
- 20:40then our item is gone. We can't use it
- 20:43anymore. Where did our item go? If if
- 20:46tools are in a player's backpack, that
- 20:48doesn't mean that it's going to stay in
- 20:50the player's backpack. We have to use
- 20:52what's known as starter gear. If you
- 20:55remember from my tools video way back
- 20:56earlier in the series, um we use starter
- 20:59gear to basically say that if whatever
- 21:02tools in here, it's going to be cloned
- 21:04into the player's backpack every single
- 21:06time they reset their character. So, not
- 21:08only do we have to add the So, not only
- 21:11do we have to add the sword to the
- 21:13player's backpack, but we also have to
- 21:14add it to the player's starter gear as
- 21:16well, so that it'll save uh whenever the
- 21:19player resets their character. So,
- 21:21here's what we're going to do. We're
- 21:22first going to check if the player does
- 21:24not have this sword inside of starter
- 21:26gear. And then if it if they don't, then
- 21:28we're going to add this to their starter
- 21:29gear. So we're going to make a simple if
- 21:31statement. We're going to say if not
- 21:34player
- 21:36startergeear
- 21:38colon find first child. And we're not
- 21:41trying to look for this sword instance.
- 21:43We're trying to look for uh a name of
- 21:46the sword that resembles it. So we're
- 21:48just going to say sword.name.
- 21:50So, if you were to imagine this, we have
- 21:52our starter gear and we open it up. We
- 21:54want to see if there is a tool that's
- 21:57called the sword, which is the diamond
- 21:59sword. So, if they don't have it, so
- 22:01then what we're going to do is make
- 22:03another sword. We're going to clone it
- 22:05again and then put it inside of the
- 22:06player starter gear. So, we can just
- 22:08copy this code right here and then paste
- 22:10it in here. But this time, we're going
- 22:12to set the parent of this to be starter
- 22:15gear. So now this should work if we join
- 22:18into the game, buy the game pass, and
- 22:20then reset our character. We should be
- 22:23able to keep it once we reset our
- 22:24character. So let's buy the game pass.
- 22:27Let's hit okay. Not only will this be
- 22:29added to our backpack, but this will
- 22:31also be added to our starter gear uh as
- 22:33well. And if we reset our character,
- 22:36then what should happen is once we go
- 22:37back in, then we should still have our
- 22:39diamond sword in our backpack. Okay, but
- 22:43this was dealt only if the player bought
- 22:45the game pass in the game. But we also
- 22:47want to check if the player who
- 22:50previously bought this game pass before
- 22:51joining into the game in the stores page
- 22:53like I like I showed you earlier uh
- 22:55joins into the game and checks whether
- 22:57the player has the game pass or not. We
- 22:58want to do the same thing up here that
- 23:00we did down here except this is going to
- 23:02be if the player joins into the game for
- 23:04the first time. And we basically have
- 23:06the initial setup here done already. If
- 23:08the player does have this game pass then
- 23:09they have this game pass. So what we can
- 23:11do down here is just simply copy what we
- 23:14already had down here and just paste it
- 23:16into here. So it pretty much does the
- 23:19exact same thing as it did before. But
- 23:21this is a lot of copied code. So what we
- 23:23could do is create a function that helps
- 23:25us with this. Uh we could just say local
- 23:28function
- 23:30give sword open and close parenthesis
- 23:32enter. And all we need to do is just
- 23:34pass in the player in this function. So
- 23:37we could just copy this. So, we could
- 23:40just cut this, paste it into here. Uh,
- 23:42and then we could just call give sword
- 23:44by passing in the player argument. So,
- 23:47we could just pass in the player here.
- 23:49And then we can also just copy this and
- 23:51pass it into here as well. Uh, give
- 23:54sword, pass in the player. All right.
- 23:56And there we go. This is how we give a
- 23:58tool to a player if they own a game pass
- 24:01or if or do anything for that matter if
- 24:03a player does own a game pass. So, I'm
- 24:05glad I was able to show you a practical
- 24:07use case of using a game pass. So for
- 24:09this last part of the video, I want to
- 24:11show you how we can add data stores to
- 24:14store purchase history information of
- 24:16the player if they decide to buy game
- 24:18passes within the game. So let's say I
- 24:20did buy the game pass in the game. Um
- 24:23what if we wanted a way to store that
- 24:25information so that we can retrieve it
- 24:27back um to let us know that oh yeah,
- 24:30this player did buy a certain game pass
- 24:33uh during this specific time. We can do
- 24:34that using data stores. And I just find
- 24:37this to be very helpful if let's say a
- 24:39player decides to buy a game pass, but
- 24:40then there were some issues with
- 24:42granting whatever privileges the game
- 24:44pass was supposed to give to the player,
- 24:45but it just ended up not working. So
- 24:47then that player decides to go to the
- 24:49creator being like, "Hey, the game pass
- 24:51does not work. Do you think you can give
- 24:52me a refund or maybe check this out and
- 24:54see whether um you can fix this in your
- 24:57game?" One way for you to tell if this
- 24:59player does have the game pass is by
- 25:01looking at their purchase history data
- 25:03store. So, that's essentially what I'm
- 25:04going to be showing you how to do uh for
- 25:06this last part. Now, things will get a
- 25:08little complicated and there is one
- 25:10caveat to this is that this will not
- 25:12work if the player bought the game pass
- 25:15on the games page before joining into
- 25:17the game. Now, it now it could work, but
- 25:20it just adds more complication to the
- 25:22whole thing. You're going to have to
- 25:24check if the player has bought this for
- 25:25the first time when they join into the
- 25:26game and then do all these other sorts
- 25:28of things with data store switching.
- 25:29It's it gets pretty complicated. So, so
- 25:32what I'm going to show you is how to
- 25:33store data of a purchase transaction if
- 25:36a player buys a game pass in the game.
- 25:38So, what we're going to do is go to our
- 25:40script and at the very top we're going
- 25:42to get access to our data store service,
- 25:45which we're just going to say local data
- 25:47store service equals game get service
- 25:51data store service. And we want to use a
- 25:54data store to store all this
- 25:55information. Uh we can call this uh
- 25:58purchase history. And we're going to set
- 26:00this equal to data store service colon
- 26:06get data store. We're just going to be
- 26:08using a normal data store. We're not
- 26:10going to be using an order data store or
- 26:11anything like that. Uh and this is just
- 26:13simply going to be called purchase
- 26:15history. Okay. Now I want you to
- 26:17understand how this purchase history
- 26:19data store will be structured. I want
- 26:21you to imagine it like this. If, let's
- 26:23say, the player decided to buy uh the
- 26:25game pass, then we want to process
- 26:28information about that game pass
- 26:30purchase to to have it be sent to our
- 26:33purchase history. So, let's say we
- 26:34wanted to have information about um when
- 26:36the purchase was made, uh what the what
- 26:39the game pass name is, how much it costs
- 26:42in Robux, and just things like that. We
- 26:45want to store all that information in a
- 26:46table and then have that as one value
- 26:49inside of the data store. So, I want you
- 26:51to imagine this as if it was a table
- 26:53full of tables uh with different
- 26:56transactions and different purchase
- 26:58histories within this one data store. I
- 27:00think it'll make more sense once I show
- 27:02you what this looks like when we add
- 27:03this in. But essentially, what we're
- 27:04going to do is go down to our script and
- 27:08this is going to happen if we buy the
- 27:10game pass within the game. What we're
- 27:12going to do is drop two lines and I'm
- 27:13just going to make some comments to
- 27:15expand our understanding of what we're
- 27:16doing here. So, the first thing we're
- 27:18going to do is create our receipt. Uh,
- 27:22basically our table full of information
- 27:23based on the item that we just bought.
- 27:25The the second thing we're going to do
- 27:27is get a player's purchase history data
- 27:32so that we can take the player's already
- 27:34existing purchase history and add our
- 27:36receipt onto it. And then finally, we're
- 27:38going to add our receipt to the purchase
- 27:43history data. So, this is how it's going
- 27:46to be broken down. And now we're going
- 27:47to And now we're going to go through
- 27:49each step. So, we're first going to
- 27:51create our receipt, and this is going to
- 27:53be a table essentially. So, it's going
- 27:56to be a table full of information that
- 27:57we might want to see if a player decides
- 28:00to buy a game pass. So, right now, I'm
- 28:02thinking of four things we could
- 28:03potentially add into here. So the first
- 28:05one would be the name. The second one
- 28:07would be the ID. The third one would be
- 28:10the date. And the fourth one would be
- 28:13the price of that game pass. So we would
- 28:15just say price down here. And as you
- 28:17know, this is how we structure our
- 28:19tables. It's separated by commas or
- 28:21semicolons if you choose to do that as
- 28:23well. But I'm just going to do commas.
- 28:24And these are all going to be set equal
- 28:27to a certain value. So the name in this
- 28:29case, we're just going to call this
- 28:31diamond sword. And for the ID, we're
- 28:34going to set this equal to uh the ID
- 28:37that was passed into here when we bought
- 28:39the game pass. And then we're going to
- 28:40skip date for right now. But for but for
- 28:43price, we're just going to say 100
- 28:44because that's how much the game pass
- 28:46was. And for this date, now this might
- 28:48be a little confusing because I never
- 28:50taught you how time is displayed in
- 28:53inside of Roblox. But I just want you to
- 28:55follow what I do for right now. So what
- 28:57we're going to do is set this equal to
- 28:58OS time open and close parenthesis.
- 29:01We're going to use quotations and then
- 29:03say percentage sign lowercase X. Don't
- 29:06worry about what this means right now.
- 29:07We're going to I'm going to go over this
- 29:08in a future video, but this essentially
- 29:10gives us the date of when the player has
- 29:13bought this game pass. So, we now have a
- 29:15receipt. What we need to do now is check
- 29:17is get the player's purchase history
- 29:19that's already existed. We're going to
- 29:21say local success data equals pall
- 29:26function open close parenthesis enter.
- 29:28We're going to return um purchase
- 29:32history colon get async because that's
- 29:35what we're going to try and retrieve and
- 29:38store it into data. We're simply just
- 29:40going to pass in the string of player
- 29:43user user ID. And so let's check if this
- 29:46was not successful. So, if this was not
- 29:49successful, then what we're going to do
- 29:50is throw in an error message saying that
- 29:53uh we failed to fetch purchase history.
- 29:58And we're also just going to return this
- 29:59because we don't want to add this to our
- 30:02because we don't want to add this to our
- 30:03data store if we were not able to
- 30:05retrieve the data store in the first
- 30:06place. Okay. But let's say if the data
- 30:08did fail, let's say if the player
- 30:11doesn't have any data to begin with,
- 30:14then what we're going to do is we're
- 30:15basically just going to set data to an
- 30:17empty table. So we're just going to say
- 30:19if data equals nil, then we're going to
- 30:23set data equal to an empty table. The
- 30:26reason again we are doing this is
- 30:28because the player could pot this could
- 30:30potentially be the first purchase that
- 30:31the player has ever done. And if it is
- 30:33the case, then we're going to set data
- 30:35equal to an empty table so that we can
- 30:37add our receipt to the table and then
- 30:39set that to the player purchase history.
- 30:41Okay. And so now we can add our receipt
- 30:44to our data table. So we're just simply
- 30:46going to use the table function called
- 30:48table.insert
- 30:50open and close parenthesis. So in our
- 30:53data table, we're going to add our
- 30:56receipt table that was up here. So,
- 30:58we're basically taking this receipt
- 31:00transaction and put it inside of our uh
- 31:02data table so that we can take this data
- 31:04table and set it to uh the player's
- 31:07transaction history. And so now down
- 31:10here, what we're going to do is say
- 31:11local success,
- 31:14error message just in case it does fail.
- 31:16We're going to set this equal to pall
- 31:18function open close parenthesis enter.
- 31:21We're going to simply say purchase
- 31:24history colon set async open and close
- 31:28parenthesis player user ID and then
- 31:32we're going to pass in our data table.
- 31:35And finally, let's do one more check to
- 31:37see if this was not successful. If not
- 31:40success, then error. And we're going to
- 31:43pass in our error message that we have
- 31:46up here. Okay, this is our entire script
- 31:48right here. I'm going to show you what
- 31:50this looks like within the game and then
- 31:52we're going to do one quick recap of
- 31:54everything that we coded up until this
- 31:56point. Okay. Now, one quick thing we can
- 31:57do is print out our results in the
- 32:00output so that we can see uh our data in
- 32:03our purchase history for the specific
- 32:04player. Uh, so what we're going to do is
- 32:06just simply say up here with our player
- 32:08added event is go down here and say
- 32:11local data equals purchase history colon
- 32:16get async and then pass in the player
- 32:20user ID. Then what we're going to do is
- 32:22check if that data actually does exist.
- 32:25Then we're going to loop through all the
- 32:27entries within each table. So we're
- 32:30going to say for i, receipt in i pairs
- 32:35open and close parenthesis data. And
- 32:37we're using i pairs because we want to
- 32:39go through each one sequentially. So
- 32:41we're going to say do and then we're
- 32:42going to use another for loop because as
- 32:44you know data is one big table that
- 32:46consists of multiple tables resembling
- 32:49each transaction. So what we're going to
- 32:51do is say for key that's what we're
- 32:53going to call it comma value in pairs
- 32:56and we're going to pass in our receipt
- 32:59table do print we're going to print out
- 33:02the key and the value. So this is
- 33:06essentially how we're going to print out
- 33:07all of our data that's in the purchase
- 33:09history so that we can see it in the
- 33:11output. So this is basically how uh our
- 33:14code is going to be structured. Okay. So
- 33:15there was actually a typo here with uh
- 33:18with our receipt table. So instead of os
- 33:21time, it's actually os. I got the the
- 33:24function messed up. Okay. So now up
- 33:26until this point, we should be able to
- 33:28see this work as expected. So if we join
- 33:30into the game, we should basically see
- 33:33nothing in the output because we didn't
- 33:34buy anything yet. But if we decide to
- 33:36buy this item right here, then what
- 33:38should happen is in the output, it
- 33:40should say Brawl Battle has bought a
- 33:42game pass, but nothing is being printed
- 33:43out right now. So if we stop the game
- 33:46and then join back into the game, what
- 33:48we should see now in the output is our
- 33:51purchase history uh being displayed. So
- 33:55as you can see for our first entry, it
- 33:58says our ID of the item, then it's going
- 34:00to say the date, which is formatted like
- 34:02this with OS.ATE, uh the name of the
- 34:05item, which is diamond sword, and then
- 34:06the price of 100. And you can do this
- 34:09again by buying this and then hitting
- 34:11okay. And then we should be able to
- 34:13leave the game and then join back again
- 34:16to see this happen uh in the output.
- 34:19Now, typically uh if this was an actual
- 34:21game, you should only be able to see
- 34:23this one time. So, ideally, this would
- 34:25be uh one this would be one game pass
- 34:28and then this would be a whole another
- 34:30game pass right here because it's not
- 34:32possible for you to buy the same game
- 34:34pass uh within a game unless you delete
- 34:36that item and put it back into the game.
- 34:39Okay, that was a lot to take in, but I
- 34:42really hope that this gave you insight
- 34:44into more of what you can do with game
- 34:46passes. Is saving game pass transactions
- 34:49necessary? I would say in some instances
- 34:51they are, but you can definitely get
- 34:54away with not doing this if you don't
- 34:55want to. But this is something I wanted
- 34:58to address in this video because I know
- 35:00that some people might find this
- 35:01helpful, especially if you're following
- 35:02along with this course. Uh, and I hope
- 35:05everything we've done in this in this
- 35:07and I hope everything we've done in this
- 35:09tutorial made sense to you. If you want
- 35:11to know more about how data stores work,
- 35:13since that's a very prevalent thing we
- 35:14used in this video, then I encourage you
- 35:16to watch my data stores video that I
- 35:18will have a link for you right here. But
- 35:21with that being said, I hope you found
- 35:22this video to be helpful and I will see
- 35:24you in the next episode. Take care.
About this transcript
This page contains the full transcript of Gamepasses (One-Time Robux Pass & DataStore Receipt) - Roblox Advanced Scripting #21 (2024) by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 7,248 words across 954 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.