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

[Solved(?)]Key won't open locked door?

Asked by 5 years ago
Edited 5 years ago

So I have a locked door, and you need a key for it and I made a localscript that makes the key unlock the door (I'd like to keep it that way) but the problem is, when you collect the key, and even if you don't hold it, the script doesn't even print anything (which it does when you don't have the key)

workspace.lockedDoor.Touched:Connect(function(hit)
print("touched")
if hit.Parent.Name == "Key" then
print("unlocked")
end
end)

2 answers

Log in to vote
1
Answered by
BielNS 40
5 years ago

Bro, First of all you need to put debounce

debounce = false
local part = script.Parent

part.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        print("Door Touched!")
        if hit.Name == "Key" then
            print("Door Opened!")
        end
        wait(1)
        debounce = false
    end
end

Second of all, are your tool named "Key"? because tool is not a part, you need to change the script or it wont work.

debounce = false
local part = script.Parent

part.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        print("Door Touched!")
        if hit.Parent.Name == "Key" and hit.Parent.ClassName == "Tool" then
            print("Door Opened!")
        end
        wait(1)
        debounce = false
    end
end

See the difference? the tool is not a part, but you can get the tool by the part, Using the part parent to detect if the parent is a tool, if it worked please mark this as an "Answer" and lock the thread (idk if it locks when u mark a "Anwer".

0
Turns out I made a dumb mistake :/ Boi52893642864912 69 — 5y
0
sad BielNS 40 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I see where I went wrong: I put that line of code inside a script that already has code in it that opens a door, and when you open that door, the added code stops working.

Answer this question