Step 3 – PlayerSetup Script

PlayerSetup Script

In this lesson we will copy the script as shown in the image above into your PlayerSetupScript. Remember to keep lines indented correctly and all letters must be correctly capitalized. 

  1. Delete Hello World and write a descriptive comment.
  2. After the comment, create a custom function named onPlayerJoin with a parameter named player.
  3. In onPlayerJoin, create a variable named leaderstats, and have it create a new Folder Instance. Naming the folder leaderstats lets Roblox Studio know to create a leaderboard.
  4. Name the new Folder instance leaderstats, and parent it to the player.
  5. After the end of the function, connect OnPlayerJoin to the PlayerAdded event. Whenever a player joins the game, the onPlayerJoin function will run.

Now that a leaderboard is created, it needs to show the player these numbers:

  • Gold – How much money the player has.
  • Items – How many items the player has collected from the world.
  • Spaces – The most items a player can hold at one time.

Each of these numbers will be an IntValue, a placeholder object for a number.

  1. In OnPlayerJoin, under leaderstats.Parent = player, type local gold = Instance.new(“IntValue”).This creates a new IntValue and stores it in the variable gold.
  2. Type gold.Name = “Gold”. This gives the IntValue a name so you can use it in other scripts. The name will also be show up on the leaderboard.
  3. On a new line, type gold.Value = 0. This makes it so players don’t start with any gold
  4. Type gold.Parent = leaderstats. This parents the IntValue for gold to leaderstats. If the IntValue is not parented to leaderstats, players won’t see it.
  5. Add a blank line after gold.Parent = leaderstats. This makes it easier to see where the code for different IntValues starts and stops.

Using .Value

While variables are normally changed using = as in myNumber = 10, an IntValue is changed using its Value property, like myIntValue.Value = 10.

  1. After the blank line, create a stat for Items by setting up a new IntValue the same way you did for gold.
  2. Create a new stat for the player’s bag spaces. Set spaces.Value to 2 so players start the game only being able to hold two items at once, encouraging them buy a new bag as soon as they can.


Playtesting

Now playlets your game and notice there is a leaderboard in your game.