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