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

How do I fix my wall color changer script?

I have made a LocalScript that is for a Text Button. The text button is supposed to look at another Text Box and see what a player typed in. If the player typed in a valid colour and clicked the text button, it will change the colour of the wall to the colour they typed in. This does work, but it only works for the first time you do it. You cannot change the colour again. How do I fix this issue?

Code:

local wall = game.Workspace.Tower
local button = script.Parent
local Box = script.Parent.Parent.ColorBox

button.MouseButton1Click:Connect(function(plr)
	if Box.Text == "Green" or "green" then
		wall.BrickColor = BrickColor.new("Lime green")
		if Box.Text == "Red" or "red" then
			wall.BrickColor = BrickColor.new("Really red")
			if Box.Text == "Pink" or "pink" then
				wall.BrickColor = BrickColor.new("Pink")
				if Box.Text == "Blue" or "blue" then
					wall.BrickColor = BrickColor.new("Medium blue")
					if Box.Text == "Yellow" or "yellow" then
						wall.BrickColor = BrickColor.new("New Yeller")
						if Box.Text == "Orange" or "orange" then
							wall.BrickColor = BrickColor.new("Deep orange")
							if Box.Text == "Violet" or "violet" or "Purple" or "purple" then
								wall.BrickColor = BrickColor.new("Royal purple")
							end
						end
					end
				end
			end
		end
	end
end)

1 answer

✓ Accepted answer

GENERAL PRACTICE

Use workspace instead of game.Workspace

Use :WaitForChild() to make sure an item exists before changing it

Use Activated instead of MouseButton1Click since Activated works for all devices while MouseButton1Click only works for computers.

Use string.lower() on the TextBox Text so that if the player typed for example ORanGE the box would still change color (string.lower makes the string into all lowercase letters)

If you need to check the string twice, you have to restate it as in Line 18, you shouldn't use "or" immediately after since it is not being compared.

ISSUES

While this isn't technically a scripting error, the problem lies in that fact that you continuously use if-then statements, so according to your structure then player needs to type "green" after which it will IMMEDIATELY check if the text is red, then if by some miracle it is, that the text is pink, and so forth.

To circumvent this, use "elseif" to add extra comparisons if the text is not "green".

(This also reduces the amount of "end"s you need in your code dramatically)

REVISED LOCAL SCRIPT

local wall = workspace:WaitForChild("Tower")
local button = script.Parent
local Box = button.Parent:WaitForChild("ColorBox")

button.Activated:Connect(function(plr)
	if string.lower(Box.Text) == "green" then
		wall.BrickColor = BrickColor.new("Lime green")
	elseif string.lower(Box.Text) == "red" then
		wall.BrickColor = BrickColor.new("Really red")
	elseif string.lower(Box.Text) == "pink" then
		wall.BrickColor = BrickColor.new("Pink")
	elseif string.lower(Box.Text) == "blue" then
		wall.BrickColor = BrickColor.new("Medium blue")
	elseif string.lower(Box.Text) == "yellow" then
		wall.BrickColor = BrickColor.new("New Yeller")
	elseif string.lower(Box.Text) == "orange" then
		wall.BrickColor = BrickColor.new("Deep orange")
	elseif string.lower(Box.Text) == "violet" or string.lower(Box.Text) == "purple" then
		wall.BrickColor = BrickColor.new("Royal purple")
	end
end)

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