Gem Deduction happening multiple times

I have a shop UI buy script which checks and deducts the neccessary amount of gems from a players leaderstats so they can buy the item, at the moment, all of the “gemCost” values in all the buy buttons are set to 10. However, whenever I buy a different item, the cost keeps doubling up every time? It’s meant to deduct only -10 every purchase, but it doubles itself up everytime I buy a new item.

Please offer any solutions or advice I could use to attempt to fix this - not a scripter!

The script inside of the buy button:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local buyButton = script.Parent
local equipButton = buyButton.Parent.EquipButton

local sword_to_give = buyButton.Parent.Name
local sword = ReplicatedStorage.Swords:FindFirstChild(sword_to_give)

local gemCost = 10

buyButton.MouseButton1Click:Connect(function()
	local player = Players.LocalPlayer
	local playerGui = player.PlayerGui

	local confirmationUI = playerGui.Frames.SwordsFrame.ConfirmationUI
	confirmationUI.SwordName.Text = sword_to_give

	if confirmationUI then
		confirmationUI.Visible = true
		confirmationUI.GemsCost.Text = gemCost

		local yesButton = confirmationUI:FindFirstChild("YesButton")
		if yesButton then
			yesButton.MouseButton1Click:Connect(function()
				if player.leaderstats.Gems.Value >= gemCost then
					confirmationUI.Visible = false
					buyButton.Visible = false
					equipButton.Visible = true
					player.leaderstats.Gems.Value = player.leaderstats.Gems.Value - gemCost
				else
					script.Parent.Parent.Parent.Parent.Visible = false
					confirmationUI.Visible = false
					script.Parent.Parent.Parent.Parent.Parent.InsufficientFunds.Visible = true
					game.Lighting.Blur.Enabled = false
					script.Parent.Parent.Parent.Parent.Parent.FrameSidebar.Visible = false
				end
			end)
		end

		local noButton = confirmationUI:FindFirstChild("NoButton")
		if noButton then
			noButton.MouseButton1Click:Connect(function()
				confirmationUI.Visible = false
			end)
		end
	end
end)

Each time you click the first button, you are making a new connection for each time the other buttons are clicked, so it is getting triggered multiple times. Try using :Once for those connections or move them outside of the main connection.

1 Like

Thanks alot, I replaced yesButton.MouseButton1Click:Connect(function() to:

yesButton.MouseButton1Click:Once(function() and that seemed to have worked.

Just a question, so using “Connect” will connect it to the past times that the player clicked on the mouse? Never knew that, good to know.

2 Likes

:Connect creates a new connection, and returns something that represents it. This connection must be manually disconnected with :Disconnect. All :Once means is it gets disconnected after being used once.

local connection = TextButton.MouseButton1Click:Connect(function()
    connection:Disconnect()
end)
--is the same as
local connection = TextButton.MouseButton1Click:Once(function()
end)

Just a question about an issue I’m having again, because I changed it from :Connect to :Once, now whenever I click “Nevermind” on the ConfirmationUI the buy button stops working. This is the script below.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local buyButton = script.Parent
local equipButton = buyButton.Parent.EquipButton
local clickSound = script.Parent.BuyClick

local sword_to_give = buyButton.Parent.Name
local sword = ReplicatedStorage.Swords:FindFirstChild(sword_to_give)

local gemCost = 10

buyButton.MouseButton1Click:Once(function()
	local player = Players.LocalPlayer
	local playerGui = player.PlayerGui
	clickSound:Play()

	local confirmationUI = playerGui.Frames.SwordsFrame.ConfirmationUI
	confirmationUI.SwordName.Text = sword_to_give

	if confirmationUI then
		confirmationUI.Visible = true
		confirmationUI.GemsCost.Text = gemCost

		local yesButton = confirmationUI:FindFirstChild("YesButton")
		if yesButton then
			yesButton.MouseButton1Click:Once(function()
				if player.leaderstats.Gems.Value >= gemCost then
					confirmationUI.Visible = false
					buyButton.Visible = false
					equipButton.Visible = true
					player.leaderstats.Gems.Value = player.leaderstats.Gems.Value - gemCost
				else
					script.Parent.Parent.Parent.Parent.Visible = false
					confirmationUI.Visible = false
					script.Parent.Parent.Parent.Parent.Parent.InsufficientFunds.Visible = true
					game.Lighting.Blur.Enabled = false
					script.Parent.Parent.Parent.Parent.Parent.FrameSidebar.Visible = false
				end
			end)
		end

		local noButton = confirmationUI:FindFirstChild("NoButton")
		if noButton then
			noButton.MouseButton1Click:Connect(function()
				confirmationUI.Visible = false
			end)
		end
	end
end)

equipButton.MouseButton1Click:Connect(function()
	if equipButton.Text == "Equip" then
		if sword then
			local swordClone = sword:Clone()
			swordClone.Parent = Players.LocalPlayer.Backpack
			equipButton.Text = "Unequip"
		end
	elseif equipButton.Text == "Unequip" then
		local equippedSword = Players.LocalPlayer.Backpack:FindFirstChild(sword_to_give)
		if equippedSword then
			equippedSword:Destroy()
			equipButton.Text = "Equip"
		end
	end
end)

Try disconnecting both connections - :Once still returns a connection that represents it.

local connections = {}

local yesCon = yesButton.MouseButton1Click:Once(function()
    for _, connection in next, connections, nil do
        connection:Disconnect()
    end
    --etc.
end)

local noCon = noButton.MouseButton1Click:Once(function()
    for _, connection in next, connections, nil do
        connection:Disconnect()
    end
    --etc.
end)

table.insert(connections, yesCon)
table.insert(connections, noCon)

Update: nevermind, I found the issue, this should be :Connect cuz you need it multiple times.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.