I am trying to make the game choose a random color for the player's torso each time they spawn but it only seems to work off and on. What am I doing wrong here?
torso = game.StarterPlayer.StarterCharacter.Torso playercolor = math.random(1,5) if playercolor == 1 then torso.BrickColor = BrickColor.new(242, 243, 243) end elseif playercolor == 2 then torso.BrickColor = BrickColor.new(196, 40, 28) end elseif playercolor == 3 then torso.BrickColor = BrickColor.new(40, 127, 71) end elseif playercolor == 4 then torso.BrickColor = BrickColor.new(27, 42, 53) end else torso.BrickColor = BrickColor.new(99, 95, 98) end
I'd recommend using the PlayerAdded event.
Instead of using an if statement to check for every random number, you can use a table
to store all of your colors. A key pair style table will allow you to refer back to an index with a number.
Since you're using an rgb value, we're going to utilize FromRGB
which gives you a number range from 0-255. Without FromRGB, you'd get a decimal, which would give you the wrong color.
Should be a Script inside of ServerScriptService
local players = game:GetService('Players') local colors = { Color3.fromRGB(242, 243, 243); Color3.fromRGB(196, 40, 28); Color3.fromRGB(40, 127, 71); Color3.fromRGB(27, 42, 53); Color3.fromRGB(99, 95, 98); } players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) wait() local torso = character:findFirstChild("Torso") if torso then local randcolor = colors[math.random(#colors)] torso.BrickColor = BrickColor.new(randcolor) end end) end)
if you want to go further, you can think of ways to make sure you don't assign the same color twice.
You need to use Color3.fromRGB(R, G, B) your script wont work right now because brickcolors require strings and those strings represent colors EX: Brickcolor.new("Bright blue")
you seem to want to use fromRGB, so every time you see BrickColor.new
change it to Color3.fromRGB
k