How to make a script where when clicking on the key, it goes in your inventory

Hello Fellow Scripters!

I want to make a script where if you click on the MasterKey it goes in your inventory and the MasterKey in Workspace disappears. I also want to make another script where if you hold out the MasterKey in inventory and click on a door, the door gets destroyed. I don’t know where to start and what to do so if anyone can help, it would be well appreciated.

Pictures: MasterKey in workspace
image_2024-12-24_104423142

1 Like

I’m pretty sure MasterKey has to be a Tool with a handle set properly to be held by the player. You can set properties to make the tool not pick up by default I’m pretty sure (could be wrong)

after that, you can connect to the MouseClick event of the ClickDetector, which passes in the player who clicked as a parameter. Reading the relevant reference documentation is always helpful

example as a sever script within the tool:

local tool = script.Parent

local function givePlayerTool(plr)
    tool.Parent = plr.Backpack
end

tool:WaitForChild("ClickDetector").MouseClick:Connect(givePlayerTool)

you can use almost the same script inside of the doors with a click detector with an additional check to see if they are holding the correct key:

local door = script.Parent
local requiredKey = "MasterKey"

local function tryUnlockDoor(plr)
    if plr.Character:FindFirstChild(requiredKey) then
        door:Destroy()
    end
end

door:WaitForChild("ClickDetector").MouseClick:Connect(tryUnlockDoor)

This works because the held tool is in the players character model in workspace, that’s where we can check to see if they are holding the right tool. Let me know if you have further questions!

I might also suggest using attributes or Value objects to store the required key instead of a string variable like that, as it is always important to maintain flexibility in your code

3 Likes

I have added the MasterKey as a tool in Serverstorage. I tested to see if i clicked on the MasterKey it would put the MasterKey Tool from ServerStorage to StarterPack. It would work but the MasterKey Tool wouldn’t appear in my inventory/hot bar.

Server Script inside the MasterKeyB:

local ClickDet = script.Parent.ClickDetector
local MasterKeyB = workspace.Build.MasterKey
local MasterKeyI = game.ServerStorage.MasterKey

ClickDet.MouseClick:Connect(function()
	MasterKeyB:Destroy()
	MasterKeyI.Parent = game.StarterPack
end)

use tool.Parent = plr.Backpack instead of StarterPack i would say. when declaring your anonymous function, you can pass in plr as a parameter.

local ClickDet = script.Parent.ClickDetector
local MasterKeyB = workspace.Build.MasterKey
local MasterKeyI = game.ServerStorage.MasterKey

ClickDet.MouseClick:Connect(function(plr)
	MasterKeyB:Destroy()
	MasterKeyI:Clone().Parent = plr.Backpack
end)
1 Like

What do you mean by that? Would i need to move the script to the MasterKey inside Serverstorage?

1 Like

I edited the reply. You can see that I clone the key from server storage and put it directly into the players backpack

Ok Thanks! Now what would i need to do so when I hold out the MasterKey and click on a door it destroys the door?

that would be the second script in my first reply, I describe it there. It has to be a server script inside the door, and the door has to have a ClickDetector

would I need to edit the second line of the script where it says “MasterKey”

Yes, that should be the name of the key tool the player holds. The script I sent there was for if the door needs the key with a tool called “MasterKey”

1 Like

The tool in my inventory is called “MasterKey” but when I click on the door while holding the MasterKey the door doesn’t get destroyed. And yes I have added the ClickDetector in the Door

Are there errors in the F9 menu? Show me if there are. Also show me how you have the script placed in the workspace viewer


Screenshot 2024-12-24 114159

2 Likes

try using this instead, as my previous script assumed your Parent model did not contain the door frame (that is not something you want to delete)

local door = script.Parent:WaitForChild("Door")
local requiredKey = "MasterKey"

local function tryUnlockDoor(plr)
    if plr.Character:FindFirstChild(requiredKey) then
        door:Destroy()
    else
        print("Player does not have " .. requiredKey .. " in their character")
    end
end

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(tryUnlockDoor)

if it gives that print output, go into your character model in workspace and verify that “MasterKey” actually exists there when you hold it

1 Like

Actually, I found the issue here. this forum post here explains that holding tools does not allow the ability to click on ClickDetector instances. I will look around for a fix to this

I personally would use a LocalScript that controls the tool, and fires an “UnlockDoor” event if the player clicks on a door. then you can use almost the same server-side logic (also gets rid of the need for a click detector for unlocking doors)

I will also try to find a solution. Thanks for you help!

1 Like

try enabling the ManualActivationOnly property on the master key. this should make it so you can use click detectors while holding the tool

1 Like