Script for a door that needs a key is not working properly

so whats supposed to happen is when you click the button the script checks if you have the key for it or not and if you do then the door will open.

local clickdetector = script.Parent.ClickDetector -

local key = game.StarterPack.VaultKey -- has a red underline under "local"

local player = game.Players.Name

local button = script.Parent

local vaultdoor = script.Parent.Parent.Vaultdoor

vaultKey = false

go1 = false

pkey = false

print("part1")
clickdetector.MouseClick:Connect(function()
	print("part2")
	local player = game.Players.PlayerAdded
	print("part3")
	local ToolName = "VaultKey"
	if player.Backpack:FindFirstChild(ToolName) then -- error   16:15:54.811 - Backpack is not a valid member of RBXScriptSignal
		print("part4")
		vaultKey = true
		pkey = true
		if vaultKey == true then
			print("part5")
			if pkey == true then
				button.BrickColor = BrickColor.new("Bright green")
				vaultdoor.Position = Vector3.new()
				go1 = true
				local TweenService = game:GetService("TweenService")
				
				local goal = {}
				
				goal.Position = Vector3.new(-28.08, 5.76, -14.415)
				
				local tweenInfo = TweenInfo.new(0.2*9) -- (0.5*9)
				print("part6")
				local tween = TweenService:Create(vaultdoor, tweenInfo, goal)
				
				if go1 == true then
					print("part7")
					tween:Play()
				end
			else
				print("part8")
				button.BrickColor = BrickColor.new("Really red")
			end
		end
	else
		print("part9")
		vaultKey = false
		pkey = false
	end
end)

I’m assuming the part that’s breaking is your player variable. The ClickDetector.MouseClick() functions first parameter is the player.

So you could do :

clickdetector.MouseClick:Connect(function(Player)

And that would probably fix your problem.

you have a dash after clickdetector causing the local before key to be underlined.

ohhh ok thank you for telling me that i didn’t see that there

ill try that but im not sure if that will work or not

keeps says there is an error with " 16:29:49.677 - Backpack is not a valid member of RBXScriptSignal"

Because you are using a Event as your variable…

player = game.Player.PlayerAdded

Try what I mentioned before.

here is a fixed version of your script like @1x_Gacha was trying to have you do:

 local clickdetector = script.Parent.ClickDetector

local key = game.StarterPack.VaultKey

local player = game.Players.Name

local button = script.Parent

local vaultdoor = script.Parent.Parent.Vaultdoor

vaultKey = false

go1 = false

pkey = false

print("part1")
clickdetector.MouseClick:Connect(function(player)
	print("part2")
	print("part3")
	local ToolName = "VaultKey"
	if player.Backpack:FindFirstChild(ToolName) then
		print("part4")
		vaultKey = true
		pkey = true
		if vaultKey == true then
			print("part5")
			if pkey == true then
				button.BrickColor = BrickColor.new("Bright green")
				vaultdoor.Position = Vector3.new()
				go1 = true
				local TweenService = game:GetService("TweenService")
				
				local goal = {}
				
				goal.Position = Vector3.new(-28.08, 5.76, -14.415)
				
				local tweenInfo = TweenInfo.new(0.2*9) -- (0.5*9)
				print("part6")
				local tween = TweenService:Create(vaultdoor, tweenInfo, goal)
				
				if go1 == true then
					print("part7")
					tween:Play()
				end
			else
				print("part8")
				button.BrickColor = BrickColor.new("Really red")
			end
		end
	else
		print("part9")
		vaultKey = false
		pkey = false
	end
end)
2 Likes

now it says " 16:37:58.923 - Workspace.Vault.VaultButton.Script:22: attempt to index nil with ‘FindFirstChild’"

Can you show us the line the error is occurring?