Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problems with gun shop cloning guns?

Greetings,

I've made a gun shop, and at first sight works well, but after buying a gun the script clones the last gun and puts both in the player backpack. How can I solve it?

Scripts: Buy button script

--[[
	Script made by: Time_URSS
	Gui buttons and shopping
--]]

--//Variables\\--
local player = game.Players.LocalPlayer
local button = script.Parent
local name = button:WaitForChild("FinalName")
local price = button:WaitForChild("FinalPrice")
local nameText = button.Parent.Name
local priceText = button.Parent.Price
local replicatedStorage = game:GetService("ReplicatedStorage")
local verificationTab = button.Parent.Parent.Parent.Verification

--//Functions\\--
local function buyProcess()
	local weapon = replicatedStorage:WaitForChild("Guns"):FindFirstChild(name.Value)
	if weapon then
		if player.leaderstats.Rubbles.Value >= price.Value then
			verificationTab.Visible = true
			verificationTab.Yes.MouseButton1Click:Connect(function()
				verificationTab.Visible = false
				player.leaderstats.Rubbles.Value = player.leaderstats.Rubbles.Value - price.Value
				local clonedWeapon = weapon:Clone()
				clonedWeapon.Parent = player.Backpack
				print("Gun successfully bought, transaction ended well.")
			end)
			verificationTab.No.MouseButton1Click:Connect(function()
				verificationTab.Visible = false
				warn("Gun failed to be bought, transaction cancelled.")
			end)
		else
			warn("Player does not have enough Rubbles to buy the gun. Wait until you've enough.")
		end
	else
		error("Weapon not found, please contact Time_URSS to fix the error. ERROR#050")
	end
end

button.MouseButton1Click:Connect(buyProcess)

Both buttons script

--[[
	Script made by: Time_URSS
	Gui buttons and shopping
--]]

--//Variables\\--
local player = game.Players.LocalPlayer
local button = script.Parent
local buybutton = button.Parent.Parent.Information.Buy
local priceText = button.Parent.Parent.Information:WaitForChild("Price")
local nameText = button.Parent.Parent.Information:WaitForChild("Name")

--//Values\\--
local name = button:WaitForChild("Name")
local description = button:WaitForChild("Description")
local characteristics = button:WaitForChild("Characteristics")
local price = button:WaitForChild("Price")

--//Functions\\--
local function onClicked()
	print("Attempted to buy the "..name.Value..".")
	priceText.Text = "Price: "..price.Value
	nameText.Text = "Name: "..name.Value
	buybutton:WaitForChild("FinalPrice").Value = price.Value
	buybutton:WaitForChild("FinalName").Value = name.Value
end

button.MouseButton1Click:Connect(onClicked)

I hope I get help

1 answer

✓ Accepted answer

Took me a moment, but your issue is pretty simple. When you connect your buyProcess function to fire when you click on a button, you then create an event within the function. Hence verificationTab.Yes.MouseButton1Click:Connect(function()

The issue here is every time you press on Button, you create an event. If you buy something twice, then you have two events causing the purchase meaning you get two weapons.

  • The Fix You could remove your event nesting to not create an event several times or you could simply use :Disconnect to, well, disconnect the event. I gave you the disconnect method of doing so because learning to do so can only help you in the future.
--[[
    Script made by: Time_URSS, modified by alphawolvess ;)
    Gui buttons and shopping
--]]

--//Variables\\--
local player = game.Players.LocalPlayer
local button = script.Parent
local name = button:WaitForChild("FinalName")
local price = button:WaitForChild("FinalPrice")
local nameText = button.Parent.Name
local priceText = button.Parent.Price
local replicatedStorage = game:GetService("ReplicatedStorage")
local verificationTab = button.Parent.Parent.Parent.Verification

--//Functions\\--
local function buyProcess()
    local weapon = replicatedStorage:WaitForChild("Guns"):FindFirstChild(name.Value)
    if weapon then
        if player.leaderstats.Rubbles.Value >= price.Value then
            verificationTab.Visible = true
		local YesEvent, NoEvent = nil
          YesEvent =  verificationTab.Yes.MouseButton1Click:Connect(function()
                verificationTab.Visible = false
                player.leaderstats.Rubbles.Value = player.leaderstats.Rubbles.Value - price.Value
                local clonedWeapon = weapon:Clone()
                clonedWeapon.Parent = player.Backpack
                print("Gun successfully bought, transaction ended well.")
		YesEvent:Disconnect() -- disconnect here
		NoEvent:Disconnect()
            end)
            NoEvent = verificationTab.No.MouseButton1Click:Connect(function()
                verificationTab.Visible = false
                warn("Gun failed to be bought, transaction cancelled.")
		YesEvent:Disconnect() -- disconnect here
		NoEvent:Disconnect()
            end)
        else
            warn("Player does not have enough Rubbles to buy the gun. Wait until you've enough.")
        end
    else
        error("Weapon not found, please contact Time_URSS to fix the error. ERROR#050")
    end
end

button.MouseButton1Click:Connect(buyProcess)

3 comments

0

Keep in mind as soon as you press Yes or No, neither button will work afterwards until Button is pressed, again.

0

I broke both events because I figured if you press Yes, then No serves 0 purpose. Of course you can change this

0

Now the buttons are not working.

This discussion is preserved as it was. New answers, comments, and votes are closed.