You are making money. Now you need somewhere to spend it, and that is the whole loop a tycoon lives on: earn cash, buy an upgrade that earns cash faster, repeat. The buy button is the upgrade machine, and it pulls together almost everything you have learned: click events, leaderstats, if conditions, math, and debounce. This is the spicy capstone, and once it clicks, you can build an entire tycoon out of these pieces.
A buy button is a part with a ClickDetector. When the owner clicks it, the script asks one make-or-break question: do you have enough Cash? Everything hinges on that check.
local button = script.Parent
local clickDetector = button:WaitForChild("ClickDetector")
local price = 100
local debounce = false
clickDetector.MouseClick:Connect(function(player)
if debounce then return end
debounce = true
local cash = player.leaderstats.Cash
if cash.Value >= price then
-- they can afford it
cash.Value = cash.Value - price
print(player.Name .. " bought the upgrade!")
-- unlock or upgrade something here
else
print(player.Name .. " cannot afford it yet")
end
wait(0.5)
debounce = false
end)
Walk through the logic, because the ORDER is what makes it safe. First the debounce gate, so a mashed click cannot fire twice. Then the big if cash.Value >= price. That >= is “greater than or equal,” meaning you can buy if you have exactly the price OR more. If the check passes, you subtract the price (cash.Value = cash.Value - price) and only then hand over the upgrade. If it fails, you fall to the else and politely tell them no. Critically, you never subtract money outside that if, because charging a player who could not afford it is the kind of bug that makes a game feel broken.
So what is “the upgrade”? Anything you want, and this is where it gets fun. Two easy starters:
- Reveal a hidden dropper. Keep a second dropper in the world with
Transparency = 1andCanCollide = false(basically switched off), and on purchase flip it back on:
local newDropper = workspace:WaitForChild("Dropper2")
newDropper.Transparency = 0
-- and switch its dropper Script on, or set a value it checks
- Speed up an existing dropper. If your dropper waits on a value, just lower it on purchase so cash falls faster.
After a purchase you usually want the button to either disappear or bump its price up so the same upgrade cannot be bought twice. Setting button.Transparency = 1 and clickDetector.MaxActivationDistance = 0 is a quick way to “use up” a one-time button.
That is the full economy loop in your hands: cash flows in from the collector, the buy button trades it for faster cash, and the player chases the next upgrade. Stack a few of these and you have a real, progressing tycoon.
Now make it yours: get one buy button working (afford check, subtract, unlock, debounce), then chain it. Make the first upgrade reveal a second dropper, and a second button (priced higher) speed everything up. Try giving each button its own price and watch a little progression curve appear. This is your tycoon now, so design the upgrade path you would want to play.