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

Why does my GUI keep giving me more and more tools when a ClickDetector is clicked?

I have put together a script that when a part is clicked, it will show a GUI showing which flavour of crisp you want. This is all put together with a RemoteFunction, and the RemoteFunction sends the flavour to a script to clone it into the players backpack. (Client to server). However, when I get the crisps the first time it works completely fine, there is no errors and it gives me crisp. However, when I click the button again and click the flavour I want, it gives me a varying amount of crisps, which keeps increasing every time I click the button. I've been messing around with this for the last 20 minutes, unable to be successful. Here are my scripts:

Local script

local crispEvent = game.ReplicatedStorage.remoteFunctions.getCrisp
local frame = script.Parent.Frame
local tool = game.ReplicatedStorage.Crisps
local added = script.Parent.added

local function getFlavour()
	for i,v in pairs(frame:GetChildren()) do
		frame:TweenPosition(UDim2.new(0.125, 0,0.141, 0))
		v.MouseButton1Click:Connect(function()
			local flavour = v
			for i,v in pairs(tool:GetChildren()) do
				if v.Name == flavour.Name then
					crispEvent:InvokeServer(flavour.Name)
					added.Text = "Added one packet of "..v.Name
					added:TweenPosition(UDim2.new(0.03, 0,0.032, 0))
					wait(1)
					added:TweenPosition(UDim2.new(0.03, 0,-0.2, 0))
					frame:TweenPosition(UDim2.new(0.113, 0,1.05, 0))
				end
			end
		end)
	end
 end

game.Workspace.Bowl.ClickDetector.MouseClick:Connect(getFlavour)

Script

local crispFlavourEvent = game.ReplicatedStorage.remoteFunctions.getCrisp
local tool = game.ReplicatedStorage.Crisps

local function addFlavour(player, flavour)
	for i,v in pairs(tool:GetChildren()) do
		if flavour == v.Name then
			v:Clone().Parent = player.Backpack
		end
	end
end

crispFlavourEvent.OnServerInvoke = addFlavour

I hope this can come with a solution as I have been working on it for ages, I'll accept your answer if it works! :)

1 answer

✓ Accepted answer

You're creating another click event for each button every time you open the gui. Do this instead:

local crispEvent = game.ReplicatedStorage.remoteFunctions.getCrisp
local frame = script.Parent.Frame
local tool = game.ReplicatedStorage.Crisps
local added = script.Parent.added

local function getFlavour()
	frame:TweenPosition(UDim2.new(0.125, 0,0.141, 0))
end
game.Workspace.Bowl.ClickDetector.MouseClick:Connect(getFlavour)


for i,v in pairs(frame:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		local flavour = v
		local flavorTool = tool:FindFirstChild(v.Name)
			if(flavorTool)then
			crispEvent:InvokeServer(flavour.Name)
			added.Text = "Added one packet of "..v.Name
			added:TweenPosition(UDim2.new(0.03, 0,0.032, 0))
			wait(1)
			added:TweenPosition(UDim2.new(0.03, 0,-0.2, 0))
			frame:TweenPosition(UDim2.new(0.113, 0,1.05, 0))
		end
	end)
end

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