BLOXBUILDS
Discord
Back
TYCOON

Buy Button (Unlock)

Paste as a Script inside a button part. Set a number attribute named Price and a string attribute named Unlock. The purchase spends Cash and makes the named part visible.

-- Paste as a Script inside a button part.
-- Attributes: Price (number), Unlock (name of the part to reveal).

local Players = game:GetService("Players")

local button = script.Parent
if not button or not button:IsA("BasePart") then
	return
end

local price = button:GetAttribute("Price")
local unlockName = button:GetAttribute("Unlock")
if typeof(price) ~= "number" or typeof(unlockName) ~= "string" then
	return
end

local unlocked = false

local function findTarget(): Instance?
	for _, inst in ipairs(workspace:GetDescendants()) do
		if inst.Name == unlockName and (inst:IsA("BasePart") or inst:IsA("Model")) then
			return inst
		end
	end
	return nil
end

local function hideTarget()
	local target = findTarget()
	if target and target:IsA("BasePart") then
		target.Transparency = 1
		target.CanCollide = false
	elseif target and target:IsA("Model") then
		for _, child in ipairs(target:GetDescendants()) do
			if child:IsA("BasePart") then
				child.Transparency = 1
				child.CanCollide = false
			end
		end
	end
end

local function showTarget()
	local target = findTarget()
	if target and target:IsA("BasePart") then
		target.Transparency = 0
		target.CanCollide = true
	elseif target and target:IsA("Model") then
		for _, child in ipairs(target:GetDescendants()) do
			if child:IsA("BasePart") then
				child.Transparency = 0
				child.CanCollide = true
			end
		end
	end
end

hideTarget()

button.Touched:Connect(function(hit)
	if unlocked then
		return
	end
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	local leaderstats = player and player:FindFirstChild("leaderstats")
	local cash = leaderstats and leaderstats:FindFirstChild("Cash")
	if not cash or not cash:IsA("IntValue") or cash.Value < price then
		return
	end

	cash.Value -= price
	unlocked = true
	showTarget()
	button.Color = Color3.fromRGB(80, 180, 120)
end)