The Advanced Roblox Coding Book by Heath Haskins
Author:Heath Haskins
Language: eng
Format: epub
Publisher: Adams Media
The values in the table are made up—this is just an example of a uniform table inside a table. No matter who we load the data for, we should have at least money and experience. That’s what the SetAsync is doing. Just remember, we don’t actually run savePlayerData directly. We will be doing it through other functions. Let’s go through those.
local function setupPlayerData(player)
local success, data = getPlayerData(player)
if not success then
-- Could not access DataStore, set session data
for player to false.
sessionData[player] = false
else
if not data then
-- DataStores are working, but no data for
this player
sessionData[player] = {Money = 0, Experience
= 0}
savePlayerData(player)
else
-- DataStores are working and we got data for
this player
sessionData[player] = data
end
end
end
Here, we have the setupPlayerData function. It takes the argument of player, which is passed into our function from the PlayerAdded event that we will program later. In the first line, we see the first instance of our getPlayerData function that we created earlier.
Remember, the getPlayerData returns two things to us: A true/false to indicate if it ran correctly, and the information we requested. We store the returned information into two variables: success and data. We check to see if success was true. If it was not, we set our data to false to prevent it being used. If the getPlayerData worked we check to see if the data is good. If there is no data for the player, we create the sessionData entry with the player as the key, then set its value to a table of indexed values. This is the creation of the table within a table.
Now, because that’s the first time that we have seen this player, we go ahead and immediately run the savePlayerData function to ensure that player’s file will be in the DataStore next time the user joins. If the data was found, then we simply add it to our sessionData table.
THINK AHEAD
ONE PROBLEM WITH THIS METHOD OF SAVING DATA IS THAT IF YOU END UP ADDING ANYTHING MORE TO YOUR GAME, SAY ANOTHER LEVEL OR SAVEZONE, THEN IT WON’T BE THERE FOR ANYONE WHO ALREADY HAS A SAVE. IMAGINE THIS. YOUR GAME IS OUT FOR A WHILE, THEN LATER ON, YOU DECIDE TO ADD ANOTHER ASPECT TO YOUR PLAYERDATA. WHEN SOMEONE WHO HAS ALREADY PLAYED THE GAME COMES IN, THE else FUNCTION HERE DOES NOT LOOK TO SEE IF THAT NEW VALUE THERE. I WOULD RECOMMEND SOME KIND OF CHECKING FOR YOUR DATA TO MAKE SURE THAT PLAYER’S FILE HAS THE SAME NUMBER OF KEYS AND VALUES THAT EVERYONE ELSE GETS WHEN THEY ARE FIRST STARTING.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Blood, Sweat, and Pixels by Jason Schreier(3473)
Dawn of the New Everything by Jaron Lanier(2676)
Godot 4 Game Development Cookbook by Jeff Johnson(2460)
The Art of Doom by Bethesda(2033)
Significant Zero by Walt Williams(1876)
Creative Character Design by Bryan Tillman(1829)
World of Warcraft Chronicle Volume 3 by Blizzard Entertainment(1651)
The Ultimate Roblox Book by David Jagneaux(1611)
Art Of Atari by Tim Lapetino(1557)
Pillars of Eternity Guidebook by Obsidian Entertainment(1530)
Dawn of the New Everything: Encounters with Reality and Virtual Reality by Jaron Lanier(1530)
1628927445Game by Unknown(1468)
Unreal Engine 4 Virtual Reality Projects by Kevin Mack(1461)
Unreal Engine Virtual Reality Quick Start Guide by Jessica Plowman(1438)
Mission Python by Sean McManus(1426)
The Ultimate Player's Guide to Minecraft by Stephen O'Brien(1414)
Learning D by 2015(1409)
Unity 2018 By Example by Alan Thorn(1388)
Road Games by Road Games(1363)
