Module running function more than once

Hi, I got a problem with something. So I am trying to make it that when you buy something it refreshs the shop. In the shop script there is a i,v in pairs loop to delete all the previous frames from the shop but instead of deleting it, it multiples. I don’t know the problem to it. Can you please help me?

Module:

function module:LoadGUIStore(player)
	
	local backpack = player.Backpack
	local character = player.Character
	local frames = script.Parent.Parent.Frames
	local shopFrame = frames.Shop
	local templateFolder = shopFrame.Templates
	local toolSelection = shopFrame.ToolSelection
	local ToolTemp = templateFolder["ToolTemplate"]
	local extraFolder = ToolTemp["Extra"]
	local ownedText = extraFolder["OwnedText"]
	
	for i, v in pairs(toolSelection:GetChildren())do
		
		if v:IsA("ImageButton") or v:IsA("TextButton")then
			v:Destroy()
		    print("Removed "..v.Name)
	end
	
	for toolName, tool in pairs(itemInfo.Tools)do
	
	if Tools:FindFirstChild(toolName) then
		
		local Temp = ToolTemp:Clone()
		local ToolNameTextLabel = Temp["ToolName"]
		local ToolCostTextLabel = Temp["ToolCost"]
		
		Temp.Name = toolName
		ToolNameTextLabel.Text = toolName
		ToolCostTextLabel.Text = tool["Cost"][1].." "..tool["Cost"][2]
		Temp.Parent = toolSelection
		Temp.LayoutOrder = tool["Cost"][1]
		
		if backpack:FindFirstChild(toolName) and character:FindFirstChild(toolName)then
		ownedText.Visible = true
		else
		ownedText.Visible = false
		end
		
		Temp.Visible = true
	end
	end
	end
	
	local deb = {}
	
	for i, button in pairs(toolSelection:GetChildren())do
	
	if button:IsA("ImageButton") or button:IsA("TextButton")then
		
		button.Activated:Connect(function()
			
			if deb[player.Name] == false or deb[player.Name] == nil then
				
				deb[player.Name] = true
				events["BuyTool"]:FireServer(button.Name)
				wait(1)
				deb[player.Name] = false
				
				end
			end)
end
end
end

Where I am using the function:

events["refreshShop"].OnClientEvent:Connect(function()

clientModule:LoadGUIStore(player)

end)

Few questions :

  • Are you sure that you have defined the right container?
  • Are you sure that the function is being called?
  • Are you receiving any prints in the console?

Please provide a screenshot of the UI explorer tree if you are not sure of the answer to the first question.

1 Like

First Question Answer:
image

Second Question Answer:
Yes, the problem is, is that when I call the function a second time it multiples even more after the first time it was called.

The first time it is called:

The second time it is called:

Third Question Answer:
Yes, it prints the removed button’s name in the output.

Are you sure that the error is with this part of the script, and not the table that it is creating the buttons with? At first glance there should be no errors with what is given here.

TL;DR: Are you certain that itemInfo.Tools does not have duplicate entries?

This is some of itemInfo.Tools:

module.Tools = {
	["Gravity Coil"] = {
	["Cost"] = {60, "Points"};
	};
	
	["AK47"] = {
	["Cost"] = {0, "Points"};
	};
	
	["Bloxiade"] = {
	["Cost"] = {100, "Points"};
	};
	
	["Cookie"] = {
	["Cost"] = {130, "Points"};
	};	
	
	["Doge"] = {
	["Cost"] = {500, "Points"};
	};		
	
	["Flute"] = {
	["Cost"] = {200, "Points"};
	};
	
	["Watermelon"] = {
	["Cost"] = {60, "Points"};
	};	
	
	["Pizza"] = {
	["Cost"] = {105, "Points"};
	};	
				
}

do you want me to give you every function using the module function?

Hi,

“instead of deleting it, it multiples”
I think it deletes them but then it fills up all tools with the first tool’s data.
Probably you should test it by disabling the code from the 2nd for loop (“for toolName…”)

Some questions:

  1. “if Tools:FindFirstChild(toolName) then” - where do you define Tools? Is it itemInfo.Tools?

  2. “for toolName, tool in pairs(itemInfo.Tools) do” - is it correct? toolName will be a numeric counter in this way, as far as I know. Probably I missed this behavior of “for” loop, sorry in this case.

Cheers,

2 Likes
  1. Tools = game.ReplicatedStorage[“Tools”], it is folder with all the tools in there. So that is checking if the selected tool is in the folder or not, if yes then continue on with the script.

  2. I am pretty sure it gets the name of the table selected from the i,v in pairs loop. So what I am trying to say is that the for loop gets you the key and the value, which in this case, the key is the tool name and the value is the inside of the table which is why I do:

ToolCostTextLabel.Text = tool[“Cost”][1]…" "…tool[“Cost”][2]

I am getting the inside of the table selected from the for loop.

I hope this helped and please correct me if I am wrong.