Author Sticky: Post Working Scripts Here
Tenchi Masaki Posted: Wednesday, 24 July 02:26AM
I was looking for something like this but couldn't find it so maybe it's something that hasn't been posted yet.

Script Name: Generic Close and Lock Placeable with key.

What it does: This will check your inventory when you close the item. if it finds the key that is needed to lock itself it will lock itself and tell you you used your key. If it doesn't find the key it will tell you you don't have the right key.

I tried it with success in a few different objects, I don't think there are any problems with it.

NWScript:
//::///////////////////////////////////////////////

//:: Name

//:: FileName

//:: Copyright (c) 2001 Bioware Corp.

//:://////////////////////////////////////////////

/*

    This script works in the OnClose script of

    lockable placeables. It will check for the

    key needed to lock itself and then act

    accordingly. It will notify the user of the

    presence, or lack there of, of the proper

    key. You must have set up the use of a key to

    make this work properly.

*/

//:://////////////////////////////////////////////

//:: Created By: Frank V. Schumacher

//:: Created On: July 23, 2002 at 7:03 PM

//:://////////////////////////////////////////////

void main()

{

    object oLocker = GetLastClosedBy();

    object oItemToLock = OBJECT_SELF;

    string oKeyTag = GetLockKeyTag(oItemToLock);

    if(GetItemPossessedBy(oLocker, oKeyTag) == OBJECT_INVALID)

        {

            SpeakString("You haven't the right key.", TALKVOLUME_TALK);

        }

    else

        {

            SetLocked(OBJECT_SELF, 1);

            SpeakString("You have used your key.", TALKVOLUME_TALK);

        }

}