The Advanced Roblox Coding Book by Heath Haskins

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



Copyright Disclaimer:
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.