UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

Creating a Point Giving Part

15 min

Creating a Point Giving Part

Person running towards color changing blocks to get points

This project will create a point giving part that is constantly changing colors. Depending on what color the part is when touched, it’ll either award points to players or take them away. Blue will give players a few points, green will give a lot of points, and red will take away points. To set up the different conditions for each color of the part, you’ll create an if statement using elseif and else.

Setting up the Project

To setup this project, you’ll need a leaderboard and a part that changes colors.

Create a Leaderboard

On your own, create a leaderboard script to keep track of player points. If you haven’t created a Leaderboard before, learn how to create one with this tutorial.

--In ServerScriptService, create a script named PlayerSetup with the contents below.

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Example of a IntValue
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
 
end
 
-- Run onPlayerJoin when the PlayerAdded event fires
game.Players.PlayerAdded:Connect(onPlayerJoin)

Create the Part and Script

Unlike the other looping part tutorials, this script will store each color in it's own variable to figure out how many points to award players later in the script. The points value for each color will be stored in separate variables.
0, 0, 255 Gives some points
0, 255, 0 Gives many points
255, 0, 0 Takes away points
  1. Create an anchored part named PointPart with an attached script named PointScript.
  2. In PointScript, store the parent part in a variable.
local pointPart = script.Parent
  1. Set up the RGB value for blue, which will give players the least points.
Create a new variable named blue and set it to Color3.fromRGB(). To create a color, in the parenthesis, type 0, 0, 255, the RGB value for blue.
local blue = Color3.fromRGB(0, 0, 255)
  1. Next, set up the green value (0, 255, 0) and the red value (255, 0, 0).
-- Gives some points
local blue = Color3.fromRGB(0, 0, 255)
-- Gives the most points
local green = Color3.fromRGB(0, 255, 0)
-- Takes away points
local red = Color3.fromRGB(255 ,0, 0)
  1. Code variables for small and large amounts of points and a third for removing points.
local red = Color3.fromRGB(255,0,0)

local smallPoints = 10
local largePoints = 50
local losePoints = 100

Set Up the Script

Add the Players Service

To award points, you'll start working not just with the player's character object in the workspace which holds the humanoid, but also with player information which is stored under Players. The Player object found there will be named after the player and holds information like leaderboard stats.

To get to the player's information, add Players service to your script. Services are additional pre-built functions made by Roblox engineers to save you time. The Players service has a lot of helpful function for working with player information, like points.
  • Type:
local Players = game:GetService("Players")
local smallPoints= 10
local largePoints = 50
local losePoints = 100

local Players = game:GetService("Players")
Naming Service Variables

While other variables start lowercase, any service, like Players service, starts capitalized to let you know that variable is a service.


Set Up Touch and Points Functions

The part will need two functions. One function to give players points based on the current color of the part, the other to listen if a player touches the part.

  1. On the next line, create a new function named givePoints() with a print statement for testing inside.
local Players = game:GetService("Players")
 
local function givePoints()
    print("Giving player points")
end
  1. Under that, create a second function named partTouched() with a parameter named otherPart.
local function givePoints()
    print("Giving player points")
end

local function partTouched(otherPart)

end
  1. To find the name of the player that touched the part type:
Players:GetPlayerFromCharacter(otherPart.Parent).
local function partTouched(otherPart)
     local player = Players:GetPlayerFromCharacter(otherPart.Parent)
end
  1. If a player touched the part, it’ll be stored inside the player variable. If not, then the variable will be empty. To award points, check if there is a player stored inside the player variable and if so, run givePoints().
local function partTouched(otherPart)
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		givePoints(player)
	end
end
  1. Connect the function to the pointPart's Touched event.
local function partTouched(otherPart)
	-- Gets the player if one touched the part
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		givePoints(player)
	end
end
 
pointPart.Touched:Connect(partTouched)
  1. Run the game and touch the part; you should see your test message.
Troubleshooting Tips
  • Check that the "Players" in game:GetService(“Players”) is capitalized and in quotations.
  • Make sure that the Touched connection is at the bottom of the script.

Create Looping Colors

To loop through colors, the script will use a while true do loop that changes the part’s color every few seconds.

  1. At the end of the script, create a new while true do loop.
pointPart.Touched:Connect(partTouched)
 
while true do

end
Why Put the Loop at the Bottom?

If the while true do loop is not at the bottom of the script, any code below it will never be run. Since the while loop doesn’t stop, it’ll keep running the loop instead of any code below it.


  1. On your own, code a while true do loop that changes pointPart to the color variables you’ve created. Don’t forget to use wait functions for each color. The solution is in the code box below.
