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
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.
buttonEquip = gui.Container.SelectedFrame.Info.EquipButton.MouseButton1Click:Connect(function()
if not buttonEquip == nil then
buttonEquip:Disconnect()
end
interactItem(tower.Name)
updateItems()
end)
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.