Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my random torso color script only working off and on?

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

2 answers

I'd recommend using the PlayerAdded event.

Tables

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.

FromRGB

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.

1 comment

0

Looks pretty clean to me. @HondaHazard271 try this.

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

4 comments

0

Still doesn't work first elseif is giving an error

1
0

Additionally you missed the extra ends for the conditional, elseif acts as an end for the first if, and only the last if/elseif/else requires an end

0

ALSO: Instead of just lecturing or providing script, do both.

This discussion is preserved as it was. New answers, comments, and votes are closed.