More Efficient Way to Apply Bodycolor to Player?

What I want to do is change a player’s BodyColor using an RGB color value. Currently, this function does work, but is there a better way to optimize this code instead of creating a table indexing the player’s limbs?

function Edit.ApplyBodyColor(Player, BodyColor) -- Uses Color3.fromRGB()
	if (Player.Character ~= nil) then
        --  Better way to do this then making a table?
		local Limbs = {
			"HeadColor3",
			"LeftArmColor3",
			"LeftLegColor3",
			"RightArmColor3",
			"RightLegColor3",
			"TorsoColor3"
		}
		for a, limb in ipairs(Limbs) do
			Player.Character["Body Colors"][limb] = BodyColor
		end
	end
end
2 Likes

I’m having a hard time understanding your question. You don’t necessarily need to construct a table to do this (though there is almost no difference in doing so):

function Edit.ApplyBodyColor(player, bodyColor)
    -- get needed Instances.
    local character = player.Character
    if not character then return end
    local bodyColors = character["Body Colors"]
    -- apply colours.
    bodyColors.HeadColor3 = bodyColor
    bodyColors.LeftArmColor3 = bodyColor
    bodyColors.LeftLegColor3 = bodyColor
    bodyColors.RightArmColor3 = bodyColor
    bodyColors.RightLegColor3 = bodyColor
    bodyColors.TorsoColor3 = bodyColor
end

This code, however, does look quite messy. I don’t believe there’s any way to change multiple body colours at once, so something along the lines of this may be your only option, unfortunately.

13 Likes

Ah thanks, I was trying to stay away from changing the colors line by line, but either way it’s going to make me I guess :smile:

This is pretty optimised, though if anything, the only other option to easily change the properties is to use an API for this. But for this case, it would be highly unnecessary.

1 Like

@OnlyRoids This is the only other idea I had that can compromise for the table.

function Edit.ApplyBodyColor(Player, BodyColor)
	if Player.Character ~= nil then
		for _, bodyparts in pairs(Player.Character:GetChildren()) do
			
		if bodyparts:IsA("MeshPart") or bodyparts:IsA("Part") and bodyparts.Name ~= "HumanoidRootPart" then
				
		bodyparts.Color = BodyColor 
				
			end
		end
	end
end

Related, might want to check this out:

1 Like

@Dandystan It would still amount to using the same function you posed as the supposed solution. You can change multiple body colours at one time using a function, with the properties indexed in an array.


@OP A category exists if you want to improve already-working code. I suggest you post something like this to #development-support:requests-for-code-feedback or move the thread by clicking the pencil beside the title.

Don’t try micro-optimising that code. That’s already fine as it is and it doesn’t necessarily need to change. Without manually picking and choosing properties of a limb, creating a table is the best way you could have to change a player’s Body Colours. In the first place, why are you against using a table? There’s nothing wrong with it, at all.

2 Likes
local bodyColors = character:WaitForChild("Body Colors")
for _, bodyPartEnum in ipairs(Enum.BodyPart:GetEnumItems()) do
	bodyColors[bodyPartEnum.Name.."Color"] = BrickColor.Random()
end

This isn’t necessarily more efficient/performant but it’s definitely cleaner. You can change Color for Color3 and assign a Color3 userdata value instead.

5 Likes