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

My Keycard door script won't work if i want it to be opened by two types?

Asked by 6 years ago

I have a keycard door script that I put inside a part and when a tool called masterkey a tool called dungeonkey touches it it turns cancollide off, but the door opens when you touch it without it

script.Parent.Touched:connect(function(p)
 if p.Parent.Name == "MasterKey" or "DungeonKey" then -- 
  script.Parent.Transparency = 0.5
  script.Parent.CanCollide = false
  wait(1)
  script.Parent.Transparency = 0
  script.Parent.CanCollide = true
 end
end)

Be aware that the single key version of this script works, aka

script.Parent.Touched:connect(function(p)
 if p.Parent.Name == "MasterKey" then -- 
  script.Parent.Transparency = 0.5
  script.Parent.CanCollide = false
  wait(1)
  script.Parent.Transparency = 0
  script.Parent.CanCollide = true
 end
end)

Works perfectly with FE on

0
You have to compare the name with both. You're telling the script "is the name equal to 'MasterKey', or is 'DungeonKey' valid?" To fix this, just simply do like what you did with "MasterKey". :) TheeDeathCaster 2368 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hey Fennict,

Your problem is quite simple. The problem is the if statement as you might have noticed by now. It's because when you check for DungeonKey, you are just asking the script to check if the string "DungeonKey" is a thing. And, you checked "DungeonKey" so, it's always going to run because you asking "if DungeonKey then" is the same thing as you asking "if true then."

So, I'm saying that this line of code:

if "DungeonKey" then
    print("X");
end

is the same thing as this line of code:

if true then
    print("X");
end

And, when you use operators like and/or, you need to re-define what you are checking. In the second part of the if statement, you didn't define what you were checking for, all you checked was "if DungeonKey then" Therefore, you are using the first line of code that you see above.

Now, it's very easy to change this, I'm sorry if I confused you because it's really not that easy to explain for me. Anyways, all you need to do is check if the name is equal to "DungeonKey" again. Here, I will show you below how this is done.

Enhancement and Fixed Issue

script.Parent.Touched:Connect(function(p)
    if p.Parent.Name == "MasterKey" or p.Parent.Name == "DungeonKey" then 
        script.Parent.Transparency = 0.5
        script.Parent.CanCollide = false
        wait(1)
        script.Parent.Transparency = 0
        script.Parent.CanCollide = true     
    end
end)

Well, I hope I helped and if u have any questions, post a comment below.

(P.S. The enhancement was just changing :connect() to :Connect() because :connect() is deprecated)

Thanks,

~~ KingLoneCat

0
Well instead of doing or use elseif. DogHouseForMe 0 — 5y
Ad

Answer this question