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

How to fix math.random not randomizing part's BrickColor? [closed]

Asked by
R0jym 47
3 years ago
Edited 3 years ago

Hello there, I made a script in which the part named "painter" randomizes the color of another part named "Cover" with the math.random script, however upon trying it the Cover only appears as color Gold with the Paper aswell colored as Gold even though my script clearly says that the Paper should be white, how do I fix this?

NOTE: There is a script inside the "Cover" part which is just cloning it along with it's children

01local RandomColorGenerator = math.random(1, 5)
02local Cover = game.Workspace:WaitForChild("Cover")
03local Paper = Cover.Paper
04local painter = script.Parent
05 
06painter.Touched:Connect(function(Cover)
07    if RandomColorGenerator == 1 then
08        Cover.BrickColor = BrickColor.new("Navy blue")
09        Paper.BrickColor = BrickColor.new("White")
10    end
11    if RandomColorGenerator == 2 then
12        Cover.BrickColor = BrickColor.new("Maroon")
13        Paper.BrickColor = BrickColor.new("White")
14    end
15    if RandomColorGenerator == 3 then
View all 27 lines...
0
Not sure what the issue is here to be honest. However, you could try moving the RandomColorGeneration variable into the start of the function rather than outside of it. KingDomas 153 — 3y
0
I just started learning LUA in a couple of days maybe try FindFirstChild event instead of WaitForChild? JerkDerekThe14alt 11 — 3y

Locked by Xapelize

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
rabbi99 714 Moderation Voter
3 years ago

You wanna get a random number every time you touch it, so you use the math.random function every time you touch it:

01local Cover = game.Workspace:WaitForChild("Cover")
02local Paper = Cover.Paper
03local painter = script.Parent
04 
05painter.Touched:Connect(function(Cover)
06    local RandomColorGenerator = math.random(1, 5)
07    if RandomColorGenerator == 1 then
08        Cover.BrickColor = BrickColor.new("Navy blue")
09        Paper.BrickColor = BrickColor.new("White")
10    end
11    if RandomColorGenerator == 2 then
12        Cover.BrickColor = BrickColor.new("Maroon")
13        Paper.BrickColor = BrickColor.new("White")
14    end
15    if RandomColorGenerator == 3 then
View all 27 lines...
0
but theres no problem even if it doesnt randomize every time, not a solution sne_123456 439 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

So I don't know why this works but I have a theory. It might be because it needs another part to touch it in the original script so you have to know if a player touched it for it to work. I used this Article by roblox to fix this. Also the reason its so different is because when I tried to test the code in studio it wouldn't work what so ever.

01local Cover = workspace:WaitForChild("Cover")
02local Paper = Cover.Paper
03local painter = script.Parent
04 
05local function onPartTouched(otherPart)
06    local RandomColorGenerator = math.random(1, 5)
07    local partParent = otherPart.Parent
08 
09    -- Look for a humanoid in the parent
10    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
11    if humanoid then
12        if RandomColorGenerator == 1 then
13            Cover.BrickColor = BrickColor.new("Navy blue")
14            Paper.BrickColor = BrickColor.new("White")
15        elseif RandomColorGenerator == 2 then
View all 31 lines...