Jump to content

Script to remove key on Door Unlock


se7enraven

Recommended Posts

Hello! I am requesting a script which removes the key used to unlock it, or posts a message. So basically it is a regular door, but removes the key when opened or posts a specific message if the player does not have the key.

 

I am working with the following conditions:

 

-"DoorA" is an animated door load door. It is locked and requires "KeyA" to unlock.

 

-If the player does not have "KeyA", door will remained locked and post "MessageNoGo".

 

-if the player has "KeyA", "DoorA" will unlock and remove "KeyA" from the player's inventory. Once open, player will transport to linked cell.

 

-"DoorA" will lock again after player transport, and requires another "KeyA" to unlock.

 

Other info:

 

-This script must be applied to different doors. ("DoorA", "DoorB", etc.)

 

-Each door requires a different key. ("KeyA", "KeyB", etc.)

 

I thank you for your time and reading this, and if the script is used, I promise to credit the script author on my mod release.

 

-Peace!

 

 

 

 

Link to comment
Share on other sites

There's a thousand ways to do this but there's one with the key being a misc item. (Useful for keycards)

 

 

You could put this on the door and don't forget to fill out the properties afterwards




MiscObject Property TheKey Auto
Message Property NoKeyMsg Auto

Event OnActivate (ObjectReference AkActionRef)
If AkActionRef == Game.Getplayer()
If Game.Getplayer().geitemcount (TheKey) > 0
Self.unlock()
Game.Getplayer().removeitem (TheKey,1)
Else
NoKeyMsg.show()

Endif
Endif
EndEvent

Additionally the same script but with a timer so the door will lock itself.

MiscObject Property TheKey Auto
Message Property NoKeyMsg Auto

Event OnActivate (ObjectReference AkActionRef)
If AkActionRef == Game.Getplayer()
If Game.Getplayer().geitemcount (TheKey) > 0
Self.unlock()
StartTimer (5, 10)
Game.Getplayer().removeitem (TheKey,1)
Else
NoKeyMsg.show()

Endif
Endif
EndEvent

Event OnTimer (Int AiTimerID)
If AiTimerID == 5

Self.lock()
Endif
EndEvent

 

Link to comment
Share on other sites

If you're going with a key maybe try this

Key Property TheKey Auto
Message Property NoKeyMsg Auto

Event OnActivate (ObjectReference AkActionRef)
If AkActionRef == Game.Getplayer()
If Game.Getplayer().geitemcount (TheKey) > 0
StartTimer (5, 10)
Game.Getplayer().removeitem (TheKey,1)
Else
NoKeyMsg.show()

Endif
Endif
EndEvent

Event OnTimer (Int AiTimerID)
If AiTimerID == 5

Self.lock()
Endif
EndEvent

 

Link to comment
Share on other sites

Hey guys! Thanks for the reply! I tried using the script:

 

Scriptname FXMscriptMEtroTrainTeleport extends ObjectReference

Key Property TheKey Auto
Message Property NoKeyMsg Auto

Event OnActivate (ObjectReference AkActionRef)
If AkActionRef == Game.Getplayer()
If Game.Getplayer().geitemcount (TheKey) > 0
StartTimer (5, 10)
Game.Getplayer().removeitem (TheKey,1)
Else
NoKeyMsg.show()

Endif
Endif
EndEvent

Event OnTimer (Int AiTimerID)
If AiTimerID == 5

Self.lock()
Endif
EndEvent

 

I get these errors:

 

Compiling "FXMscriptMEtroTrainTeleport"...
C:\Users\se7enraven\AppData\Local\Temp\PapyrusTemp\FXMscriptMEtroTrainTeleport.psc(8,20): geitemcount is not a function or does not exist
C:\Users\se7enraven\AppData\Local\Temp\PapyrusTemp\FXMscriptMEtroTrainTeleport.psc(8,41): cannot compare a void to a int (cast missing or types unrelated)
No output generated for FXMscriptMEtroTrainTeleport, compilation failed.

Batch compile of 1 files finished. 0 succeeded, 1 failed.
Failed on FXMscriptMEtroTrainTeleport

 

Thank you for your time!

Link to comment
Share on other sites

So, the message posts when no key is in inventory, which is good. When the key IS in the inventory, the message still posts, but the door opens and works anyway. The only other problem is, the door does not lock again. I am wondering if maybe I should attack this problem in a different way? The idea was to have a metro train system, and the key (metro pass) is required to access the trains to move from station to station. I am starting to wonder if maybe I should have the train "door" just be an activator, and teleports the player to a marker in another cell? Being that you are an expert at this (I have "The Wilderness" and "Marshlands" installed and playing them) what do you think I should do? I Thank you for your help!

Link to comment
Share on other sites

When the key IS in the inventory, the message still posts, but the door opens and works anyway.

 

To suppress the "Key has been removed." notification:

Game.Getplayer().RemoveItem(TheKey, abSilent = True)

The only other problem is, the door does not lock again. I am wondering if maybe I should attack this problem in a different way?

 

Try RegisterForPlayerTeleport(). (I haven't used it in an OnActivate() event but it definitely works with load doors).

(...)

Event OnActivate (ObjectReference akActionRef)
        If AkActionRef == Game.Getplayer()
                 RegisterForPlayerTeleport()            ; Add this line to your script
(...)


Event onPlayerTeleport()
        ObjectReference DoorRef = (Self as ObjectReference)
        DoorRef.Lock()
	DoorRef.SetLockLevel(254)    ; so it will require its Key
        UnregisterForPlayerTeleport()
EndEvent
Link to comment
Share on other sites

Awesome! I think that did it. It's a little buggy, as sometimes the message still posts, but I can use it. Thank you Zorkaz and LarannKiar for the good work! When the mod is released, I will make sure to add you to the credits. This is what the final script looks like:


Scriptname FXM:FXMscriptMetroTainDoor02 extends ObjectReference

Key Property TheKey Auto
Message Property NoKeyMsg Auto

Event OnActivate (ObjectReference AkActionRef)
If AkActionRef == Game.Getplayer()
	RegisterForPlayerTeleport()            ; Add this line to your script
If Game.Getplayer().getitemcount (TheKey) > 0
Game.Getplayer().RemoveItem(TheKey, 1,  abSilent = True)
Else
NoKeyMsg.show()

Endif
Endif
EndEvent

Event onPlayerTeleport()
        ObjectReference DoorRef = (Self as ObjectReference)
        DoorRef.Lock()
	DoorRef.SetLockLevel(254)    ; so it will require its Key
        UnregisterForPlayerTeleport()
EndEvent
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...