I cant copy and paste my code here rn, because I am on my phone, but how would I make it so that when my character touches a block (Part) it turns the BasePlate into either Purple, Blue, or Black..?
I tried doing
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
P.S. I just typed this from my phone just now so it might be incorrect, also please dont judge my coding skills. I just started coding 3 days ago..
Hello SongHanEulzz,
I've been looking at your script as it is right now, and for as far as I can see you're trying to make your script to go through three different colors every time the player touches it. Things I would recommend to add to your script are a cooldown, a number instead of detecting the color and checking if the object that touched your part actually is a player. It would be something like this:
local base = workspace.Baseplate local part = workspace.Part -- Extra variables I added local touchEnabled = true local colorIndex = 0 part.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then -- Check if the touched object is a player if touchEnabled == true then -- If the part may be touched touchEnabled = false -- Change the variable to 'false' if colorIndex == 0 then base.BrickColor = BrickColor.Purple() colorIndex = 1 elseif colorIndex == 1 then base.BrickColor = BrickColor.Blue() colorIndex = 2 elseif colorIndex == 2 then base.BrickColor = BrickColor.Black() colorIndex = 0 end wait(1.2) -- Wait some time in order to color from changing too fast touchEnabled = true -- Set the value to 'true' again end end end)
I hope this got you started! Good luck and don't forget to have fun while learning how to script!
Regards,
DefaultAxis
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.
--So this is what it looks like. I will mark the parts where your script will error. 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 -- right here* base.BrickColor = BrickColor.Purple() if base.BrickColor = BrickColor.Purple() then -- right here* base.BrickColor = BrickColor.Blue() if base.BrickColor = BrickColor.Blue() then -- right here * base.BrickColor = BrickColor.Black() end end end end -- right here** -- * you used = instead of == -- ** you did not close your event using ")"
"=" 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.
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.