How To Make A Roblox Game – Saving Data (4)

How To

How To Make A Roblox Game – Saving Data (4) Video

popular roblox games

How To Make A Roblox Game – Saving Data (4) Video Transcript

roblox save and load script

welcome back to part four in the how to create a game on roblox series now in this episode we’re going to be doing data stores so that we can save the players currency so that in future they’ll be able to leave the game rejoin and then we can script allowing them to buy different items in the game but today we’re going to be doing data store and before we kick off the video I just want to start off by letting you guys know that YouTube have now enabled channel memberships on the channel underneath this video you can see that there is a join button and a lot of people have been asking how to get the source code so now the code is restricted to channel members only because what people were doing was they were coming to the video just taking the code and leaving so they weren’t actually learning anything which is unfair because I’ve taken the time to create these videos and people are just watching them for five seconds and so if you want to get the source code you can become a channel member and you also get other perks such as a badge next to your name in chats you get exclusive live streams merch discounts and much more so just a quick announcement about channel memberships let’s get straight into part 4 so what we need to do is we need to go into the stats script over here and this is where we have our books where we actually create the currency and at the top here we’re going to define the data store service so I’m going to say local data stores equals game current get service data store service spell like this in Pascal case and then we can say it : get data store and then in these brackets in speech marks we’re going to say the name of our data store and that’s going to be books data store you can call it whatever you like I’m calling mine books data store as it tells us what we’re going to be saving so now that we’ve done this we’re just going to drop beneath the character added event over here and I’m just going to add a quick comment saying data stores and what we’re going to do is we’re going to create a variable called player data so local player underscore data that is the variable which is going to store the results of what’s in their data store key right so we’re gonna we’re going to take their key if they’ve got one now a key is what’s used to look up their data and get their that specific data for one player and we’re going to bind whatever comes back from that key whatever result whatever value that data store gives us we’re gonna put in this variable but we’re gonna define it first in case there is no data there so what we want to do is you want to do a P call here and this will just so in case the data store fails then the whole script won’t error and we can carry on nothing’s going to actually break so we can what we can say is when say player underscore data equals data stores and then we can say : gets async and then in brackets we’re gonna add their key now the key can be whatever we want but we have to include their user ID so it’s unique and so that everyone has a different key so we can say player dot a user ID dot dot because we’re concatenating it together with a string and that string is going to say – and then the name of your data your currency so in our case it’s books but you could call it whatever you want so it could be coins it could be points whatever you want but I’m going to keep mine as books so we’re just going to put that in speech marks here and a person’s key might look something like this so that we have the user ID and then we have books on the end and each player has a different user ID so each key is going to be different and of course something could go wrong with get a sync data stores might be down or there might be some kind of error which is stopping it from getting the data so we wrap it in this Pico function in case something goes wrong the whole script won’t error and break down so now that we’ve done that what we need to do is we need to check to actually see if they have some data or whether they’re a new player to the game because if they’re a new player then we’re going to want to give them a default amount of cash to start off with or books in this case so we’re going to say if player underscore data is not equal to nil so that means if there is something there there’s a value there is some data already saved then we’re going to load that data in so we can say player has saved data load it’s in so to load it in we can just say books dot value equals player underscore data because if there is some some data stored in that player data variable then we can just set the books value to player underscore data let’s just make books lowercase here because we defined it with a lower case when we over here so now that we have it loaded it in what if the player is brand new to the game and doesn’t have any safe data well we can say else so if we go to this else here we know that it’s a new player so we can give them a default cash value as either no data saved on this player so we can say books got value equals and then we can either give them an amount of cash right here or we can set it to a variable which I’m going to do I’m going to create a variable called default cash and then at the top of the script what we can do is we can create another variable called default cash also local default cash equals and then you can set an amount of cash which you want a brand new player to receive of course if you don’t want any cash to be given to a new player you can just set it to zero but I’m going to set mine to ten so now that we have got the the default cash for a new player what we need to do is we need to well that’s all of the loading finished we now need to work on saving the data and that can be a little bit tricky because we need to make sure that every player in the game gets their data saved because when the last player leaves the game the server will soon shut down afterwards so we don’t want to corrupt any data or make a player lose their data because the server shut down before it was able to save so what we’re going to do is we’re going to create another event we’re just going to drop outside of the player added one here and we can say game adopt players dot player removing heard on connect and then in brackets we can say function and then in another set of brackets we can say player just your normal event down here and player is going to be the player that is leaving the game because player moving fires whenever a player decides to leave the game so just before they leave this event will fire so let’s drop a line and roblox have added in the end here so you need to make sure you’ve got the end with a closing bracket here and inside of here what we can do is we can do another peak or function so P cool and then in brackets function just like in events and make sure to add an end here with another closing bracket and so inside of here any code if it errors it’s not going to break the entire script so in this case we want to say data stores curl on set async and then in brackets we can say player dot user ID and then we want to concatenate again because we’re getting there key and we want to save a specific value to this players unique key so that we can get it when they next during the game so we’re gonna say player dot user ID and we want the key to be exactly the same as we said up here so we’re going to then in speech marks say – books so that we have the exact same key it looks like I made the D for datastores capital so make sure that’s lowercase so it’s in line with how we defined it at the top here and we also want to then we want to give the data which we’re going to save so to this key we’re going to save these players books so we’re going to say player dot leader stats dot books dot value okay now we saved the books in their leader stats which is why we’re saying player don’t lead to sack stock books and then we’re saying dot value because that’s the number of books they’ve got and we’re assigning that to that key so we can save it so we should have that should have saved the data let’s just add a little print saying saved and hopefully everything went went well and what we want to do now is so that will save the data if a player leaves the game but what if this player is the last person in the game and the server shuts down before the data safe we don’t want that to happen so we’re going to use a binder pool event and we’re gonna fire that when we know that the data has successfully saved and that will tell the server that it’s all good to shut down so to do that we’re going to create a binder ball event okay um we’re going to create it in our script so we can say local bindable event I’m gonna I’m gonna define it outside of the player added function just before our play removing event and I’m going to say equals instance dot new bindable event now I’m not parenting this to any works we don’t need to do that we’re only using this bindable event to make sure that the game is okay to shutdown and what we’re going to do is we’re also going to create another variable at the top of our script called local players left now you’ll see why we’re doing this in a minute but essentially what we’re doing is we’re keeping track of player still in the game and if there is one place if there’s no players left in the game then we’re going to wait for that bindable event to be fired and when it is fired we will know that the game is to be shut down safely without any data being wiped so we need to increment the players left variable when a player joins the game we need to add 1 to that value because a player’s just join the game and if somebody leaves the game then when their data has been saved we can subtract 1 from that value and then when it gets to 0 we will know that all players have had their data successfully saved so because set async yields and the script won’t continue until the data has been saved what we can do is we can go over here underneath the print saved as we know that the data will have been saved we are able to decrease the players left a variable by 1 so let’s go ahead and do that now players left equals players left minus 1 and then we can fire our bindable event and in a minute we’ll do our bind to close event which is going to be triggered when the game is ready to shut down so we can say bindable event : fire and that will fire off our events and we’re now going to go ahead and pick up in the bind to close event so let’s just drop a couple of lines and we’re gonna say game code on find it to close and then in brackets we’re gonna say function another pair of brackets and drop a line let’s zoom in here so we have got our bind to close this is going to be triggered whenever the game gets a signal to shut down so this will be triggered upon shutdown so just before the game is shutting down anything inside of here will will be executed and it will prolong the game from shutting down so we want to make sure that all players data is saved so if there’s more than 0 players left we need to make sure that their data has been saved so we can say while players left is greater than 0 do now this is a while loop it will run forever we can save bindable event okay dot event : wait okay and this is going to wait for that bindable event to be fired the server’s not going to be closed until we get that confirmation that the last players data has successfully been saved so that’s all we need to do for the bind to close so now all players data should be saved ready forged for the server to be shut down and finally at the top of the script just underneath player added we can say players left equals players left plus one we want to add to that value whenever a new player joins the game so that at the end we know that there is going to be another player whose data needs to be saved so now I’m going to publish the game and see if the data store works so I’m just going to copy the link and load into the game so you can see we are a new player to the game and we’ve been given our 10 books because no data has been found so let’s go ahead into the console and give us some cash so we can test to see if it saves so we’re gonna go into the server click on command line and we’re gonna say game dot players I’m gonna write my name in I’m gonna say leader stats doctor books dot value equals 900 we’re gonna leave the game and then rejoin and see if it saves so here we are we’re joining the server and we should load in with 900 books there we go we’ve got our books and they loaded in perfectly fine so there we go we have done data stores in our sword fight game and that was part 4 we’re now able to save a player’s currency so the in future maybe they can spend it on in-game items we could maybe add some purchasable items as well through developer products and game passes so if you want to see those in future make sure you subscribe to my youtube channel make sure you’ve liked the video and don’t forget if you want to become a channel member so you can unlock the source code and lots of other perks you can click on the join button right next to the subscribe button down below thanks for watching and have a great day

roblox saving player data

How To Make A Roblox Game – Saving Data (4) Related Keywords

roblox saving player data

how to make roblox games, roblox saving player data, roblox save and load script, roblox game play, popular roblox games, how to save your game on roblox 2017, roblox studio 2017, how to download roblox studio