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

Making Shop Script: I get 6 tools instead of 1?

I'm making a Shop script that has GUI's and everything. It works, but it gives me 6 of the items instead of 1. Can anyone help?

aduritecost = 500
americancost = 1000
--banhammer = admin
canadiancost = 1000
cupcakecost = 100
diecost = 100
dogecost = 250
galaxycost = 800
goldencost = 5000
koolcost = 400
mediccost = 500
missingtexturecost = 1800
moneycost = 10000
nightvisioncost = 500
noobcost = 50
robloxcost = 500
rainbowcost = 2500
unicorncost = 10000
tediorecost = 7500
ukcost = 1000
yeetcost = 15000

local player = game.Players.LocalPlayer

local shopgui = player.PlayerGui:WaitForChild("ShopGUI")
local shopmaster = shopgui:WaitForChild("ShopMaster")
local pages = shopmaster:WaitForChild("Pages")
local knives = pages:WaitForChild("Knives")
local description = knives:WaitForChild("Description")
local purchasesound = knives:WaitForChild("PurchaseNoise")

local replicatedstorage = game:GetService("ReplicatedStorage")
local knifestorage = replicatedstorage:WaitForChild("KnifeStorage")

local knifename = description:WaitForChild("ItemName")
local buy = description:WaitForChild("BuyButton")



--when preferred item is shown
--checks if player chose item
--clicks on the buy
--buys it
--done

function checkandbuy()
	buy.MouseButton1Click:connect(function()
		
		if knifename.Text == ("Adurite Knife") then
			local leaderstats = player:FindFirstChild("leaderstats")
			local coins = leaderstats:FindFirstChild("Coins")
			if coins.Value >= aduritecost then
				coins.Value = coins.Value - aduritecost
				knifestorage.AduriteKnife:clone().Parent = player.Backpack
				wait(2)
			end
			
	elseif knifename.Text == ("American Knife") then
			local leaderstats = player:FindFirstChild("leaderstats")
			local coins = leaderstats:FindFirstChild("Coins")
			if coins.Value >= americancost then
				coins.Value = coins.Value - americancost
				knifestorage.AmericanKnife:clone().Parent = player.Backpack
				wait(2)
			end
			
	elseif knifename.Text == ("Ban Hammer") then
				if player.Name == "Zingoro" or "Jxemes" or "LinkMe" or "GoodCreeper917" then
				wait(1)
				knifestorage.BanHammer:clone().Parent = player.Backpack
				wait(2)
				end

		else
		end
	end)
end







knifename.Changed:connect(checkandbuy)
buy.Changed:connect(checkandbuy)

1 answer

✓ Accepted answer

You're probably clicking the button too many times in one click. That's why we use debounces. Though since your script is super long, I won't be writing the scripts like I usually do. Though, I'm going to show you how to use debounces.

##What Is A Debounce?##

A debounce system is a set of code that keeps a function from firing too many times. It comes from the idea of mechanical switch bounce, where a switch bounces when pushed, creating multiple fired events.

Since I don't see a problem in your script, you should try debounces.

Here's a great example from Roblox Wiki:

local buttonPressed = false
--Store whether the button is pressed in a local variable
 
Workspace.Button.Touched:Connect(function(hit)
    if not buttonPressed then
    -- Is it not pressed?
 
        buttonPressed = true
        -- Mark it as pressed, so that other handlers don't execute
 
        print("Button pressed")
        wait(1)
        print("Hi :D")
        -- Do Stuff
 
        buttonPressed = false
        -- Mark it as not pressed, so other handlers can execute again
    end
end)

Though this is for touched event, so I will be showing you a MouseButton1Down example.

local b = script.Parent -- button
local Clicked == false
 -- the debounce variable
b.MouseButton1Down:Connect(function()
	if Clicked == false then -- only work if clicked is false
		Clicked = true -- set's to true so that the player can't click again
		-- You code
		Clicked = false -- set's click to false so you can click again
	end
end)

Good luck and have fun developing! If you find any problems, make sure to comment on this question.

8 comments

0

Got 2 things. What is the difference between MouseButton1Down and MouseButton1Click? And it doesn't work. It still gives me 6.

0

For first thing, not much of a difference. Second, add prints to find out where it gives more

0

Still doesn't work, I added prints and there is no source of telling where the cloning is! I think I should restart.

0

Here, i think you should change that function(), remove it and maybe make it buy.MouseButton1Click:connect(function() in that add all those other weapon things..

0

NVM, I fixed it using Black's way. Thanks tho neil.

0

Hurray :D

0

Hurray :D

0

Hurray :D

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