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

Need help with MouseClick Event?

Asked by
JoneXI 51
5 years ago

Hello, I have a problem with my script. I have three different parts every second they change their color from red to green with math.random. Basiclly its a color cycle. So my problem is that have a part and its transparency is 1.


if part1.BrickColor = Brickcolor.new("Dark Green") then click.MouseClick:Connect(function()( Number1.Transparency = 0 end) end

thats what I want to happen. When the button is red then nothing happens - thats ok. When it gets green and I click on it - it works, but when it hits green and then I wait till it hits red again i click and it works as well - thats bad. I know it might be complicated to understand... Any solutions? whole Script:

Number1 = script.Parent.Number1
part1 = script.Parent.part1
part2 = script.Parent.part2
part3 = script.Parent.part3
click = script.Parent.part1.ClickDetector
-- Properties 
Number1.Transparency = 1
part1.BrickColor = BrickColor.new("Really red")
part2.BrickColor = BrickColor.new("Really red")
part3.BrickColor = BrickColor.new("Really red")
--Code
while true do
    RandomPart = math.random(1,3)
    wait(1)

if RandomPart == 1 then 
    part1.BrickColor = BrickColor.new("Dark green")
    part2.BrickColor = BrickColor.new("Really red")
    part3.BrickColor = BrickColor.new("Really red")
    part4.BrickColor = BrickColor.new("Really red")
    part5.BrickColor = BrickColor.new("Really red")
    part6.BrickColor = BrickColor.new("Really red")
    part7.BrickColor = BrickColor.new("Really red")
    part8.BrickColor = BrickColor.new("Really red")
    part9.BrickColor = BrickColor.new("Really red")
elseif RandomPart == 2 then 
    part1.BrickColor = BrickColor.new("Really red")
    part2.BrickColor = BrickColor.new("Dark green")
    part3.BrickColor = BrickColor.new("Really red")
    part4.BrickColor = BrickColor.new("Really red")
    part5.BrickColor = BrickColor.new("Really red")
    part6.BrickColor = BrickColor.new("Really red")
    part7.BrickColor = BrickColor.new("Really red")
    part8.BrickColor = BrickColor.new("Really red")
    part9.BrickColor = BrickColor.new("Really red")
elseif RandomPart == 3 then
    part1.BrickColor = BrickColor.new("Really red")
    part2.BrickColor = BrickColor.new("Really red")
    part3.BrickColor = BrickColor.new("Dark green")
wait(1)
end
if part1.BrickColor == BrickColor.new("Dark green") then
    click.MouseClick:Connect(function()
    Number1.Transparency = 0
    end)
end
end

Thank you.

2 answers

Log in to vote
0
Answered by 5 years ago

The problem is that once you start listening for the mouseclick, it listens no matter what.

click.MouseClick:Connect(function()
    if part1.BrickColor = BrickColor.new("Dark Green") then
        Number1.Transparency = 0
    end
end)
Ad
Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
5 years ago
Edited 5 years ago

First of all, for changing the color I would recommend using a table instead of separate values.

Ex.

Local PartTable = {PartA, PartB, PartC}

For i=1,#PartTable do
    PartTable[i].PartColor = “Really Red”
end

{} creates a table containing its parameters, i is what item in the table it’s on and a number/variable contained in square brackets [] are which value in the table you want

As for fixing the problem, try having the mouse event happen before checking the color. Also try formatting it as a string instead of color.

Ex.

click.MouseClick.Connect(function()
    if PartTable[1].PartColor == “Really Red” then
        -- code --
    end
end)

For more see Tables

0
{} doesn't make a value a table. {} is the table constructor. In fact, all Lua datatypes but tables are immutable. programmerHere 371 — 5y
0
That is what I meant by “{} make a value a table”, though I do see how that could be confusing. I edited it and it should be fixed. Benbebop 1049 — 5y

Answer this question