In Part 2, we created buttons that let players unlock items. But right now, they’re free — which makes the Tycoon too easy! In this part, we’ll add cash (currency) so players need to spend money to buy items.
Step 1: Set Up Leaderstats (Player Data)
We’ll store each player’s money in a leaderboard (the top-right corner of the screen).
- In ServerScriptService, insert a new Script.
- Rename it:
Data
. - Add this code:
-- Data Script (handles player stats like money)
game.Players.PlayerAdded:Connect(function(player)
-- Create Leaderstats folder (must be lowercase "leaderstats")
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
-- Create Cash value
local cash = Instance.new("IntValue")
cash.Name = "Cash" -- appears on leaderboard
cash.Value = 0 -- start with 0
cash.Parent = folder
end)
Now when you press Play, you’ll see a leaderboard in the top-right corner with “Cash = 0.”
Step 2: Add Prices to Buttons
Each button should have a price so players must pay to unlock items.
- Select your Button in the Explorer.
- In Properties → Attributes, click ➕.
- Add:
- Name =
Price
- Type = Number
- Example Value =
50
- Name =
This means unlocking this button costs 50 Cash.
Step 3: Update Plot Handler Script
Go back to your PlotHandler script (where you detect button touches).
Find the section where the player touches the button and add:
-- Get price from button
local price = button:GetAttribute("Price")
-- If no price set, item is free
if price and player.leaderstats.Cash.Value < price then
warn("You cannot afford this item!")
return
end
-- Deduct price from Cash (only if price exists)
if price then
player.leaderstats.Cash.Value -= price
end
Step 4: Test the System
- Press Play.
- Step on the button with 0 Cash.
- You should see a warning: “You cannot afford this item!”
- The item won’t appear.
- Give yourself Cash (in Server View only):
- Switch to
Server
mode in Studio. - In Explorer → Players → [YourName] → leaderstats → Cash → set value to
500
.
- Switch to
- Switch back to
Client
mode. - Step on the button again.
- The item appears
- Your Cash decreases (e.g., from 500 → 450).
Recap
- Created a
leaderstats
system to track Cash. - Added a Price attribute to buttons.
- Checked if players have enough money before unlocking items.
- Deducted money when they buy items.
Next Step
Right now, players start with 0 Cash. In the next part, we’ll create a dropper system so players can earn money automatically and keep upgrading their Tycoon.