Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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.

0
Looks pretty clean to me. @HondaHazard271 try this. GamingZacharyC 152 — 5y
Ad
Log in to vote
-2
Answered by 5 years ago

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

0
Still doesn't work first elseif is giving an error JJBIoxxer 50 — 5y
1
Actually BrickColor supports r g b per https://developer.roblox.com/api-reference/datatype/BrickColor clc02 553 — 5y
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 clc02 553 — 5y
0
ALSO: Instead of just lecturing or providing script, do both. GamingZacharyC 152 — 5y

Answer this question