Shop buttons don't work

Trying to get the items in a shop to print it’s name themselves when clicked and found in a module script using these script:

local RS = game:GetService("ReplicatedStorage")
local S = RS.Remotes:WaitForChild("Shop")
local Backpacks = require(RS:WaitForChild("BPacks"))
local SPanel = SGui.ShopPanel
local BPShop = SPanel.BPackShop
local BPItems = BPShop.Items

  for _,v in pairs (BPItems:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			local GetPack = v[Backpacks.Packs]
			print(GetPack)
		end)
	end
end

MODULE:

local module = {
	Packs = {
		Default = {
			Cost = 0,
			Size = 5
		},
		Small = {
			Cost = 0,
			Size = 10
		},
	}
}

return module

Problem is GetPack is either printing nil or saying that it expected a string. Not sure how to get a string out of a table from the module, any ideas?

Is the name of the TextButton supposed to align with the name of the “Pack” inside of the Module, such as “Default” or “Small”? I’m trying to get a better understanding of what you’re using the TextButton for inside of the GetPack variable.

Yes, that’s what I would like to achieve

In that case, the variable would need to be modified in order to reference a table inside of the Packs table using the name of the TextButton:

for _, item in ipairs(BPItems:GetChildren()) do
	if item:IsA("TextButton") then
		item.MouseButton1Click:Connect(function()

            local packName = item.Name
			local GetPack = Backpacks.Packs[packName] -- This will look through the "Packs" table for something with the same name as the button that clicked it

            if GetPack then
			    print(GetPack)
            end
		end)
	end
end
1 Like