-- Loops through 3 colors, waiting between each color
while true do
	pointPart.Color = blue
	wait(3)
	pointPart.Color = green
	wait(2)
	pointPart.Color = red
	wait(1)
end

  1. Playtest and check that all three colors loop without stopping.
Troubleshooting Tips
  • Check that the while loop is at the bottom of the script, below the Touched event. If the loop is not at the bottom, it’ll keep other parts of the script from running correctly.
  • Check that each color inside Color3.fromRGB() is correctly written. There must be three numbers between 0 and 255 separated by commas, like (255, 50, 0).

Rewarding Players Points

Now that the part loops through the colors, it needs to give players points. Because each color gives a different amount of points, the script will use an if statement to check what color is active when touched and give points based on that color.

Get Player Points and Part Color

Before the player can be awarded the right amount of points, you need to set up variables to capture what color the part was when the player touched it and the amount of points the player already has.

  1. To get the current color inside the part’s Color property, type:
local currentColor = pointPart.Color
local function givePoints(player)
    local currentColor = pointPart.Color
end
  1. Next, store that player’s leaderboard by typing:
local playerStats = player:FindFirstChild("leaderstats")
local function givePoints(player)
    local currentColor = pointPart.Color

    local playerStats = player:WaitForChild("leaderstats")
end
  1. Create a variable to get the player’s Points value, a child of their leaderboard.
local function givePoints(player)
    local currentColor = pointPart.Color
 
    local playerStats = player:WaitForChild("leaderstats")
    local playerPoints= playerStats:WaitForChild("Points")
end

Give Players Points

Next, you'll type an if statement to give different values of points based on the color of the part.
smallPoints Gives some points
largePoints Gives many points
losePoints Takes away points
  1. Code an if statement that checks if the current color is blue and if so, adds the number inside smallPoints to the player’s points. Remember because playerPoints is an IntValue, it’s changed by typing .Value after the variable name.
local function givePoints(player)
    local currentColor = pointPart.Color

    local playerStats = player:WaitForChild("leaderstats")
    local playerPoints= playerStats:WaitForChild("Points")

    if currentColor == blue then
        playerPoints.Value = playerPoints.Value + smallPoints		
    end

end
  1. To check for the second color, create an elseif condition for green and add the larger points variable to that player’s points.
if currentColor == blue then
    playerPoints.Value = playerPoints.Value + smallPoints	
elseif currentColor == green then
    playerPoints.Value = playerPoints.Value + largePoints		
end
  1. Use an else statement to remove points if pointsPart was neither blue nor green. Remember to subtract rather than add this time.
if currentColor == blue then
    playerPoints.Value = playerPoints.Value + smallPoints	
elseif currentColor == green then
    playerPoints.Value = playerPoints.Value + largePoints
else
    playerPoints.Value = playerPoints.Value - losePoints	
end
  1. Destroy the part after the if statement so that the script only runs once, and doesn’t keep giving out points.
if currentColor == blue then
    playerPoints.Value = playerPoints.Value + smallPoints	
elseif currentColor == green then
    playerPoints.Value = playerPoints.Value + largePoints
else
    playerPoints.Value = playerPoints.Value - losePoints	
end
 
pointPart:Destroy()
  1. Playtest and check that each color gives points as expected.
Testing Every Condition

When working with if statements with multiple conditions, it’s important to test that every elseif and else statement works. It’s possible to test one statement, think everything works, but then discover later on there’s a bug in one of the statements that could have been caught earlier.


local pointPart = script.Parent

-- Gives small points
local blue = Color3.fromRGB(0, 0, 255)
-- Gives large points
local green = Color3.fromRGB(0, 255, 0)
-- Makes players lose points
local red = Color3.fromRGB(255 ,0, 0)

-- points given to players
local smallPoints = 10
local largePoints = 50
local losePoints = 100

local Players = game:GetService("Players")

local function givePoints(player)
	local currentColor = pointPart.Color

    local playerStats = player:WaitForChild("leaderstats")
	local playerPoints= playerStats:WaitForChild("Points")

	-- Gives player points based on the color of the part
	if currentColor == blue then
	    playerPoints.Value = playerPoints.Value + smallPoints	
	elseif currentColor == green then
	    playerPoints.Value = playerPoints.Value + largePoints
	else
	    playerPoints.Value = playerPoints.Value - losePoints	
	end
	
	pointPart:Destroy()
end

local function partTouched(otherPart)
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		givePoints(player)
	end
end

pointPart.Touched:Connect(partTouched)

-- Changes the color of the part based on variables. Needs to be at bottom of script. 
while true do
	pointPart.Color = blue
	wait(3)
	pointPart.Color = green
	wait(2)
	pointPart.Color = red
	wait(1)
end

