Ok so I am working on a Piggy game, and I needed help with scripting an unlockable door.
I have already made a script to unlock the door when you have key in your hands, but I am having problem searching/making the script where you have the key in your backpack and not your hands, This type of method requires a ClickDetector I assume. also don’t mind if I made a silly mistake as I am just a beginner.
local RedKey = workspace.Items["Red Key"]
local Door = script.Parent
local Click = Door.ClickDetector
local Lock = Door.Parent.Lock
local Glass = Door.Parent.Glass
local keysound = Instance.new("Sound")
keysound.Parent = Door
keysound.SoundId = "rbxassetid://4381793351"
keysound.Volume = 9
Click.MouseClick:Connect(function()
if Player.Backpack == "Red Key" then
Door.CanCollide = false
Door.Transparency = 1
Lock.CanCollide = false
Lock.Transparency = 1
Glass.Transparency = 1
keysound:Play()
RedKey:Destroy()
end
end)
script.Parent.Touched:Connect(function(p)
if p.Parent.Name == "Red Key" then
Door.CanCollide = false
Door.Transparency = 1
Lock.CanCollide = false
Lock.Transparency = 1
Glass.Transparency = 1
keysound:Play()
RedKey:Destroy()
end
end)
local RedKey = workspace.Items["Red Key"]
local Door = game.Workspace.RedDoor
local Click = Door.ClickDetector
local Lock = Door.Lock
local keysound = Instance.new("Sound")
keysound.Parent = Door
keysound.SoundId = "rbxassetid://4381793351"
keysound.Volume = 9
local function Unlock(key, door)
for i, v in pairs(door:GetChildren()) do
if v:IsA("BasePart") then
v.Transparency = 1
v.CanCollide = false
keysound:Play()
key:Destroy()
end
end
end
Click.MouseClick:Connect(function(player)
if player.Backpack:FindFirstChild("Red Key") then
Unlock(player.Backpack:FindFirstChild("Red Key"), Door)
end
end)
Door.Door.Touched:Connect(function(p)
if p.Parent.Name == "Red Key" then
Unlock(p.Parent, Door)
end
end)
You do need to change some stuff up, since this is my code.
Yes, you do need to change up some stuff. Like you have to define your door etc. And second, obviously! We can!
Don’t worry, since developers are always to help each other!