That's great that you started coding 3 days ago! Let's reformat your program so it'll be easier for us to read. Add that structuree.
local base = game.Workspace.Baseplate
local part = game.Workspace.Part
part.Touched:Connect(function(hit)
base.BrickColor = BrickColor.Black()
if base.BrickColor = BrickColor.Black() then
base.BrickColor = BrickColor.Purple()
if base.BrickColor = BrickColor.Purple() then
base.BrickColor = BrickColor.Blue()
if base.BrickColor = BrickColor.Blue() then
base.BrickColor = BrickColor.Black()
end
end
end
end
"=" is the assignment operator.
Once you open "(" anything with a parenthesis, you must close ")" it, especially when using the Connect method.
Let's get down to analyzing your code. If all is good and dandy, then the following code is errorless.
local base = game.Workspace.Baseplate
local part = game.Workspace.Part
part.Touched:Connect(function(hit)
base.BrickColor = BrickColor.Black()
if base.BrickColor == BrickColor.Black() then
base.BrickColor = BrickColor.Purple()
if base.BrickColor == BrickColor.Purple() then
base.BrickColor = BrickColor.Blue()
if base.BrickColor == BrickColor.Blue() then
base.BrickColor = BrickColor.Black()
end
end
end
end)
Summary of the code.
Once "part" is touched,
set "base" to Black.
if "base's" color is Black, then change it to Purple.
if it's color is Purple, then change it to Blue.
if it's color is Blue, then change it back to Black.
]]
Problem here is, you'll never see the program alternate colors because you made it go through all the colors.
- thing touches part.
- base is black.
- first "if" statement: is it black? change to purple.
- changed to purple.
- second "if": is it purple? change to blue.
- changed to blue.
- third "if": is it blue? change to black.
- back to square one.
Let's prevent that by changing the structure of your "if" statements and relocate line 5.
TL;DR
My suggestion: Don't nest your "ifs". Separate it into compound "if"/"elseif"/"else" statements.
local base = game.Workspace.Baseplate
local part = game.Workspace.Part
base.BrickColor = BrickColor.Black()
part.Touched:Connect(function(hit)
if base.BrickColor == BrickColor.Black() then
base.BrickColor = BrickColor.Purple()
elseif base.BrickColor == BrickColor.Purple() then
base.BrickColor = BrickColor.Blue()
elseif base.BrickColor == BrickColor.Blue() then
base.BrickColor = BrickColor.Black()
end
end)
The final code serves to solve your problem. Comment below if you have any questions regarding improvements, coding practice advice, anything.
1 comment
Can you put the code in a ode block, https://forum.scriptinghelpers.org/topic/82/how-to-format-questions-answers-on-the-main-site