Function gets called twice instead of once

Im making this script where when this function is called, it writes the text on the text label. That is working, the problem is: its being called twice when its supposed to be called once.

function lookItem(content) -- Here it is called twice, making the text duplicated and weird
	script.Parent.InventoryFrame.EquipFrame.LookText.Visible = true
	useButton.Visible = false
	lookButton.Visible = false
	dropButton.Visible = false
	script.Parent.InventoryFrame.EquipFrame.LookText.Text = ""
	wait(0.3)
	for i = 1, #content do
		script.Parent.InventoryFrame.EquipFrame.LookText.Text = script.Parent.InventoryFrame.EquipFrame.LookText.Text..content:sub(i, i)
		if content:sub(i, i) == "." or content:sub(i, i) == "!" or content:sub(i, i) == "?" then
			wait(0.5)
		elseif content:sub(i, i) == "," then
			wait(0.4)
		end
		wait(0.03)
	end
	wait(1.5)
	script.Parent.InventoryFrame.EquipFrame.LookText.Text = ""
	script.Parent.InventoryFrame.EquipFrame.LookText.Visible = false
	useButton.Visible = true
	lookButton.Visible = true
	dropButton.Visible = true
end

function addItem(ItemName, value)
	local debounce = false
	
		local ItemButton = script.Parent.InventoryFrame.ItemsFrame.ItemButton:Clone()
		local ItemButtonb = miscFrame.Items.ItemButton:Clone()
		ItemButton.Visible = true
		ItemButton.Name = ItemName
		ItemButton.Text = "* "..itemsModule[ItemName].name
		ItemButtonb.Visible = true
		ItemButtonb.Name = ItemName
		ItemButtonb.Text = "* "..itemsModule[ItemName].name
		repeat
			ItemButton.Parent = itemFrame
		until ItemButton.Parent == itemFrame
		repeat
			ItemButtonb.Parent = miscFrame.Items
		until ItemButtonb.Parent == miscFrame.Items

		ItemButton.MouseButton1Click:Connect(function()
		if SelectedItem ~= nil then
		itemFrame.Parent.EquipFrame.LookText.Visible = false
		useButton.Visible = true
		lookButton.Visible = true
		dropButton.Visible = true
		SelectedItem.BackgroundTransparency = 1
		end
		SelectedItem = ItemButton
		SelectedItem.BackgroundTransparency = 0
		end)

		ItemButtonb.MouseButton1Click:Connect(function()
			SelectedItem = ItemButtonb
			useItem(SelectedItem,ItemName)
			ItemButton:Destroy()
			SelectedItem = nil
		end)

		useButton.MouseButton1Click:Connect(function()
			if SelectedItem ~= nil then
				useItem(SelectedItem)
				ItemButtonb:Destroy()
				SelectedItem = nil
			end
		end)

		dropButton.MouseButton1Click:Connect(function()
			if SelectedItem ~= nil then
				SelectedItem:Destroy()
				ItemButtonb:Destroy()
				SelectedItem = nil
			end
		end)

		lookButton.MouseButton1Click:Connect(function()
		if SelectedItem ~= nil and debounce == false then
			debounce = true
			lookItem(itemsModule[SelectedItem.Name].look) --Here it is called once
			task.wait(1)
			debounce = false
		end
	end)
end

Can you show which function it is happening to? And can you show where are these functions getting are called?

they are marked with a note [- -]

the lookitem() function and the last function inside the additem function

Use prints to see what’s going on, but print variables, not just “it works”

		lookButton.MouseButton1Click:Connect(function()
        print("SelectedItem = ", SelectedItem, "    debounce = ", debounce)
		if SelectedItem ~= nil and debounce == false then

This lets you know if the mouse was clicked, and what the 2 variables are that allow the lookItem function to be called.

i pressed it once, and this happened.
image
the “Times called” is from inside the lookItem() to check it inside of the function too

I solved it!

The solution was: Adding a debounce variable OUTSIDE of the function.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.