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