Can't Change HumanoidRootPart Color in ServerScript? Pls Help

Why won’t the HumanoidRootPart Color on my Character Change?

local humanoidRootPart = nil;
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		humanoidRootPart= char:WaitForChild("HumanoidRootPart")
	end)
end)


local ColorA = math.random(1,255)
local ColorB = math.random(1,255)
local ColorC = math.random(1,255)

humanoidRootPart.Color =  Color3.fromRGB(ColorA, ColorB, ColorC)
1 Like

The code

local ColorA = math.random(1,255)
local ColorB = math.random(1,255)
local ColorC = math.random(1,255)

humanoidRootPart.Color =  Color3.fromRGB(ColorA, ColorB, ColorC)

runs before than PlayerAdded and CharacterAdded have fired (so humanoidRootPart is nil when this code runs). You should move this code to the function connected to CharacterAdded.

local humanoidRootPart = nil;
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		humanoidRootPart= char:WaitForChild("HumanoidRootPart")

		local ColorA = math.random(1,255)
		local ColorB = math.random(1,255)
		local ColorC = math.random(1,255)

		humanoidRootPart.Color =  Color3.fromRGB(ColorA, ColorB, ColorC)
	end)
end)

Do you have the output window open? I’m pretty sure your current code gives an error.

No errors in the output, The new code didnt work as I also had tried that before.

I added a wait to see if the issue was that the player wasn’t fully loaded in yet but that also didn’t work

I am going to keep trying, thanks for the help.

Have you added prints to make sure that the code actually runs?

Ya, the code stops working after the first line

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		humanoidRootPart= char:WaitForChild("HumanoidRootPart")
		

		local ColorA = math.random(1,255)
		local ColorB = math.random(1,255)
		local ColorC = math.random(1,255)

		humanoidRootPart.Color =  Color3.fromRGB(ColorA, ColorB, ColorC)
		
	end)
end)

I believe I found the issue, your script was trying to change the color of the HumanoidRootPart, when there could possibly be body colors in there that’s overriding the color you are setting. I would recommend using BodyColors

--script must be placed somewhere on server (like ServerScriptService)

local humanoidRootPart = nil;
game:GetService("Players").PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local bodyColors = char:WaitForChild("Body Colors")
		humanoidRootPart= char:WaitForChild("HumanoidRootPart")
		
		local ColorA = math.random(1,255)
		local ColorB = math.random(1,255)
		local ColorC = math.random(1,255)
		
		bodyColors.TorsoColor3 = Color3.fromRGB(ColorA, ColorB, ColorC)
	end)
end)

Where is the script located? It should be in ServerScriptService. If it’s in StarterPlayerScripts or StarterCharacterScripts, PlayerAdded will not fire for the first player. Thus, CharacterAdded will only fire when another player spawns because there’s no CharacterAdded connection for the first player because of no PlayerAdded connection for the first player. Also, if it’s in StarterPlayerScripts or StarterCharacterScripts, there will be many scripts doing the same thing which is not good.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.