Transparency of parts not changing

The code runs and it doesn’t give any errors, but the transparency of the parts isn’t set.
How can I fix this?

		elseif ChosenEvent == "Event9" then
			local Players = game.Players:GetPlayers()
			local CountValue = Instance.new("IntValue",game.Workspace)
			local Number = 0
			for i, Count in pairs(Players) do
				local Number = Number + 1
				CountValue.Value = Number
			end
			if CountValue.Value >= 1 then
				print("2playersingame")
				local RandomPlayer1 = Players[math.random(#Players)]
				local RandomPlayer2 = Players[math.random(#Players)]
				local P1Char = RandomPlayer1.Character
				local P2Char = RandomPlayer2.Character
				if P1Char and P2Char then
					for i, Part in pairs(P1Char:GetChildren() and P2Char:GetChildren()) do
						if Part:IsA("Part") or Part:IsA("MeshPart") then
							Part.Transparency = 1
							if Part:IsA("Accessory") then
								Part:Destroy()
							end
						end
						wait(3)
						for i, Part in pairs(P1Char:GetChildren() and P2Char:GetChildren()) do
							if Part:IsA("Part") then
								Part.Transparency = 0
							end
						end
					end
				end
			else
				print("Only 1 player in game")
				local RandomPlayer1 = Players[math.random(#Players)]
				local Char = RandomPlayer1.Character
				if Char then
					for i, Part in pairs(Char:GetChildren()) do
						if Part:IsA("Part") or Part:IsA("MeshPart") then
							Part.Transparency = 1
							if Part:IsA("Accessory") then
								Part:Destroy()
							end
						end
					end
					wait(3)
					for i, Part in pairs(Char:GetChildren()) do
						if Part:IsA("Part") or Part:IsA("MeshPart") then
							Part.Transparency = 0	
						end
					end
					CountValue:Destroy()

P1Char:GetChildren() and P2Char:GetChildren() returns an empty array which is why it doesn’t throw any errors. Instead make a custom function to combine the two arrays:

function combineArrays(array1, array2)
	for _, v in pairs(array2) do 
		table.insert(array1, v)
	end
	return array1 
end
--combineArrays(P1Char:GetChildren(), P2Char:GetChildren())

Also if you are trying to change the transparency of every part type, you can use Part:IsA("BasePart")

Why do you use table.foreach instead of a for loop?

I edited my reply so it uses a for loop instead, although the behavior remains the same.

1 Like