How can I fix my NPC color changing script?

I’m trying to create an NPC that looks as if it’s glitching. To achieve this effect, I wrote a script that should randomly change the NPC’s body colors. However, the script doesn’t seem to be working.

The output is showing no errors.

To try and solve this, I also created another, less complex, script that only changes the torso color, and only once. This script worked, the more complex one, didn’t.


Actual Script
while true do

wait(0.25)

local CD = math.random(1,8)
local PD = math.random(1,6)

if CD == 1 then  Result = Color3.fromRGB(200,200,200)
elseif CD == 2 then   Result = Color3.fromRGB(255,255,0)
elseif CD == 3 then   Result = Color3.fromRGB(0,255,255)
elseif CD == 4 then   Result = Color3.fromRGB(0,255,0)
elseif CD == 5 then   Result = Color3.fromRGB(255,0,255)
elseif CD == 6 then   Result = Color3.fromRGB(255,0,0)
elseif CD == 7 then   Result = Color3.fromRGB(0,0,255)
elseif CD == 8 then   Result = Color3.fromRGB(0,0,0)
end

if PD == 1 then  PartR = script.Parent["Body Colors"].HeadColor3
elseif PD == 2 then  PartR = script.Parent["Body Colors"].LeftArmColor3
elseif PD == 3 then  PartR = script.Parent["Body Colors"].LeftLegColor3
elseif PD == 4 then  PartR = script.Parent["Body Colors"].RightArmColor3
elseif PD == 5 then  PartR = script.Parent["Body Colors"].RightLegColor3
elseif PD == 6 then  PartR = script.Parent["Body Colors"].TorsoColor3
end


	
	PartR = Result
	
end



Less Complex Script (Working)
script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)

They’re both regular scripts, both parented to the NPC. I ran one at a time.

Thanks!

Sorry if the code isn’t the most efficient :​P

1 Like

Is that the whole script? You aren’t setting the player’s body colors, just getting the values you want.

They did not say they were changing the color of the player’s character, they stated “NPC”. :cry:

They said the second script worked, so I assume that is not the case. :cry:

Yes, that’s the whole script. As @Araknala mentioned, it’s an NPC, not a player. Although, can you elaborate on what you mean by not setting the colors?

Why don’t you use the less complex script for each body part, as it is working. If want to make each part change colors individually (at the same time), you can put it all into ONE loop with a math.random variable for EACH line of the less complex script containing the different body parts and their specified colors. I do not know why you assigned a random number containing a color to a body part, when you can make loops for each body part, changing it’s color. I will try to write that out, wait a few minutes. :wink:

They meant as in, setting a player’s character’s “Humanoid” ‘color values’ to the specific color, in the script. :crazy_face:

Here you go, I did what I said, and made a script according to your variables, but much more easier to look at; it is just a copy and paste of color randomizing for each body part. I hope this works as the SOLUTION and if you have any problems, reply and tell me what happened. :blush:

EDIT: I made all of the Color3 RGB values, (100, 100, 100), because I wanted you to change them accordingly to your desired part randomizing color, as I stated in the script. I got that color as it was the one from the simple, but WORKING script. This is like your complex script AND the working script, just much more neat. If this is not what you wanted, reply with anything, I would be glad to help, as I am trying to become a full member of the DevForum! :hugs:

NOTE: If you want all parts corresponding to eachother, to be the same color, use the same “math.random” variable. You can also delay each body part changing by adding “wait()” commands in between their changes.

-- Made by Araknala for Secretum_Flamma's DevForum post.
-- If you want all parts corresponding to eachother, to be the same color, use the same "math.random" variable.

while wait(0.25) do
	local randomTorso = math.random(1, 8)
	if randomTorso == 1 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100) -- Change these RGB values, and the ones below, to the desired colors; for each random number chosen.
	elseif randomTorso == 2 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	elseif randomTorso == 3 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	elseif randomTorso == 4 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	elseif randomTorso == 5 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	elseif randomTorso == 6 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	elseif randomTorso == 7 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	elseif randomTorso == 8 then
		script.Parent["Body Colors"].TorsoColor3 = Color3.fromRGB(100,100,100)
	end
	
	local randomHead = math.random(1, 8)
	if randomHead == 1 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100) -- Change these RGB values, and the ones below, to the desired colors; for each random number chosen.
	elseif randomHead == 2 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	elseif randomHead == 3 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	elseif randomHead == 4 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	elseif randomHead == 5 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	elseif randomHead == 6 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	elseif randomHead == 7 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	elseif randomHead == 8 then
		script.Parent["Body Colors"].HeadColor3 = Color3.fromRGB(100,100,100)
	end
	
	local randomLeftArm = math.random(1, 8)
	if randomLeftArm == 1 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100) -- Change these RGB values, and the ones below, to the desired colors; for each random number chosen.
	elseif randomLeftArm == 2 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftArm == 3 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftArm == 4 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftArm == 5 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftArm == 6 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftArm == 7 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftArm == 8 then
		script.Parent["Body Colors"].LeftArmColor3 = Color3.fromRGB(100,100,100)
	end
	
	local randomRightArm = math.random(1, 8)
	if randomRightArm == 1 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100) -- Change these RGB values, and the ones below, to the desired colors; for each random number chosen.
	elseif randomRightArm == 2 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightArm == 3 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightArm == 4 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightArm == 5 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightArm == 6 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightArm == 7 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightArm == 8 then
		script.Parent["Body Colors"].RightArmColor3 = Color3.fromRGB(100,100,100)
	end
	
	local randomLeftLeg = math.random(1, 8)
	if randomLeftLeg == 1 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100) -- Change these RGB values, and the ones below, to the desired colors; for each random number chosen.
	elseif randomLeftLeg == 2 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftLeg == 3 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftLeg == 4 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftLeg == 5 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftLeg == 6 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftLeg == 7 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomLeftLeg == 8 then
		script.Parent["Body Colors"].LeftLegColor3 = Color3.fromRGB(100,100,100)
	end
	
	local randomRightLeg = math.random(1, 8)
	if randomRightLeg == 1 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100) -- Change these RGB values, and the ones below, to the desired colors; for each random number chosen.
	elseif randomRightLeg == 2 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightLeg == 3 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightLeg == 4 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightLeg == 5 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightLeg == 6 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightLeg == 7 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	elseif randomRightLeg == 8 then
		script.Parent["Body Colors"].RightLegColor3 = Color3.fromRGB(100,100,100)
	end
end
2 Likes

That works! I was even able to compress it a bit, so it uses a few less math.random’s.

You’ve already done a lot, but is there any way to make it more efficient? After 30+ NPC’s, it starts to lag and the color changing becomes 10x, or even 100x slower.

Add a “wait()” command after every body part change, in the script. This should delay everything and reduce lag! I hope this helps! :crazy_face:

1 Like