You already know Touched, which fires when something bumps a part. But touching is messy: it fires when a player merely brushes the part, and it fires for falling bricks too. Sometimes you want a deliberate “I am choosing to interact with this” instead. That is a click, and clicks are how nearly every button in a tycoon works.
To make a part clickable, you drop a ClickDetector inside it. That little object is what tells Roblox “this part can be clicked,” and it turns the mouse cursor into a hand when a player hovers over the part. Insert one, add a Script next to it, and listen for its MouseClick event:
local part = script.Parent
local clickDetector = part:WaitForChild("ClickDetector")
clickDetector.MouseClick:Connect(function(player)
print(player.Name .. " clicked the part!")
part.Color = Color3.fromRGB(0, 200, 255)
end)
Here is the part that makes clicks so much friendlier than Touched: MouseClick hands your function the player who clicked, right there as an argument. No GetPlayerFromCharacter dance, no “was that a leg or a brick” guessing. You instantly know exactly who clicked, which is perfect for “give THIS player a reward” or “only let the owner click this.”
But clicks still need a debounce, just like touches. A player can mash the mouse, and an impatient clicker can fire your function several times fast. So gate it the same way you learned:
local part = script.Parent
local clickDetector = part:WaitForChild("ClickDetector")
local debounce = false
clickDetector.MouseClick:Connect(function(player)
if debounce then return end
debounce = true
print(player.Name .. " bought it!")
part.Color = Color3.fromRGB(0, 200, 255)
wait(1)
debounce = false
end)
Same pattern you used for Touched: check the flag, flip it to busy, do the work, wait, flip it back. The debounce is even more important for buy buttons, because you do not want one frantic click to charge a player twice.
Quick mental model: Touched is “something bumped into me,” MouseClick is “a player chose me on purpose.” Both are events, both use :Connect, but they answer different questions. For a tycoon, MouseClick is your button workhorse.
Now make it yours: build a clickable part, then give it some personality. Maybe each click cycles it through a few colors, or counts how many times this player has clicked and prints the running total. Once a deliberate click reliably runs your code, you are ready to wire up real buy buttons.