Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

5 comments

0

Is this the exact code in the script, because if it is the indenting might be the problem but idk.

0

It's not the exact code. It's a shortened up version, but this is the important stuff.

0

Is the indenting the same?

0

Yes of course.

0

Any errors?

2 answers

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 comment

1

Yes this works, although you could use 'elseif -- then' statements instead

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

1 comment

0

Dooesn't work.

This discussion is preserved as it was. New answers, comments, and votes are closed.