Nested Loop issues - help

This all worked great when I tested with a rig so I went ahead and changed for enemy players character.

Basically I decided to save the player.characters parts to a table. I got the name and the color. Idk if I need the name but it’s there anyway. I’ve probably made a mistake somewhere which is why I’m struggling now. When the player enters the game the color of the players characters parts are saved into a module that I can call at any time. The reason being that I wanted feedback to the player when they get hit by an attack (their player turns red for a bit) and then is supposed to turn back to the original color. Idk what that color will be so I saved it to the module as the player enters the game.

I’m having trouble iterating over the player’s character and the module to turn the colors back to original.

image
Table seems to work on player join

-- MODULE --
local PlayerInfoModule = {}

function PlayerInfoModule.GetInitialColor(character)

	local initialColors = {}

	for _, part in pairs(character:GetChildren()) do
		if part:IsA("MeshPart")then -- ignore HumanoidRootPart
			local partInfo = {
				name = part.Name,--keep if name needed
				color = part.Color
			}

			table.insert(initialColors, partInfo)
		end
	end

	return initialColors
end

function PlayerInfoModule.OnPlayerJoin(player)
	print(player.Name .. " has joined.")

	player.CharacterAdded:Connect(function(character)
		PlayerInfoModule.CharacterAdded(character)
	end)
end

function PlayerInfoModule.OnPlayerDied(player)
	print(player.Name .. " has died.")
end

function PlayerInfoModule.OnPlayerLeave(player)
	print(player.Name .. " has left.")
end

function PlayerInfoModule.CharacterAdded(character)
	if character then
		local initialColors = PlayerInfoModule.GetInitialColor(character)

		if initialColors then
			print(initialColors)-- just check the table is being formed and loaded correctly
		else
			print("Table has no data")
		end
	else
		print("Character not loaded properly.")
	end
end

return PlayerInfoModule

--SCRIPT--

---- Spell Casting Script ----

-- SERVICES --
local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local DebrisService = game:GetService("Debris")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")

-- MODULES --
local playerInfoModule = require(ServerScriptService:WaitForChild("PlayerInfoModule"))

-- EVENTS ETC --
local castEvent = ReplicatedStorage:WaitForChild("CastEvent")
local canAttack = {}

castEvent.OnServerEvent:Connect(function(player, eventType, arg1)
	local character = player.Character
	
	if eventType == "Water Bolt" then
		-- add in casting animation here (player winds up like a baseball throw)
		local animTrack = character.Humanoid:LoadAnimation(ServerStorage.Animations.SpellAnim.CastingAnim)
		local animTime = animTrack.Length
		animTrack:Play()
		
		task.wait(animTime)
		
		local startPosition = character.RightHand.Position
		local endPosition = arg1
		local duration = (endPosition - startPosition).Magnitude / 60
		-- how to cast during an animation keyframe?
		
		local waterBoltClone = ServerStorage.Particles.Water.WaterBolt:Clone()
		waterBoltClone:PivotTo(character.RightHand.CFrame)
		waterBoltClone.Parent = workspace
		
		local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
		
		--create tween for water bolt casting
		local castingTween = TweenService:Create(waterBoltClone, tweenInfo, {Position = endPosition})
		castingTween:Play()
		
		--play sound
		
		DebrisService:AddItem(waterBoltClone, duration)
		
		--Damage the enemy
		waterBoltClone.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and not table.find(canAttack, hit.Parent.Name)then
				local enemyCharacter = hit.Parent
				table.insert(canAttack, enemyCharacter.Name)
				
				enemyCharacter.Humanoid.Health -= 10
				
				--Enemy Damage Effect
				for i, v in pairs(enemyCharacter:GetChildren())do
					if v:IsA("MeshPart")then
						local tween1 = TweenService:Create(v, TweenInfo.new(0.4, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out),{Color = Color3.new(255,0,0), Transparency = 0.5})
						tween1:Play()
					end
				end
				
				task.wait(0.1)
				
				table.remove(canAttack, table.find(canAttack, enemyCharacter.Name))

				--module to get the characters parts color
				local enemyStats = playerInfoModule.GetInitialColor(enemyCharacter)--find the correct players table
				
				--checking players parts against module table parts
				for i, v in pairs(enemyCharacter:GetChildren())do
					for _, part in pairs(enemyStats.initialColors)do--this is not a table, error table = nil
						if v.Name == part.name and v:IsA("MeshPart")then
							v.Color = part.color
							break
						end
					end
					local originalColor = v.Color
					local tween2 = TweenService:Create(v, TweenInfo.new(0.4, Enum.EasingStyle.Exponential, Enum.EasingDirection.In),{Color = originalColor, Transparency = 0})
					
					tween2:Play()
				end
			end
		end)
	end
end)

The Error,

Basically what you’re doing is returning a table which is named initialColors locally, in each index is another table listing the names and colors of the enemy parts. I believe that you messed up and tried to access the table’s initialColors property ({...}.initialColors), which doesn’t exist since the table just contains other tables. All you need to do is loop through enemyStats instead of initialColors inside of it.

UPDATED.

				--module to get the characters parts color
				local initialColors = playerInfoModule.GetInitialColor(enemyCharacter)--find the correct players table
				
				--checking players parts against module table parts
				for i, v in pairs(enemyCharacter:GetChildren())do
					for _, part in pairs(initialColors)do
						if v.Name == part.name and v:IsA("MeshPart")then
							local originalColor = part.color
							local tween2 = TweenService:Create(v, TweenInfo.new(0.4, Enum.EasingStyle.Exponential, Enum.EasingDirection.In),{Color = Color3.new(originalColor), Transparency = 0})

							tween2:Play()
							break
						end
					end

				end

The enemy player turns red on cue, then turns black fsr. No errors I can see.