Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do I Make A Part Change The Baseplate To Specific Colors?

Asked by 7 years ago

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

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years 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

0
Line 19, you misspelled Black. But good on you for mentioning debounce and detecting player validity! Redbullusa 1580 — 7y
0
tysm!! The only things I do not understand are the TouchEnabled and ColorIndex. haha, but I am so excited to learn code. TY!!! iizWishzii 21 — 7y
Ad
Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
7 years ago
Edited 7 years ago

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 ")"
  1. "=" is the assignment operator.

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

  1. thing touches part.
  2. base is black.
  3. first "if" statement: is it black? change to purple.
  4. changed to purple.
  5. second "if": is it purple? change to blue.
  6. changed to blue.
  7. third "if": is it blue? change to black.
  8. 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.

0
WOOOWW!!! Thank you so much!!! I can't believe how fast i got answers! I am really happy that you gave me such detailed information! One thing though, What is "elseif" and how do I use it? Tyvm!! :D iizWishzii 21 — 7y
0
I linked it under TL;DR. Redbullusa 1580 — 7y

Answer this question