Imagebutton's has the same name

In short I have stringvalue’s inside a folder inside the player. There are imagebutton’s inside the playergui and I want to set those imagebutton’s names to the names of the stringvalue’s inside the player. The problem is that currently there are 4 imagebutton’s inside the player but 2 of those have the same name. One of them should have a 9 at the end becuase that is the missing one.

image

local Player = game.Players.LocalPlayer
local count = 0
for _,v in pairs(Player.PetInventory:GetChildren()) do
	count = count + 1
end

local counter = 0
for i, petInInventory in pairs(Player.PetInventory:GetChildren()) do
	for i, petInScrollingFrame in pairs(Player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
		if petInScrollingFrame:IsA("ImageButton") then
			if counter == count then
				break
			end
			counter = counter + 1
			print(counter)
			if petInScrollingFrame.Name == petInInventory.Name then
			else
				newTemplate.Name = petInInventory.Name
			end
		end
	end
end

Can you describe me a little bit more, I don’t understand what result are you trying to achieve? And for the first for loop with counter, you can instead, just use

#Player.PetInventory:GetChildren() -- "#" returns number of children in object

Quick hint:

count = #Player.PetInventory:GetChildren() -- sets count to the number of items returned by GetChidren

m8, first why does the title not match with the context, second the problem might be here:

if petInScrollingFrame.Name == petInInventory.Name then
else
    newTemplate.Name = petInInventory.Name
end

and a quick tip that i just noticed:

if petInScrollingFrame.Name ~= petInInventory.Name then -- ~= means not equal to
    newTemplate.Name = petInInventory.Name
end

and for the title, to make a loop stop just call break so for ex:

for i = 1, 10 do
    print(i)
    if i == 8 then
        print("Ended")
        break -- Ends the loop
    end
end
-- and continue here after the break

Unrelated to your problem but you could instead type

counter+=1

This means the same thing but less code

Oh sorry about my title. I’ll change it right away. I appreciate your help but my problem still exists.

can u show me whats inside petinventory? if so then i can see how the script fully works

Apologies for the late responses, I had to go help my family.
image2

After a lot of time I was finally able to solve this issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.