Hello, I was wondering if there was a way I could improve this ButtonHandler script for my Button Simulator game. The script is parented to a folder that stores all of the buttons (in their respective currencies folders) in the workspace.
The script:
--Modules
local formatNumbersModule = require(game.ReplicatedStorage.Modules.FormatNumbers)
local boosts = require(script.Boosts)
--Variables
local Buttons = script.Parent:GetDescendants()
local debounce = false
for _, button in ipairs(Buttons) do
if button:IsA("BasePart") then
local currency = button.Parent.Name
local amount = button.Amount
local cost = button.Cost
local requiredCurrency = button.RequiredCurrency
local display = button.DisplayGUI.Display
local costLabel = display.Cash
local amountLabel = display.Amount
costLabel.Text = formatNumbersModule.Comma(cost.Value) .. " " .. requiredCurrency.Value
amountLabel.Text = "+" .. formatNumbersModule.Comma(amount.Value) .. " " .. currency
button.Touched:Connect(function(hit)
if debounce then return end
debounce = true
local char = hit.Parent
local hum = char:FindFirstChild("Humanoid")
if not hum then
debounce = false
return
end
local player = game.Players:GetPlayerFromCharacter(char)
if player and player:FindFirstChild("PlayerStats") then
local stats = player.PlayerStats
local reqCurrency = stats:FindFirstChild(requiredCurrency.Value)
local targetCurrency = stats:FindFirstChild(currency)
if reqCurrency and targetCurrency and reqCurrency.Value >= cost.Value then
-- Apply boosts if this currency has any boost relationships
if boosts[currency] then
for statName, multiplier in pairs(boosts[currency]) do
local boostedStat = stats:FindFirstChild(statName)
if boostedStat then
local boostAmount = amount.Value * multiplier
boostedStat.Value = boostedStat.Value + boostAmount
end
end
end
-- Main upgrade
targetCurrency.Value = targetCurrency.Value + amount.Value
reqCurrency.Value = reqCurrency.Value - cost.Value
end
end
task.wait(1)
debounce = false
end)
end
end
Thank you ![]()