So I got the equip and unequip working but I’m able to all of the buttons, how can I fix this
local repstorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local equipped = false
for index, value in pairs(script.Parent.TrailFolderButtons:GetChildren()) do
if value:IsA("TextButton") and player.leaderstats.Coins.Value >= value.Price.Value then
value.MouseButton1Click:Connect(function()
if value.Text == "Equipped" then
value.Text = "Equip"
equipped = false
else
value.Text = "Equipped"
equipped = true
end
end)
end
end
Try this. I added comments explaining some of the differences. Instead of a true/false value showing if “the” button is equipped, there’s a reference to which button is equipped, which makes sense because there are several buttons, one of which may be equipped.
local repstorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local equippedButton: TextButton?
--Restructured stuff as a bunch of simple functions with side effects that are obvious from the names.
--init function gets called at the bottom of the script, after all functions are defined. That way the script can be read top-to-bottom, with the most general/important stuff first.
function init()
local buttons = script.Parent.TrailFolderButtons:GetChildren()
for _, button in ipairs(buttons) do
--Use early continue/return instead of deep nesting for better code readability
if not button:IsA("TextButton") then
warn(("%s is not a TextButton!"):format(button:GetFullName()))
continue
end
setupButton(button)
end
end
function setupButton(button: TextButton)
button.MouseButton1Click:Connect(function()
--Check coins every time button is clicked, NOT just at the start of the game
if player.leaderstats.Coins.Value < button.Price.Value then return end
--Unequip currently equipped button (if any), then equip newly clicked button
unequipButton()
equipButton(button)
end)
end
function unequipCurrentButton()
if not equippedButton then return end
equippedButton.Text = "Equip"
equippedButton = nil
end
function equipButton(button: TextButton)
unequipCurrentButton()
button.Text = "Equipped"
equippedButton = button
end
init()
for index, value in pairs(script.Parent.FoilFolderButtons:GetChildren()) do
if value:IsA(“TextButton”) and player.leaderstats.Coins.Value >= value.Price.Value then
value.MouseButton1Click:Connect(function()
if value.Text == “Equipped” then
value.Text = “Equip”
equipped = false
else
value.Text = “Equipped”
equipped = true
end
end)
end
end
for index, value in pairs(script.Parent.FireworkFolderButtons:GetChildren()) do
if value:IsA(“TextButton”) and player.leaderstats.Coins.Value >= value.Price.Value then
value.MouseButton1Click:Connect(function()
if value.Text == “Equipped” then
value.Text = “Equip”
equipped = false
else
value.Text = “Equipped”
equipped = true
end
end)
end
end