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

Changing color script isn't adding 1 to a value to change the color of "Target". What is going on?

Asked by
Rukreep 15
2 years ago
local value = 0
if value == 0 then
                value = value + 1
                Target.BrickColor = BrickColor.new("Black")
            end
            if value == 1 then
                value = value + 1
                Target.BrickColor = BrickColor.new("White")
            end
            if value == 2 then
                value = value -2
                Target.BrickColor = BrickColor.new("Mid gray")
            end

I'm doing a building game but this doesn't work as intended. When a player clicks "Target", it checks if value is 0 and if so, it adds 1. If the player clicks "target" again it checks if value is1 and changes color and adds 1. Finally, when "target" is clicked, it checks for the value to be 2 and changes color and resets back to 0 by subtracting 2. Then when the player clicks "target" again the cycle repeats. I tested this and when I click "target" the color changes to white instead of what's supposed to be black. If I click it again nothing happens. Please help me.

0
Is this the exact code in the script, because if it is the indenting might be the problem but idk. Penguin_Dveloper -4 — 2y
0
It's not the exact code. It's a shortened up version, but this is the important stuff. Rukreep 15 — 2y
0
Is the indenting the same? Penguin_Dveloper -4 — 2y
0
Yes of course. Rukreep 15 — 2y
0
Any errors? SWX253 2 — 2y

2 answers

Log in to vote
1
Answered by
Robowon1 323 Moderation Voter
2 years ago

The program will continue through each if statement because you're adding 1 to value each time then proceeding into another if statement that's condition is value is 1 higher than before. To fix this, add a function that uses return statements.

local Target = workspace:WaitForChild("Part") -- change this
local CD = Target.ClickDetector

local value = 0

function changeColor()
    if value == 0 then
        value = value + 1
        Target.BrickColor = BrickColor.new("Black")
        return
    end

    if value == 1 then
        value = value + 1
        Target.BrickColor = BrickColor.new("White")
        return
    end

    if value == 2 then
        value = value -2
        Target.BrickColor = BrickColor.new("Mid gray")
        return
    end
end

CD.MouseClick:connect(changeColor)


1
Yes this works, although you could use 'elseif -- then' statements instead appxritixn 2235 — 2y
Ad
Log in to vote
-2
Answered by 2 years ago

Indenting needs to be corrected im guessing

if value == 0 then
    value = value + 1
    Target.BrickColor = BrickColor.new("Black")
end

if value == 1 then
    value = value + 1
    Target.BrickColor = BrickColor.new("White")
end

if value == 2 then
    value = value -2
    Target.BrickColor = BrickColor.new("Mid gray")
end
0
Dooesn't work. Rukreep 15 — 2y

Answer this question