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

Wont Pick A Random color when touched?

Asked by 3 years ago

Script :


local function onTouch(hit) end game.Workspace.Part.BrickColor = math.random()

error : 21:24:22.193 - Workspace.Part.Script:5: invalid argument #3 (BrickColor expected, got number)

0
local Name = "Part" local Touch = workspace:FindFirstChild(Name) debounce = false Touch.Touched:Connect(function(touch) if touch.Parent:FindFirstChild("Humanoid") then if debounce == false then Touch.BrickColor = BrickColor.Random(math.random(1,155), math.random(1,155), math.random(1,155)) debounce = true wait(3) debounce = false end end end) TheBeastMare 3 — 3y

2 answers

Log in to vote
1
Answered by
xEmmalyx 285 Moderation Voter
3 years ago
Edited by JesseSong 3 years ago

Problems with original code

math.random() has no arguments, therefore it will randomly generate a real number between 0 and 1.

You are setting the "BrickColor" property of your part to a number between 0 and 1.

Here is a list of BrickColor Codes for reference List of BrickColor Codes

Solution

If you want to randomize the colour there are 2 options I can recommend.

Color3.fromRGB() -- Ranges from 0-255 per value
BrickColor.random() -- Gives a random brick colour from the preset colours

So you'd want to change your code to something like this

game.Workspace.Part.BrickColor = BrickColor.random()

or

local r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255)
-- Randomizes 3 values from 0 to 255

game.Workspace.Part.Color = Color3.fromRGB(r, g, b)
-- Creates a colour using the 3 values from earlier

I would recommend the latter option (Color3.fromRGB) as it is more random than the former (BrickColor.random).

Tip for the poster: I'd advise you to look at articles that have already been addressed as this question is one of a kind question that has been covered at least thrice or more. These include: Documentations from the developer hub, dev forums, "Scripting Helpers!"

Thanks, JesseSong!

0
EDIT: Added more comments and "problem" section xEmmalyx 285 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Brick colors are ran through three different values and those are the three primary colors (red green blue) . You are giving the script one value and there for the script can’t change the value without all three values given. Try doing this

Local red = math.random()
Local green = math.random()
Local blue = math.random()

game.workspace.part.brickcolor = brickcolor.new(red, green, blue)
0
? JordanTheDev_Team 33 — 3y

Answer this question