Help with scripting an unlockable door (like piggy)

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)

I know for sure that I’m making a silly mistake…

1 Like

Is the Player defined anywhere? You’d need to add the playerWhoClicked parameter in MouseClick's event.

Click.MouseClick:Connect(function(plr) will also return the player where “plr” will be the player that click it try using that

yea, I haven’t defined the Player anywhere, how do I define it?

local Player = ...

Click.MouseClick:Connect(function(Player)

You can find the player trough your MouseClick event.

it’s not working, i tried so much :frowning:

it didn’t work, i tried oof :frowning:

Call FindFirstChild() on their backpack to search for the item, then run the code associated with unlocking/opening the door.

1 Like

I can provide the code I have! I already worked on a piggy styled game! I just need a moment to get into studio!

1 Like

Click.MouseClick:Connect(function(player)
if player.Backpack:FindFirstChild(“Red Key”) then
Door.CanCollide = false
Door.Transparency = 1
Lock.CanCollide = false
Lock.Transparency = 1
Glass.Transparency = 1
keysound:Play()
player.Backpack:FindFirstChild(“Red Key”):Destroy()
end
end)
script.Parent.Touched:Connect(function(part)
if part.Parent.Name == “Red Key” then
Door.CanCollide = false
Door.Transparency = 1
Lock.CanCollide = false
Lock.Transparency = 1
Glass.Transparency = 1
keysound:Play()
part.Parent:Destroy()
end
end)

1 Like
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.

2 Likes

Do I have to change anything else (ex- location of certain objects or the location of door)?
Also tysm this could help me soo much! :slight_smile: can we be friends?

1 Like

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!

2 Likes