How can I optimize this character race script?

	local function ColorChanger()
		local BodyColor = Character["Body Colors"]
		
		while true do
			wait(0.01)
			BodyColor.HeadColor = BrickColor.new(Data.SkinColor.Value)
			BodyColor.LeftArmColor = BrickColor.new(Data.SkinColor.Value)
			BodyColor.LeftLegColor = BrickColor.new(Data.SkinColor.Value)
			BodyColor.RightArmColor = BrickColor.new(Data.SkinColor.Value)
			BodyColor.RightLegColor = BrickColor.new(Data.SkinColor.Value)
			BodyColor.TorsoColor = BrickColor.new(Data.SkinColor.Value)
			
			if Data.Race.Value == 1 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Orc" then
						
						child:WaitForChild("OutsideEar").BrickColor = BrickColor.new(Data.SkinColor.Value)
						child.InsideEar.BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			elseif Data.Race.Value == 2 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Altmer" then
						child:WaitForChild("Ears").BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			elseif Data.Race.Value == 3 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Dunmer" then
						child:WaitForChild("Ears").BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			elseif Data.Race.Value == 4 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Bosmer" then
						child:WaitForChild("Ears").BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			elseif Data.Race.Value == 5 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Kahjiits" then
						child:WaitForChild("Ears").Cube.BrickColor = BrickColor.new(Data.SkinColor.Value)
						child:WaitForChild("Tail").Tail.BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			elseif Data.Race.Value == 6 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Argonian" then
						child:WaitForChild("Horn").BrickColor = BrickColor.new(Data.SkinColor.Value)
						child:WaitForChild("Tail").Tail.BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			elseif Data.Race.Value == 7 then
				for _, child in pairs(Character:GetChildren()) do
					if child.Name == "Vampire" then
						child:WaitForChild("Ears").BrickColor = BrickColor.new(Data.SkinColor.Value)
					end
				end
			end
		end
	end

Instead of doing a YandereDev you could instead try using tables. You can learn more about tables here

3 Likes

ninjamaster is correct. The best way forward would be to use tables. You’re repeating a lot of looping that doesn’t need to be.

I would recommend the following:

  • Assign BrickColor.new(Data.SkinColor.Value) to a variable (e.g., local skinColor = BrickColor.new(Data.SkinColor.Value), so that you’re not repeating the BrickColor constructor and so that the lines are shorter.
  • You have while true in your loop, but no break statement inside of the loop itself. If this is intentional, and the loop is meant to repeat forever, then I would recommend changing the infinite loop to something that runs when the character is added. For instance:
player.CharacterAdded:Connect(ColorChanger)

If you are worried about exploiters, you could also add a property changed event, e.g.,

player.Character:FindFirstChild("Body Colors").Changed:Connect(handle_exploit)
  • For the several loops you have, just make a table that associates the data. Something like
local race_data = {1 = {"Argonian", "Horn", "Tail"}, 2 = {"Vampire", "Ears"}}

and then you can handle it something like:

local race_features = race_data[Data.Race.Value]
for _, child in pairs(Character:GetChildren()) do
    if child.Name == race_features[1] then
        for i, p in ipairs(race_features)
            if i > 1 then
                child:WaitForChild(p).BrickColor = skinColor
            end
        end
    end
end

Feel free to implement whatever data method you choose in the table though. I gave you a simple example that is kind of messy. I think the idea of having a “Data.Race.Value” as an integer is messy, though, but it is a system that can work if you set it up right.

Coding is 90% data manipulation and that’s where all the fun lies. Have fun!

1 Like

local RaceDictionary = {

[1]=“Orc”;

[2]=“Altmer”;

[3]=“Dunmer”;

[4]=“Bosmer”;

[5]=“Kahjiits”;

[6]=“Argonian”;

[7]=“Vampire”

}

local AssociatedParts = {

[“Orc”] = {“OutsideEar”,“InsideEar”},

[“Altmer”] = {“Ears”},

[“Dunmer”] = {“Ears”},

[“Bosmer”] = {“Ears”},

[“Kahjiits”] = {“Ears”,“Tail”},

[“Argonian”] = {“Horn”,“Tail”},

[“Vampire”] = {“Ears”}

}

– // Pull race from int

local function GetRaceFromInt(int)

return ( RaceDictionary[int][2] )

end