Keeping track of score

Your script should look like this so far:

function SpawnPotato(position) {
	isPotatoNice = false
	if math.random(1,50) == 1 {
		isPotatoNice = true
	}
	newPotato = Instance.new("Part", workspace)
	newPotato.Shape = "Ball"
	newPotato.Size = Vector3.new(1,1,1)
	newPotato.Position = position
	newMesh = Instance.new("SpecialMesh", newPotato)
	newMesh.Scale = Vector3.new(1.5, 1.5, 1.5)
	newMesh.MeshId = "rbxassetid://477543051"
	newMesh.TextureId = "rbxassetid://477543054"
	Debris.AddItem(newPotato, 60)
	if isPotatoNice {
		newMesh.VertexColor = Vector3.new(2,2,2)
		newPotato.Size = Vector3.new(4,4,4)
		newMesh.Scale = Vector3.new(6,6,6)
		Instance.new("Sparkles", newPotato)
	}
	
	event newPotato.Touched(otherPart) {
		if otherPart.Parent:FindFirstChild("Humanoid") {
			sound = Instance.new("Sound", newPotato)
			sound.SoundId = "rbxassetid://3125624765"
			sound.PlayOnRemove = true
			newPotato:Destroy()
		}
	}
}

while true {
	wait(1)
	position = Vector3.new(math.random(-100,100), 100, math.random(-100,100))
	SpawnPotato(position)
}

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:

event PlayerAdded(player) {
	leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	points = Instance.new("IntValue", leaderstats)
	points.Name = "Potatoes"
}

function SpawnPotato(position) {
...

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:

...

function SpawnPotato(position) {
	...
	
	event newPotato.Touched(otherPart) {
		if otherPart.Parent:FindFirstChild("Humanoid") {
			player = Players.GetPlayerFromCharacter(otherPart.Parent)
			player.leaderstats.Potatoes.Value = player.leaderstats.Potatoes.Value + 1
			sound = Instance.new("Sound", newPotato)
			sound.SoundId = "rbxassetid://3125624765"
			sound.PlayOnRemove = true
			if isPotatoNice {
				msg = Instance.new("Message", workspace)
				msg.Text = player.Name .. " just collected a Nice Potato!"
				Debris.AddItem(msg, 2)
				player.leaderstats.Potatoes.Value = player.leaderstats.Potatoes.Value + 4
			}
			newPotato:Destroy()
		}
	}
}

...

Here's what we added:

  • 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).

Your full code should look like this now:

event PlayerAdded(player) {
	leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	points = Instance.new("IntValue", leaderstats)
	points.Name = "Potatoes"
}

function SpawnPotato(position) {
	isPotatoNice = false
	if math.random(1,50) == 1 {
		isPotatoNice = true
	}
	newPotato = Instance.new("Part", workspace)
	newPotato.Shape = "Ball"
	newPotato.Size = Vector3.new(1,1,1)
	newPotato.Position = position
	newMesh = Instance.new("SpecialMesh", newPotato)
	newMesh.Scale = Vector3.new(1.5, 1.5, 1.5)
	newMesh.MeshId = "rbxassetid://477543051"
	newMesh.TextureId = "rbxassetid://477543054"
	Debris.AddItem(newPotato, 60)
	if isPotatoNice {
		newMesh.VertexColor = Vector3.new(2,2,2)
		newPotato.Size = Vector3.new(4,4,4)
		newMesh.Scale = Vector3.new(6,6,6)
		Instance.new("Sparkles", newPotato)
	}
	
	event newPotato.Touched(otherPart) {
		if otherPart.Parent:FindFirstChild("Humanoid") {
			player = Players.GetPlayerFromCharacter(otherPart.Parent)
			player.leaderstats.Potatoes.Value = player.leaderstats.Potatoes.Value + 1
			sound = Instance.new("Sound", newPotato)
			sound.SoundId = "rbxassetid://3125624765"
			sound.PlayOnRemove = true
			if isPotatoNice {
				msg = Instance.new("Message", workspace)
				msg.Text = player.Name .. " just collected a Nice Potato!"
				Debris.AddItem(msg, 2)
				player.leaderstats.Potatoes.Value = player.leaderstats.Potatoes.Value + 4
			}
			newPotato:Destroy()
		}
	}
}

while true {
	wait(1)
	position = Vector3.new(math.random(-100,100), 100, math.random(-100,100))
	SpawnPotato(position)
}

That's all for Potato Rush. If you understood the guide, hit that "Compile" button!

Now publish the game, invite a friend over, and have fun collecting 100% Pure Powerlang potatoes!

Last updated