How do i stop this function stacking?

How do i stop this function stacking? i dont really know what this is called but basically im calling a function then inside that function updating info but the info is stacking and idk how to fix ill give a example

the thing where im sending data
image

the script that is getting the data

and then the output of hte stacking
image

everytime i call the function it stacks the data and does all of them at once any fixes? sorry if its not clear

1 Like

Please use ``` instead of images for your next Post.

The reason it’s printing every tower name is because you’re never disconnecting the MouseButton1Click Event that’s within updateInfo.
What you could do is have a variable outside the function called MB1Click and set the MouseButton1Click to that then in the update info check if that variable is not nil if it isn’t then :Disconnect() it.

1 Like

ohh alright thanks for the suggestion of the like code thingys i forgot about that but im noit that good at scripting but uhh ill see if it works

hey uh quick question how would i set the mousebutton1click to that?

local VariableName
VariableName = TextButton.MouseButton1Click:Connect(function() print("Hello World" end)

alright thanks ill test it and see if it works

hey uh where do i put those because i have it in thatfirst image it sets

buttonEquip = gui.Container.SelectedFrame.Info.EquipButton.MouseButton1Click

but idk where to put the checking part

Above the part where you assign the variable.

i did that but its still stacking :frowning: this is my code

local buttonEquip
buttonEquip = gui.Container.SelectedFrame.Info.EquipButton.MouseButton1Click:Connect(function()
		if not buttonEquip == nil then
			buttonEquip:Disconnect()
		end
		interactItem(tower.Name)
		updateItems()
		
	end)

newButton.MouseButton1Click:Connect(function()
updateInfo(tower.Name, tower)
end)

Can I see all the code?
This isn’t complete, and the location of where local buttonEquip is very important

ok so i just have the local button equip in the top of the script with the other variables was i supposed to put it when i call the function?

Are you disconnecting it when you setup a new button event?

if buttonEquip then
	buttonEquip:Disconnect()
end

yeah if you want i can send you both functions

Please send it.
characters characters characters

alright here it is

function updateInfo(itemName, tower)
	gui.Container.SelectedFrame.Info.Visible = true

	gui.Container.SelectedFrame.Info.TowerName.Text = itemName

	gui.Container.SelectedFrame.Info.Description.Text = towers[itemName].Description

	for i,v in pairs(gui.Container.SelectedFrame.Info.ViewportCharacter.WorldModel:GetChildren()) do
		if v:IsA("Model") or v:IsA("BasePart") then
			v:Destroy()
		end
		--gui.Container.SelectedFrame.Info.ViewportCharacter.WorldModel:GetChildren().Destroy()
	end
	
	local towerModel = ReplicatedStorage.Towers[itemName]

	local TowerTemplate2 = towerModel:Clone()
	TowerTemplate2.Parent = gui.Container.SelectedFrame.Info.ViewportCharacter.WorldModel
	local camera = Instance.new("Camera")
	camera.Parent =gui.Container.SelectedFrame.Info.ViewportCharacter
	gui.Container.SelectedFrame.Info.ViewportCharacter.CurrentCamera = camera

	camera.CFrame = CFrame.new(TowerTemplate2.PrimaryPart.Position + TowerTemplate2.PrimaryPart.CFrame.LookVector * 1, TowerTemplate2.PrimaryPart.Position)

	camera.CFrame = TowerTemplate2.PrimaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(-200),0) * CFrame.new(0,0,3)

	playAnimation(TowerTemplate2, "Idle")
	
	buttonEquip = gui.Container.SelectedFrame.Info.EquipButton.MouseButton1Click:Connect(function()
		if buttonEquip then
			buttonEquip:Disconnect()
		end
		interactItem(tower.Name)
		updateItems()
	end)
	
	local status = getItemStatus(itemName)
	if status == "For Sale" then
		gui.Container.SelectedFrame.Info.EquipButton.Text = "$" .. tower.Price
	elseif status == "Equipped" then
		gui.Container.SelectedFrame.Info.EquipButton.Text = "UnEquip"
	else
		gui.Container.SelectedFrame.Info.EquipButton.Text = "Equip"
	end
end
function updateItems()
	cash.Text = "πŸ’Έ" .. playerData.Cash
	limit.Text = #playerData.SelectedTowers .. "/5"
	
	for i, tower in pairs(towers) do
		-- Find any old buttons
		local oldButton = itemsFrame:FindFirstChild(tower.Name)
		if oldButton then
			oldButton:Destroy()
		end
		
		-- Creating new button
		local towerconfig = game.ReplicatedStorage.Towers:FindFirstChild(tower.Name).Config
		local newButton = itemsFrame.TemplateButton:Clone()
		local tower2 = game.ReplicatedStorage.Towers:FindFirstChild(tower.Name)
		newButton.Name = tower.Name
		newButton.Title.Text = tower.Name
		--newButton.Image = tower.ImageAsset
		newButton.Visible = true
	--	newButton.LayoutOrder = towerconfig.Price.Value
		newButton.Parent = itemsFrame
		
		if newButton.Name == "DChris" then
			if table.find(playerData.OwnedTowers, "DChris") then
				newButton.Visible = true
			else
				newButton.Visible = false
			end
		end
		
		if newButton.Name == "Survivor" then
			if table.find(playerData.OwnedTowers, "Survivor") then
				newButton.Visible = true
			else
				newButton.Visible = false
			end
		end
		
		local TowerTemplate = tower2:Clone()
		TowerTemplate.Parent = newButton.ViewportCharacter.WorldModel
		local camera = Instance.new("Camera")
		camera.Parent =newButton.ViewportCharacter
		newButton.ViewportCharacter.CurrentCamera = camera

		camera.CFrame = CFrame.new(TowerTemplate.PrimaryPart.Position + TowerTemplate.PrimaryPart.CFrame.LookVector * 1, TowerTemplate.PrimaryPart.Position)

		camera.CFrame = TowerTemplate.PrimaryPart.CFrame * CFrame.Angles(math.rad(0),math.rad(-200),0) * CFrame.new(0,0,3)

		playAnimation(TowerTemplate, "Idle")
		
		local status = getItemStatus(tower.Name)
		if status == "For Sale" then
			gui.Container.SelectedFrame.Info.EquipButton.Text = "$" .. tower.Price
			newButton.Status.Text = "$" .. tower.Price
		elseif status == "Equipped" then
			gui.Container.SelectedFrame.Info.EquipButton.Text = "UnEquip"
			newButton.Status.Text = "βœ… Equipped"
			newButton.Status.TextColor3 = Color3.new(255,255,255)
			newButton.Status.BackgroundColor3 = Color3.new(0,0,0)
		else
			gui.Container.SelectedFrame.Info.EquipButton.Text = "Equip"
			newButton.Status.Text = ""
		end
		
		newButton.MouseButton1Click:Connect(function()
			--interactItem(tower.Name)
			updateInfo(tower.Name, tower)
		end)
	end
end

You need to put this code inside of the updateInfo function above where buttonEquip is assigned.
Otherwise the button events never get disconnected which results in multiple.MouseButton1Click events being connected.

1 Like

OMG IT WORKS THANK YOU SO MUCH :smiley: :smiley: :smiley:

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