Function Recreating the Same Frame

This script is supposed to create a frame for each skin from a table although when testing it just clones the same skin into a frame for the amount of skins there are in the table. Any help is appreciated.

Gui:

function CreateFrame(List)
	for index, Skin in pairs(List) do
		local Frame = Template:Clone()

		Frame.Name = Skin.Name
		Frame.Title.Text = Skin.Name
		Frame.Image = Skin.Image
		Frame.Cost.Text = "Ashes "..Skin.Cost

		Frame.Parent = Skins
		return Frame
	end
end

local SkinsList = require(SkinsFolder:WaitForChild("SkinsList"))

function CreateSkins(Data)
	for i, v in pairs(Data) do
		local Frame = CreateFrame(Data)
	end
end

SendData.OnClientEvent:Connect(function()
	CreateSkins(SkinsList)
end)

Table:

local Skins = {
	Ashes = {
		["Name"] = "Ashes",
		["Image"] = "rbxassetid://9217837283",
		["Cost"] = 0,
	},
	Unloved = {
		["Name"] = "Unloved",
		["Image"] = "rbxassetid://9217836267",
		["Cost"] = 500,
	},
	Bush = {
		["Name"] = "Bush",
		["Image"] = "rbxassetid://9217836845",
		["Cost"] = 900,
	},
}

return Skins

I’m my experience, looping through dictionaries is a tricky business. I found a topic that gave me a getKeys() function, that you can plug in to your table loop.

local function getKeys(t)
    local keys = {}
    for k in next, t do
        table.insert(keys, k)
    end
    table.sort(keys)
    return keys
end

If you use that function it should solve your problem :blush:

1 Like

Alright so I changed the ipairs to pairs and now it’s creating the frames for the amount of skins in the table, but it’s just the same thing over and over again.

What is the point in the CreateSkins function? It loops through the skins list only for the CreateFrame function to loop through it again.

There’s other stuff in the CreateSkins function that I’ve removed to keep this post simple, I’ve found that it’s passing the entire table in the CreateFrame function instead of just parts which is causing the issue, how could I make this edit?

local function CreateFrames()
    for i,skin in pairs(SkinList) do
        local Frame = Template:Clone()

        Frame.Name = Skin.Name
        Frame.Title.Text = Skin.Name
        Frame.Image = Skin.Image
        Frame.Cost.Text = "Ashes "..Skin.Cost

        Frame.Parent = Skins
    end
end

That should be all you need. No CreateSkins function necessary.

1 Like

With this it’s still creating the same skin over and over again.