Giving Player Feedback

The PointPart works correctly, but players might not realize it's working unless they look at their leaderboard. To show it works, you can add colored particles that follow the player after PointPart was destroyed.

Adding feedback when players use a part, like sounds, shakes, or particles, makes interactions with objects more satisfying to players.

Create a Particle Effect

The particle effect will be the same color as the part when touched. This is another reason why the colors were stored in variables so they can be used again here. Each particle will be attached to the player before it’s destroyed.

  1. In givePoints() after the part was destroyed, create a new ParticleEmitter instance. Make sure the instance name is spelled exactly as shown in the quotations.
local function givePoints(player)
	local currentColor = pointPart.Color
 
	local playerStats = player:WaitForChild("leaderstats")
	local playerPoints = playerStats:WaitForChild("Gold")
 
	-- Gives player gold based on the color of the part
	if currentColor == blue then
	    playerPoints.Value = playerPoints.Value + smallPoints	
	elseif currentColor == green then
	    playerPoints.Value = playerPoints.Value + largePoints
	else
	    playerPoints.Value = playerPoints.Value - losePoints	
	end
 
	pointPart:Destroy()
	
	-- create a particle effect
	local particle = Instance.new("ParticleEmitter")
end
  1. To let players know which color they hit, you’ll set the particle’s color property to a new color sequence, a type of color used by Particle Emitters.
pointPart:Destroy()
 
local particle = Instance.new("ParticleEmitter")
particle.Color = ColorSequence.new(currentColor)
  1. So the script can find where the player is physically in-game, use the player’s character. To store that character, create a new variable and set it equal to player.Character.
local particle = Instance.new("ParticleEmitter")
particle.Color = ColorSequence.new(currentColor)
local playerCharacter = player.Character
  1. Now that you have the character, you can parent the particle to that player’s head.
local particle = Instance.new("ParticleEmitter")
particle.Color = ColorSequence.new(currentColor)
local playerCharacter = player.Character
particle.Parent = playerCharacter:WaitForChild("Head")
WaitForChild() Helps Avoid Run-time Errors

Because scripts in Roblox run at different times, it’s possible that a player’s head might not be created yet. To avoid errors, like trying to find a part that doesn’t exist, the script uses WaitForChild() to get the head part attached to the player’s character instead of using the dot operator, like player.Head.


  1. Add a wait function and destroy the particles after one second.
local particle = Instance.new("ParticleEmitter")
particle.Color = ColorSequence.new(currentColor)

local playerCharacter = player.Character
particle.Parent = playerCharacter:WaitForChild("Head")

wait(1)
particle:Destroy()
  1. Playtest the game and make sure particles briefly follow the player after touching each color.
Troubleshooting Tips
  • Make when creating a new instance that ParticleEmitter is spelled exactly as shown and inside quotations.
  • When parenting the particles, make sure to use : between playerCharacter and WaitForChild() with no spaces between.

Finished Project Sample

Project File

Download the finished project here.

Finished Script

local pointPart = script.Parent
--local storage = game:GetService("ServerStorage")

-- Gives some points
local blue = Color3.fromRGB(0, 0, 255)
-- Gives more points
local green = Color3.fromRGB(0, 255, 0)
-- Makes players lose points
local red = Color3.fromRGB(255 ,0, 0)

-- gold given to players
local smallPoints = 10
local largePoints = 50
local losePoints = 100

local Players = game:GetService("Players")

local function givePoints(player)
	local currentColor = pointPart.Color

    local playerStats = player:WaitForChild("leaderstats")
	local playerPoints = playerStats:WaitForChild("Points")

	-- Gives player gold based on the color of the part
	if currentColor == blue then
	    playerPoints.Value = playerPoints.Value + smallPoints	
	elseif currentColor == green then
	    playerPoints.Value = playerPoints.Value + largePoints
	else
	    playerPoints.Value = playerPoints.Value - losePoints	
	end

	-- Destroy the part, wait a second, and thne destroy the particle
	pointPart:Destroy()
	
	-- Creates a sparkles effect and destroys it
	local playerCharacter = player.Character
	local particle = Instance.new("ParticleEmitter")
	particle.Color = ColorSequence.new(currentColor)
	particle.Parent = playerCharacter:WaitForChild("Head")
	wait(1)
	particle:Destroy()

end


local function partTouched(otherPart)
	--local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)

	if player then
		givePoints(player)
	end
end

pointPart.Touched:Connect(partTouched)

-- Changes the color of the part based on variables. Needs to be at bottom of script. 
while true do
	pointPart.Color = blue
	wait(4)
	pointPart.Color = green
	wait(3)
	pointPart.Color = red
	wait(2)
end

These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.