Player Colors script keeps breaking

Hello! I am trying to achieve a working script where it where it will assign colors to the players character (which is a starter character) based on the colors of the players avatar.

It is a ServerScript in ServerScriptService and works as follows:

local Players = game:GetService('Players')



while true do
	
	for _, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		
		if Character then
			
	
			if Character:FindFirstChild('Humanoid') then
				if Player:GetAttribute('DoneColours') == nil then
					if Player.UserId ~= nil and Player.UserId > 0 then
					if Players:GetCharacterAppearanceInfoAsync(Player.UserId).bodyColors ~= nil then
					local charinfo = Players:GetCharacterAppearanceInfoAsync(Player.UserId).bodyColors
					Player:SetAttribute('DoneColours', true)
					for n,v in pairs(charinfo) do
						if n == 'leftArmColorId' then
							Character['Left Arm'].BrickColor = BrickColor.new(v)
						elseif n == 'torsoColorId' then
							Character['Torso'].BrickColor = BrickColor.new(v)
						elseif n == 'rightArmColorId' then
							Character['Right Arm'].BrickColor = BrickColor.new(v)
						elseif n == 'headColorId' then
							Character['Head'].BrickColor = BrickColor.new(v)
						elseif n == 'leftLegColorId' then
							Character['Left Leg'].BrickColor = BrickColor.new(v)
						elseif n == 'rightLegColorId' then
							Character['Right Leg'].BrickColor = BrickColor.new(v)
						else
					end
					end
				end
				end
			end

			end


		end

	end

	task.wait(1)
end

However at random times when I join a running server, all players will have gray characters. There are no errors thrown at all, so I do not know what is being done that causes it to stop working. It will work for a while but it seems eventually it will just stop assigning player colors.


image

Is there anywhere it is possible being hung indefinitely, or isnt written correctly which is causing that issue?

Thanks!

The issue could be that you Async requests are causing an error. You should try to wrap them in a pcall like so:

local success, characterAppearanceInfo = pcall(Players.GetCharacterAppearanceInfoAsync, Players, Player.UserId)
if success and characterAppearanceInfo then
    local success, charInfo = pcall(Players.GetCharacterAppearanceInfoAsync, Players, Player.UserId)
    if success and charInfo then
        -- your other code
    end
end
1 Like

So like this:

local Players = game:GetService('Players')

while true do
	task.wait(1)
for _, Player in pairs(Players:GetPlayers()) do
	local Character = Player.Character


local success, characterAppearanceInfo = pcall(Players.GetCharacterAppearanceInfoAsync, Players, Player.UserId)
if success and characterAppearanceInfo then
	local success, charInfo = pcall(Players.GetCharacterAppearanceInfoAsync, Players, Player.UserId)
	if success and charInfo then

		if Character then


			if Character:FindFirstChild('Humanoid') then
				if Player:GetAttribute('DoneColours') == nil then
					if Player.UserId ~= nil and Player.UserId > 0 then
						if Players:GetCharacterAppearanceInfoAsync(Player.UserId).bodyColors ~= nil then
							local charinfo = Players:GetCharacterAppearanceInfoAsync(Player.UserId).bodyColors
							Player:SetAttribute('DoneColours', true)
							for n,v in pairs(charinfo) do
								if n == 'leftArmColorId' then
									Character['Left Arm'].BrickColor = BrickColor.new(v)
								elseif n == 'torsoColorId' then
									Character['Torso'].BrickColor = BrickColor.new(v)
								elseif n == 'rightArmColorId' then
									Character['Right Arm'].BrickColor = BrickColor.new(v)
								elseif n == 'headColorId' then
									Character['Head'].BrickColor = BrickColor.new(v)
								elseif n == 'leftLegColorId' then
									Character['Left Leg'].BrickColor = BrickColor.new(v)
								elseif n == 'rightLegColorId' then
									Character['Right Leg'].BrickColor = BrickColor.new(v)
								else
									end
								end
							end
						end
					end
				end
			end
		end
	end
	end
	end

Yes, you should always wrap Async calls in a pcall, they can give an error at anytime.

1 Like

Well it didnt solve the issue…