I’m making a script in 5 different buttons within a frame with a boolean as a child equal true when that button is clicked. And if another button is clicked, it turns the other button’s boolean equal false. This is how I made my selected button code.
Then I made another button that buys whatever the selected button gives. I made this Buy Button have a for loop that looks through the buttons’ booleans and find which one is selected. The problem with my script is that if a player selects a different button while the for loop is still going, it buys BOTH of them. Does anyone know how I can fix this? Thanks.
Here is my Buy Button script. The selected button script is self-explanatory, so I will not be showing it. If the fix requires that script, I will show it, if needed.
local buyWhat = script.Parent.Parent.WhatToBuy
local priceText = buyWhat.givePrice
local BuyButton = script.Parent
local player = game.Players.LocalPlayer
local sfxPack = game:GetService("SoundService").SFX.BuyingAndSelling
local ding = sfxPack.ChaChing
local eror = sfxPack.error
local theme = game:GetService("SoundService"):WaitForChild("BgrndMusic")
local wah = sfxPack.Hack
local hacks = sfxPack.Hacks:GetChildren()
local start = true
local processing = false
BuyButton.MouseButton1Down:Connect(function()
local og = buyWhat.Text
local cashInt = player:WaitForChild("leaderstats"):WaitForChild("Cash").Value
local selected = BuyButton.Parent.Parent:GetDescendants()
local functions = game:GetService("ReplicatedStorage").Remotes.Upgrades:GetChildren()
local rfunc = game:GetService("ReplicatedStorage").Remotes.Upgrades.RemoteFunction
if processing == false then
processing = true
for i,descendant in pairs(BuyButton.Parent.Parent:GetDescendants()) do
if descendant.Parent.Name ~= "RIghtSide" and descendant.Parent:IsA("Frame") and descendant:IsA("TextButton") then
if descendant.Bool.Value == true then
local upgrades = player:WaitForChild("Upgrades")
local price
for i, upgrade in pairs(upgrades:GetChildren()) do
if upgrade.Name == descendant.Parent.Name then
local priceChild = upgrade:GetChildren()
for i, priceC in pairs(priceChild) do
price = priceC.Value
priceText.Text = "Price: " .. price.. " Cash"
end
end
end
if rfunc:InvokeServer(descendant.Parent.Name, cashInt, descendant.Name) == true then
task.wait()
buyWhat.Text = "Purchased!"
ding:Play()
ding.Ended:Wait()
buyWhat.Text = og
processing = false
elseif rfunc:InvokeServer(descendant.Parent.Name, cashInt, descendant.Name) == "Maxed out" then
buyWhat.Text = "This is maxed out!"
processing = false
elseif rfunc:InvokeServer(descendant.Parent.Name, cashInt, descendant.Name) == "Hack" then
task.wait()
buyWhat.Text = "Error: Stop hacking"
for i, music in pairs(theme:GetChildren()) do
music:Pause()
end
local function memestuff()
while wait(0.2) do
local randomMemes = hacks[math.random(1, #hacks)]
if randomMemes.Playing ~= true then
randomMemes:Play()
end
if start == false then
break
end
end
end
wah:Play()
memestuff()
wah.Ended:Wait()
start = false
for i, music in pairs(theme:GetChildren()) do
music:Play()
end
buyWhat.Text = og
wait(5)
start = true
processing = false
elseif rfunc:InvokeServer(descendant.Parent.Name, cashInt, descendant.Name) == false then
task.wait()
buyWhat.Text = "You don't have enough money for that!"
eror:Play()
eror.Ended:Wait()
wait(2)
buyWhat.Text = og
processing = false
else
task.wait()
buyWhat.Text = "Error while purchasing."
eror:Play()
eror.Ended:Wait()
buyWhat.Text = og
processing = false
end
end
end
end
end
end)