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

Why is this event triggering multiple times?

Hi, I am brand new to scripting. I am having trouble with a gui button firing multiple times. When the button is pressed the first time, the code runs once. the second press runs the code twice. the third press runs the code 3 times.

Local Script in Gui (Line 49 is the source of my problem from what i could tell):

local replicatedStore = game:WaitForChild("ReplicatedStorage")
local dropItem = replicatedStore:WaitForChild("dropItem")
local UIS = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local inventory = player.Inventory

local mainGui = script.Parent
local inventoryGui = mainGui.GUI
local interactGui = player.PlayerGui:WaitForChild("interactGui")

local fullGui = player.PlayerGui:WaitForChild("fullInventory")
local maxSlots = 4
local currentSlots = script.Parent.currentSlots

for i, itemValue in pairs(inventory:GetChildren()) do
	itemValue.Quant.Changed:Connect(function()
		local itemGui = inventoryGui.itemList:FindFirstChild(itemValue.Name)
		if itemGui then
			itemGui.ItemQuantity.Text = itemValue.Quant.Value
			if itemValue.Quant.Value <= 0 then
				itemGui.Visible = false
			elseif currentSlots.Value <= maxSlots then
				itemGui.Visible = true
			end
		end
	end)
end

for i, itemButton in pairs(inventoryGui.itemList:GetDescendants()) do
	if itemButton:IsA("ImageButton") then
		itemButton.MouseButton2Up:Connect(function()
			local debounce = false
			interactGui.Frame.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
			interactGui.Enabled = true
			local name = itemButton.Parent.ItemName.Text
			local Drop = inventory[name].Drop.Value
			local dropButton = interactGui.Frame.Drop
			interactGui.Frame.Info.Visible = true
			if Drop == true then
				interactGui.Frame.Drop.Visible = true
			else
				interactGui.Frame.Drop.Visible = false
			end
			local itemVal = inventory[name].Quant.Value
			if debounce == false then
				debounce = true
				dropButton.MouseButton1Down:Connect(function()
					if itemVal > 0 then
							local dropItem = dropItem:InvokeServer(name)
						if dropItem == true then
							if itemVal > 0 then
								itemButton.Parent.ItemQuantity.Text = itemVal
							else
								itemButton.Parent.ItemQuantity.Text = itemVal
								itemButton.Parent.Visible = false
							end
						end
					end
					wait(1)
					debounce = false
				end)
			end
		end)
	end
end

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if interactGui.Enabled == true then
			interactGui.Enabled = false
		end
	end
end)

2 comments

0

too big of script Its kinda hard to find the problem I recommend posting the main part you believe the error is on

0

Sorry for the large script. I seem to have found the source of the problem, but cant find a solution. Post is edited.

1 answer

✓ Accepted answer

Try adding debounce inside of the function. Write at the starting:

local db = true

After that, rewrite from line 49:

dropButton.MouseButton1Down:Connect(function()
	if itemVal > 0 and db == true then
		db = false
		local dropItem = dropItem:InvokeServer(name)
		if dropItem == true then
			if itemVal > 0 then
				itemButton.Parent.ItemQuantity.Text = itemVal
			else
				itemButton.Parent.ItemQuantity.Text = itemVal
				itemButton.Parent.Visible = false
			end
		end
		wait(2)
		db = true
	end
wait(1)
debounce = false
end)

I hope that will help :D

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