Building a Simple Currency System for Your Game

Learn how to create a working currency system in Roblox using leaderstats, data storage, and scripts to reward players with coins they can earn and spend.

Every great Roblox game needs a way for players to earn and spend currency. Whether it’s coins, gems, or points, a currency system adds motivation and progression to your game. In this tutorial, we’ll build a simple but functional currency system that you can expand on later!

Understanding Leaderstats

Before we start coding, let’s talk about leaderstats. This is a special folder in Roblox that displays player statistics on the leaderboard (that list you see in the top-right corner of games). When you create a folder named “leaderstats” inside a player’s character data, any IntValue or StringValue you put inside it will automatically show up on the leaderboard.

This is perfect for currency because players can always see how many coins they have!

Setting Up the Basic Currency System

We’ll create a script that gives every player a “Coins” stat when they join the game. Create a Script (not a LocalScript) in ServerScriptService and add this code:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    -- Create the leaderstats folder
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    -- Create the Coins value
    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0  -- Starting amount
    coins.Parent = leaderstats
end)

Let’s break this down:

  • We use PlayerAdded to detect when someone joins the game
  • We create a Folder named “leaderstats” and parent it to the player
  • We create an IntValue (a number) called “Coins” and set it to 0
  • We parent the Coins to leaderstats so it shows on the leaderboard

Test your game now! You should see “Coins: 0” appear on the leaderboard when you play.

Giving Players Currency

Now let’s make a way for players to actually earn coins. We’ll create a part that gives coins when touched. Insert a Part into the Workspace, then add a Script inside that part:

local part = script.Parent
local COIN_REWARD = 10  -- How many coins to give
local COOLDOWN = 5  -- Seconds between collections

local playerCooldowns = {}  -- Track who recently collected

part.Touched:Connect(function(hit)
    -- Check if a player touched it
    local character = hit.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    
    if player then
        -- Check if player is on cooldown
        if playerCooldowns[player.UserId] then
            return  -- Too soon, exit early
        end
        
        -- Find their coins stat
        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            local coins = leaderstats:FindFirstChild("Coins")
            if coins then
                coins.Value = coins.Value + COIN_REWARD
                
                -- Start cooldown
                playerCooldowns[player.UserId] = true
                task.wait(COOLDOWN)
                playerCooldowns[player.UserId] = nil
            end
        end
    end
end)

What’s happening here?

  • We detect when the part is touched
  • We check if a player touched it (not just any part)
  • We use a cooldown system so players can’t spam-collect coins
  • We safely check that leaderstats and Coins exist before adding to them
  • We increase the coin value and start the cooldown timer

The cooldown prevents players from standing on the part and getting infinite coins instantly. The playerCooldowns table remembers who recently collected coins using their UserId.

Making Currency Useful: A Simple Shop

Currency isn’t meaningful unless players can spend it! Here’s a simple example of a purchase system. Create another part (maybe a different color) and add this script:

local part = script.Parent
local COST = 25
local SPEED_BOOST = 10  -- How much to increase WalkSpeed

part.Touched:Connect(function(hit)
    local character = hit.Parent
    local player = game.Players:GetPlayerFromCharacter(character)
    
    if player then
        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats then
            local coins = leaderstats:FindFirstChild("Coins")
            
            if coins and coins.Value >= COST then
                -- Player can afford it!
                coins.Value = coins.Value - COST
                
                -- Give them a speed boost
                local humanoid = character:FindFirstChild("Humanoid")
                if humanoid then
                    humanoid.WalkSpeed = humanoid.WalkSpeed + SPEED_BOOST
                end
            end
        end
    end
end)

This script checks if the player has enough coins, deducts the cost, and gives them a permanent speed boost. You can modify this to sell tools, pets, cosmetics, or anything else!

Important Notes

Data doesn’t save yet! Right now, when players leave and rejoin, their coins reset to 0. To make coins save, you’ll need to learn about DataStores, which is a more advanced topic. For now, this system works perfectly while players are in your game.

Also, always use server-side scripts for currency systems. Never use LocalScripts for adding currency, or exploiters could give themselves unlimited coins!

Recap and Next Steps

Congratulations! You’ve built a working currency system with:

  • A leaderboard display using leaderstats
  • A way to earn currency by touching parts
  • A cooldown system to prevent spam
  • A simple shop to spend currency

To take this further, learn about DataStores to save player data, or explore RemoteEvents to create GUI-based shops. You could also add different coin types (like gems or tickets) for premium purchases. Keep building and experimenting—you’re doing great!

Author

Leave a Reply

Your email address will not be published. Required fields are marked *