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.
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)
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