In case you haven't done this already, enable local variables on the compiled script! This can be done either using the importer or the Properties window. Local variables will be important for the next steps to work.
Setting up the leaderboard
Roblox provides a built-in way to create and manage scores within leaderboards. You may be familiar with the leaderboard in the top right.
It's created using a concept known as leaderstats. It can be created by placing a folder called "leaderstats" in each player, and then respective player scores as IntValues/NumberValues/StringValues in that folder. Here's a hierarchy example:
This will lead to the following:
Now if we want to change the "Potatoes" score of that player, we will need to find their leaderstats folder and set the value of the "Potatoes" IntValue.
Let's begin implementing scores by creating a "leaderstats" folder for all players that join our game:
This event (PlayerAdded) is triggered whenever a new player joins our game. Then we immediately create a folder called "leaderstats" in that player's Player instance, and in the folder we create an IntValue called "Potatoes" which is where that player's score will be.
Now we must actually increase the score each time a player collects a potato. Let's modify our SpawnPotato function like this:
player = Players.GetPlayerFromCharacter(otherPart.Parent): This will use a library called Players to get the Player instance from the player's character model.
player.leaderstats.Potatoes.Value = player.leaderstats.Potatoes.Value + 1: This will get the player's score and add 1 to it, then save it.
if isPotatoNice {: Additional things to do if the potato is nice.
msg = Instance.new("Message", workspace) creates a Message that is visible to all users, the next line sets its contents, and the line after it makes the message only last 2 seconds,
player.leaderstats.Potatoes.Value = player.leaderstats.Potatoes.Value + 4: If the potato is nice, add 4 extra points (for a total of 5).