Frames is duplicating 2 times

My script gets the pets and creates (Clone) a frame for each Pet.

The issue is that instead of cloning the frame once, it duplicates twice. I don’t know how to solve it, here is the script:


local Lighting = game:GetService("Lighting")
local ShopItems = Lighting.Pets
local player = script.Parent.Parent.Parent.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShopData = require(ReplicatedStorage.ShopData)
local PetsData = ShopData["Pets"]

local PrefabItem = ReplicatedStorage.ShopStuff:FindFirstChild("SlotLayout")

local Frame = script.Parent.PetsFrame

for _, Pet in pairs(ShopItems:GetChildren()) do --//Find pets
    local Item = PrefabItem:Clone() --//Duplicate
	if (PetsData[Pet.Name]) then
            local PetData = (PetsData[Pet.Name])
            Item.Parent = Frame
    	    Item.Name = Pet.Name
    	    Item.Info.Text = PetData["Info"]
    	    Item.Cost.Value = PetData["Cost"]
    	    Item.ImageShow.Image = "rbxassetid://" .. PetData["ImageId"]
	    wait(0.2)
	end
	print(Pet)
end

I hope you help me! Thanks :+1:

This could be because you have two of the same pet names. If that isn’t the case you could try putting a break somewhere in it and see if that fixes the problem.

What does Break does? I thought it was something unnecessary…

And also, this is a Script, not a LocalScript, this error started when i used Script instead of Local, since the server can get the information about the “petdata” in the client-sided script.

Does it create 2 frames per item in Shop items or just sometimes it clones 2?

Break will stop the loop from running. It’s for both script types.

@Jaycbee05 Always creating 2 frames


@suspectshot108 Nothing happens…

Dont know if this fixes your problem, but consider moving PrefabItem:Clone() after your If statement because otherwise when (PetsData[Pet.Name]) is not true it would unnecessarily create frames.

He never set’s a parent outside of the if statement so that shouldn’t be a problem.

No, that doesn’t works…
30chars

Do you mind providing your Shop Data module script?

Just do a check, for example,

for _, Pet in pairs(ShopItems:GetChildren()) do --//Find pets
    local Item = PrefabItem:Clone() --//Duplicate
	if (PetsData[Pet.Name]) then
if not Frame:FindFirstChild(PetsData[PetName]) then
            local PetData = (PetsData[Pet.Name])
            Item.Parent = Frame
    	    Item.Name = Pet.Name
    	    Item.Info.Text = PetData["Info"]
    	    Item.Cost.Value = PetData["Cost"]
Item.Name = PetData[Pet.Name]
    	    Item.ImageShow.Image = "rbxassetid://" .. PetData["ImageId"]
	    wait(0.2)
	end

ShopData is a module as you said:

return {

	["Pets"] = {
		
		["SlotExample"] = {
			["Name"] = "Name";
			["Cost"] = 0;
			["Currency"] = "Nubits";
			["Rarity"] = 0;
			["ImageId"] = 0;
			["Info"] = "Description"
		};
        };
}
		

By any reason, now the player doesn’t get the pet. How do i solve this?

If you need the script which sends the event, i can give it:

local player = game.Players.LocalPlayer
for i,Buttons in pairs(script.Parent.PetsFrame:GetChildren()) do
	local ImageButton = Buttons:FindFirstChild("ImageButton")
 	if ImageButton then
 		ImageButton.MouseButton1Down:connect(function()
 			local HasBought = Buttons:FindFirstChild("HasBought")
 			game.ReplicatedStorage.PetAdd:FireServer(Buttons.Name)
			print(player.Name.." sent a request to the server to buy something!")
		end)
	end
end

@ignacasas06 im going to cry if u dont respond me :d

I’m on phone so ignore any grammar error s, just do if ImageButto :IsA(“ImageButton”) then