Tables manipulation

I want to make the script as efficient and as aesthetic as possible, the issue is that I can’t think of why it doesn’t work just don’t have the energy to keep processing, the idea behind of it is instead of writing a bunch of Indian code making a formula that finds the number string inside of each one of the i.Name and search for it in the table arrays when it matches it’ll process the requested function

-Client side

TeamMenu = M.Parent.Parent.TeamMenu TeamsPack = table.unpack(C.TeamButtons)

for _,i in pairs(TeamMenu:GetDescendants()) do
	print(10)
	local TeamsUnpacked = TeamsPack[string.match(i.Name,"%d+")]
	print(13)
	if i:IsA("ImageLabel") and TeamsUnpacked == true then
		print(14)
		i.TeamName.Text = TeamsUnpacked
	end
end

-TeamsPackage side

TeamButtons = {"TeamName1","TeamName2","TeamName3","TeamName4","TeamName5","TeamName6","TeamName7","TeamName8","TeamName9"};

Doesn’t print 14 AND I know the TeamName.Text = TeamsUnpacked is messed up not a big deal I’ll fix it after getting a solution

Maybe you wanted something like this:

local TeamsUnpacked = TeamsPack[tonumber(string.gsub(i.Name,"%D", ""))]

Doesn’t seem to work but it was a good shot

I even tried to print it but it returns nil

gsub will remove the int from each name which mightn’t be that good of an idea btw

It worked for me to get the number out, but maybe add a few checks:

TeamMenu = M.Parent.Parent.TeamMenu TeamsPack = table.unpack(C.TeamButtons)

for _,i in pairs(TeamMenu:GetDescendants()) do
	print(10)
	local number = string.gsub(i.Name,"%D", "")
	number = tonumber(number)
	if number then
		local TeamsUnpacked = TeamsPack[number]
		print(13)
		if i:IsA("ImageLabel") and TeamsUnpacked == true then
			print(14)
			i.TeamName.Text = TeamsUnpacked
		end
	end
end

It doesn’t remove the number from each name but only keeps the number.

doesn’t print 14, I think sum is off with the second statement

Again not a clue at all stuck again damn

Oh wait, it does return TeamsUnpacked, but it tries to check whether it is true, and not just existent.

TeamMenu = M.Parent.Parent.TeamMenu TeamsPack = table.unpack(C.TeamButtons)

for _,i in pairs(TeamMenu:GetDescendants()) do
	print(10)
	local number = string.gsub(i.Name,"%D", "")
	number = tonumber(number)
	if number then
		local TeamsUnpacked = TeamsPack[number]
		print(13)
		if i:IsA("ImageLabel") and TeamsUnpacked then
			print(14)
			i.TeamName.Text = TeamsUnpacked
		end
	end
end

Nope same result I’ve tried it minute ago

I get it now lol we checked IsA before all of this thing that’s why, only imagelabels have numbers in them

Oh, hold on, it’s because you are unpacking the table, (I’m missing a lot and not paying close enough attention.

local TeamMenu = M.Parent.Parent.TeamMenu
local TeamsPack = C.TeamButtons

for _,i in pairs(TeamMenu:GetDescendants()) do
	print(10)
	local number = string.gsub(i.Name,"%D", "")
	number = tonumber(number)
	if number then
		local TeamsUnpacked = TeamsPack[number]
		print(TeamsUnpacked)
		print(13)
		if i:IsA("ImageLabel") and TeamsUnpacked then
			print(14)
			i.TeamName.Text = TeamsUnpacked
		end
	end
end

This could be another cause, but I’m assuming what you wanted was to change the team name under the imagelabel, and if that is what you wanted, there’s nothing wrong with it.

Right mb I’m not 100% focused a bit tired but I have very short deadline for this thing

If I won’t unpack it, it simply won’t work
image

Is “C” even defined? If so, where was it defined? Also, where exactly was TeamButtons defined?

C=require(game:GetService("ReplicatedStorage"):WaitForChild("Configuration"))
-- from the configuration script 
TeamButtons = {"TeamName1","TeamName2","TeamName3","TeamName4","TeamName5","TeamName6","TeamName7","TeamName8","TeamName9"};

I’m going to assume the Configurations module returns the TeamButtons, so, it should probably look like this:

local TeamMenu = M.Parent.Parent.TeamMenu
local TeamsPack = C

for _,i in pairs(TeamMenu:GetDescendants()) do
	print(10)
	local number = string.gsub(i.Name,"%D", "")
	number = tonumber(number)
	if number then
		local TeamsUnpacked = TeamsPack[number]
		print(TeamsUnpacked)
		print(13)
		if i:IsA("ImageLabel") and TeamsUnpacked then
			print(14)
			i.TeamName.Text = TeamsUnpacked
		end
	end
end

If it doesn’t only return the TeamButtons, then could I see what exactly it returns? It would help a lot if I could see the layout of what it returns.


nothing special