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

How do I make this door open based on a part's color?

Asked by 4 years ago

I am adding buttons in my game that you can press which turn the button and screens red to indicate they were pressed. When all buttons are pressed, it should open a secret door and play a sound of the door opening. But when I tested it once I had pressed all the buttons the secret door did not open, is there anything wrong with the code?

if game.Workspace.Screen1.BrickColor == BrickColor.new("Really red") then
    if game.Workspace.Screen2.BrickColor == BrickColor.new("Really red") then
        if game.Workspace.Screen3.BrickColor == BrickColor.new("Really red") then
            script.Parent.evad:Play()
            wait(2)
            script.Parent:Destroy()
        end
    end
end

The script detects if the screens are red (on) and then destroys itself after making the door opening noise. Evad is the sound of the door opening.

3 answers

Log in to vote
0
Answered by
DesertusX 435 Moderation Voter
4 years ago
Edited 4 years ago

I assume that unlike in R4INBOWDASH's answer, your doors aren't red when the game just starts. You are only checking if the doors are red once, when the game just loads. What you should really do is to keep on checking them. You can do this while using a repeat - until loop.

Here is the fixed script:

local checked = false --We are defining a boolean here to make sure we didn't already check it.

repeat wait()
if game.Workspace.Screen1.BrickColor == BrickColor.new("Really red") then
    if game.Workspace.Screen2.BrickColor == BrickColor.new("Really red") then
            if game.Workspace.Screen3.BrickColor == BrickColor.new("Really red") then
            if not checked then --Checking if we didn't already check.
                    script.Parent.evad:Play()
                    wait(2)
                script.Parent:Destroy()
            end
            until
            game.Workspace.Screen1.BrickColor == BrickColor.new("Really red") and
            game.Workspace.Screen2.BrickColor == BrickColor.new("Really red") and
            game.Workspace.Screen3.BrickColor == BrickColor.new("Really red") and
            game.Workspace.Screen4.BrickColor == BrickColor.new("Really red")
            --Will stop when the conditions are met.
            end
    end
end

Hope this helps!!

Don't forget to accept this if it does!!!

0
Thank you! But at line 11 where it says until there is an error where the script expected end to close 'then' at line 7 but got until. Kirito40000000000 7 — 4y
0
I edited my script! It should work now. DesertusX 435 — 4y
0
There was just a missing "end". DesertusX 435 — 4y
Ad
Log in to vote
0
Answered by
MemezyDev 172
4 years ago
Edited 4 years ago

I'm assuming that the issue was the ifs. It worked for me when I did this:

if game.Workspace.Screen1.BrickColor == BrickColor.new("Really red") and game.Workspace.Screen2.BrickColor == BrickColor.new("Really red") and game.Workspace.Screen3.BrickColor == BrickColor.new("Really red") then
            script.Parent.evad:Play()
            wait(2)
            script.Parent:Destroy()
end

the sound played and the parent got destroyed! Please accept my answer if it works. If it doesn't, please let me know

0
I also forgot to put that you want to check the brick colors on the server to prevent exploits, and you should have this run when a button is pressed or using part.Changed MemezyDev 172 — 4y
0
I might have been doing something wrong since the script worked for you but it did not work for me. Thank you though! Kirito40000000000 7 — 4y
Log in to vote
0
Answered by 4 years ago

You could just have a value that is incremented when each button is pressed And if one deactivated decrement it. Then have a .Changed listener on the value and if it hits 3 then open the door.

Answer this question