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
2 years ago
Edited 2 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

local RandomColorGenerator = math.random(1, 5)
local Cover = game.Workspace:WaitForChild("Cover")
local Paper = Cover.Paper
local painter = script.Parent

painter.Touched:Connect(function(Cover)
    if RandomColorGenerator == 1 then
        Cover.BrickColor = BrickColor.new("Navy blue")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 2 then
        Cover.BrickColor = BrickColor.new("Maroon")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 3 then
        Cover.BrickColor = BrickColor.new("Gold")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 4 then
        Cover.BrickColor = BrickColor.new("Magenta")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 5 then
        Cover.BrickColor = BrickColor.new("Sea green")
        Paper.BrickColor = BrickColor.new("White")
    end
end)

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 — 2y
0
I just started learning LUA in a couple of days maybe try FindFirstChild event instead of WaitForChild? JerkDerekThe14alt 11 — 2y

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
2 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:

local Cover = game.Workspace:WaitForChild("Cover")
local Paper = Cover.Paper
local painter = script.Parent

painter.Touched:Connect(function(Cover)
    local RandomColorGenerator = math.random(1, 5)
    if RandomColorGenerator == 1 then
        Cover.BrickColor = BrickColor.new("Navy blue")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 2 then
        Cover.BrickColor = BrickColor.new("Maroon")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 3 then
        Cover.BrickColor = BrickColor.new("Gold")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 4 then
        Cover.BrickColor = BrickColor.new("Magenta")
        Paper.BrickColor = BrickColor.new("White")
    end
    if RandomColorGenerator == 5 then
        Cover.BrickColor = BrickColor.new("Sea green")
        Paper.BrickColor = BrickColor.new("White")
    end
end)
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.

local Cover = workspace:WaitForChild("Cover")
local Paper = Cover.Paper
local painter = script.Parent

local function onPartTouched(otherPart)
    local RandomColorGenerator = math.random(1, 5)
    local partParent = otherPart.Parent

    -- Look for a humanoid in the parent
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        if RandomColorGenerator == 1 then
            Cover.BrickColor = BrickColor.new("Navy blue")
            Paper.BrickColor = BrickColor.new("White")
        elseif RandomColorGenerator == 2 then
            Cover.BrickColor = BrickColor.new("Maroon")
            Paper.BrickColor = BrickColor.new("White")
        elseif RandomColorGenerator == 3 then
            Cover.BrickColor = BrickColor.new("Gold")
            Paper.BrickColor = BrickColor.new("White")
        elseif RandomColorGenerator == 4 then
            Cover.BrickColor = BrickColor.new("Magenta")
            Paper.BrickColor = BrickColor.new("White")
        elseif RandomColorGenerator == 5 then
            Cover.BrickColor = BrickColor.new("Sea green")
            Paper.BrickColor = BrickColor.new("White")
        end
    end
end

painter.Touched:Connect(onPartTouched)