Table.find dont work for some reason

Hello, i am currently working on random color selection for each class.
Here is my code:

local RandomColors = {Color3.fromRGB(255, 89, 89),Color3.fromRGB(248, 217, 109),
		Color3.fromRGB(9, 137, 207),Color3.fromRGB(207, 142, 50),Color3.fromRGB(99, 95, 98),
		Color3.fromRGB(91, 154, 76),Color3.fromRGB(255, 148, 148),Color3.fromRGB(255, 102, 204),
		Color3.fromRGB(0, 143, 156)
	}

local MadeColors = {}

for i, GamePiece in pairs(FolderToCheck:GetChildren()) do
		local AtColor = Color3.fromRGB(0,0,0)
		local FoundItem = table.find(MadeColors,GamePiece.Name)
		
		print(FoundItem) -- always prints nil
		if FoundItem then
			AtColor = MadeColors[table.find(MadeColors,GamePiece.Name)]
		else
			local RandomNewColor = RandomColors[math.random(1,#RandomColors)]
			MadeColors[GamePiece.Name] = RandomNewColor
			
			table.remove(RandomColors,table.find(RandomColors,RandomNewColor))
			AtColor = RandomNewColor
			
			print(MadeColors,GamePiece.Name)
		end
	end

-- There are multiple parts with the same name, thats why i add it to table.

This is message in output:

image

Please help :grin:

Made colors is an empty table im confused… how would it find anything in an emotional table?

table.find only works on the array part of the table.

Here if you already use the table like a dictionary, you can just lookup the key:

local FoundItem = MadeColors[GamePiece.Name]
1 Like

In the loop, it adds values to the table

for i, GamePiece in pairs(FolderToCheck:GetChildren()) do
		local AtColor = Color3.fromRGB(0,0,0)
		local FoundItem = table.find(MadeColors,GamePiece.Name)
		
		print(FoundItem) -- always prints nil
		if FoundItem then
			AtColor = MadeColors[table.find(MadeColors,GamePiece.Name)]
		else -- look here
			local RandomNewColor = RandomColors[math.random(1,#RandomColors)]
			MadeColors[GamePiece.Name] = RandomNewColor -- and here
			
			table.remove(RandomColors,table.find(RandomColors,RandomNewColor))
			AtColor = RandomNewColor
			
			print(MadeColors,GamePiece.Name)
		end
	end

I tried, it doesnt work.

31 characters

Note: I was wrong :sweat_smile:

It has to work:

local RandomColors = {Color3.fromRGB(255, 89, 89),Color3.fromRGB(248, 217, 109),
		Color3.fromRGB(9, 137, 207),Color3.fromRGB(207, 142, 50),Color3.fromRGB(99, 95, 98),
		Color3.fromRGB(91, 154, 76),Color3.fromRGB(255, 148, 148),Color3.fromRGB(255, 102, 204),
		Color3.fromRGB(0, 143, 156)
	}

local MadeColors = {}

for i, GamePiece in pairs(FolderToCheck:GetChildren()) do
		local AtColor = Color3.fromRGB(0,0,0)
		local FoundItem = MadeColors[GamePiece.Name]
		
		print(FoundItem) -- always prints nil
		if FoundItem then
			AtColor = FoundItem
		else
			local RandomNewColor = RandomColors[math.random(1,#RandomColors)]
			MadeColors[GamePiece.Name] = RandomNewColor
			
			table.remove(RandomColors,table.find(RandomColors,RandomNewColor))
			AtColor = RandomNewColor
			
			print(MadeColors,GamePiece.Name)
		end
	end

-- There are multiple parts with the same name, thats why i add it to table.
1 Like

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