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
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.
: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)