How would I make a randomized color consistent

Hello,

Forgive me if the title is vague or misleading, couldn’t word it any better for I am illiterate!

I have this server script - it is meant to pick a random color for the players torso when they join, simple enough.

function onPlayerAdded(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local bodyColors = char["Body Colors"]
	
	local torsoColors = {"Bright red", "Institutional white", "Bright blue", "Medium stone grey", "Dark stone grey"}

	local currentColor = math.random(1, #torsoColors)
	bodyColors.TorsoColor = BrickColor.new(torsoColors[currentColor])

end

game.Players.PlayerAdded:Connect(onPlayerAdded)

The script itself works fine, the problem I am having is the fact that the color is not consistent. What a shocker!
What would be the best way to store the chosen torso color, and keep it consistent throughout the entire game, including when they leave and rejoin? The only thing I can think of would be to have the BrickColor sent to a StringValue, which is then saved via datastores. Have the script check for a value, if a value is found it won’t run. However that seems complicated for a small thing like changing a torsos color.

Thanks in advanced, and I apologize if the question here is… silly.

1 Like

After some messing around, I got this

function onPlayerAdded(player)
	local char = player.Character or player.CharacterAdded:Wait()
	local bodyColors = char["Body Colors"]
	
	local torsoColors = {"Bright red", "Institutional white", "Bright blue", "Medium stone grey", "Dark stone grey"}

	local chosenColor = math.random(1, #torsoColors)
	bodyColors.TorsoColor = BrickColor.new(torsoColors[chosenColor])
	local currentColor = tostring(torsoColors[chosenColor])
	
	local value = Instance.new("StringValue")
	value.Name = "TorsoColor"
	value.Parent = player
	value.Value = currentColor

end

game.Players.PlayerAdded:Connect(onPlayerAdded)

This creates a value upon the player joining that stores the select color. In another script, located in StarterCharacterScripts - the value is picked up, read, and the players torso color is changed accordingly.

local char = script.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local value = player:FindFirstChild("TorsoColor")

if value then
	char["Body Colors"].TorsoColor = BrickColor.new(value.Value)
end

Although it does not make the torso color consistent across games, and it probably isn’t the neatest way to go about this, it works. I suppose I’ll mess around with DataStores a little and see if I can complete the entire job.

3 Likes