7 min read

Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.

Create a coin collection mechanic


Now that you have a 3D world, it's time to add your first script. In this section, you'll create a coin collection mechanic that lets players collect coins and temporarily disables coins after they've been collected.

Create the coins

Before you can script anything, you need to have placeholder objects in the world to use as your coins. Like the sea stack platforms you made in the previous section, the coins can be simple Part objects.

Build with Assistant

In Workspace > World, create a new folder named Coins. 
Inside the Coins folder, create a cylinder Part named Coin with BrickColor set to Gold, Material set to Metal, Size 0.6, 8, 4, and Orientation 90, 0, 0. 
Disable CanCollide and enable Anchored. 
Then duplicate the Coin part several times and scatter the duplicates around the island and on the platforms so they're reachable from the sea stack levels. 
Keep each duplicate named Coin.

Build it Yourself

  1. In the Explorer, insert a new folder inside the existing World folder and rename it Coins. Grouping all coins in a single folder makes it easier for scripts to find and manage them.

  2. Inside the Coins folder, insert a cylinder part and rename it Coin. With the part selected, configure the following properties in the Properties window:

    1. Set BrickColor to Gold.
    2. Set Material to Metal.
    3. Set Size to 0.6, 8, 4.
    4. Set Orientation to 90, 0, 0 so the coin lies flat like a disc.
    5. Disable CanCollide so players can walk through the coin to collect it.
    6. Enable Anchored so physics doesn't push the coin around.
    A close up view of a gold coin next to two gray cylinder sea stacks on the island.
  3. Duplicate the coin a few times and scatter the copies around the island and on the platforms so you have somewhere to test pickups. Make sure each duplicate is also named Coin.

    A view of multiple coins on the island and two gray cylinder sea stacks.

Create the script

Next, you'll make the coins respond to players. The Roblox engine can detect when something touches a coin, but you need to use a script to define how the coin should react.

Build with Assistant

In ServerScriptService, create a Script named CoinService with the following code:

```lua
-- Initializing services and variables
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")

local coinsFolder = Workspace.World.Coins
local coins = coinsFolder:GetChildren()

local COOLDOWN = 10

-- Defining the event handler
local function onCoinTouched(otherPart, coin)
	if coin:GetAttribute("Enabled") then
		local character = otherPart.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			-- Player touched a coin
			coin.Transparency = 1
			coin:SetAttribute("Enabled", false)
			print("Player collected coin")
			task.wait(COOLDOWN)
			coin.Transparency = 0
			coin:SetAttribute("Enabled", true)
		end
	end
end

-- Setting up event listeners
for _, coin in coins do
	coin:SetAttribute("Enabled", true)
	coin.Touched:Connect(function(otherPart)
		onCoinTouched(otherPart, coin)
	end)
end
```

Build it Yourself

  1. In the Explorer, insert a new Script inside ServerScriptService and rename it CoinService. Placing the script in ServerScriptService ensures it runs on the server and can't be accessed by clients.
  2. Replace the default code with the following script, which retrieves every coin in the Coins folder, connects a Touched handler to each one, and uses an Enabled attribute to track whether a coin can currently be collected:
-- Initializing services and variables
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")

local coinsFolder = Workspace.World.Coins
local coins = coinsFolder:GetChildren()

local COOLDOWN = 10

-- Defining the event handler
local function onCoinTouched(otherPart, coin)
	if coin:GetAttribute("Enabled") then
		local character = otherPart.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			-- Player touched a coin
			coin.Transparency = 1
			coin:SetAttribute("Enabled", false)
			print("Player collected coin")
			task.wait(COOLDOWN)
			coin.Transparency = 0
			coin:SetAttribute("Enabled", true)
		end
	end
end

-- Setting up event listeners
for _, coin in coins do
	coin:SetAttribute("Enabled", true)
	coin.Touched:Connect(function(otherPart)
		onCoinTouched(otherPart, coin)
	end)
end

Now, whenever a player touches a coin, the coin disappears for 10 seconds, and the output log prints Player collected coin.

Code explanation

The following sections describe how the script works in more detail.

Initialize services and variables

Like with a lot of the code you've probably written in other languages, you define variables that you need later at the top of the script. Our code does the following:

   local Workspace = game:GetService("Workspace")
   local Players = game:GetService("Players")

   local coinsFolder = Workspace.World.Coins
   local coins = coinsFolder:GetChildren()

   local COOLDOWN = 10
   ...

Define the event handler

The Roblox Engine physically simulates the 3D world and handles a lot of the logic to handle events related to rendering, physics, and networking. When you're interested in scripting your own logic during some of these events, you can listen for and handle them, while letting the engine do the rest. In this case, you listen for and handle events related to coins being touched. The script defines the logic for handling this event in the onCoinTouched() method, which does the following:

   local function onCoinTouched(otherPart, coin)
      if coin:GetAttribute("Enabled") then
         local character = otherPart.Parent
         local player = Players:GetPlayerFromCharacter(character)
         if player then
            -- Player touched a coin
            coin.Transparency = 1
            coin:SetAttribute("Enabled", false)
            print("Player collected coin")
            task.wait(COOLDOWN)
            coin.Transparency = 0
            coin:SetAttribute("Enabled", true)
         end
      end
   end

Connect the event handler

All simulated 3D objects inherit from BasePart and therefore have a Touched() event. The following loop connects the onTouchedEvent() handler to every coin's touch event by doing the following:

   for _, coin in coins do
      coin:SetAttribute("Enabled", true)
      coin.Touched:Connect(function(otherPart)
         onCoinTouched(otherPart, coin)
      end)
   end

Playtest the mechanic

Select Test from the dropdown and click Play. Move your character to touch a coin. If everything is working, the coin disappears, the Output window prints Player collected coin, and the coin reappears 10 seconds later.

Studio's Output window that displays confirmation that the player collected a coin.