ProximityPrompt Module Script Bug

Hi I want a proximity prompt to require a tool in the players backpack to open a door.

The issue is that the script works but when ever I open the door it gives me the key required to open the door defeating the purpose I have tried adding delays but I can’t find the issue, I am also new to scripting.

(This is a module script)

local RunService = game:GetService("RunService")

local ObjectActions = {}

function  ObjectActions.promptHoldEndedActions(promptObject, player)
	
	local ancestorModel = promptObject:FindFirstAncestorWhichIsA("Model")
	local constraint = ancestorModel:FindFirstChildWhichIsA("HingeConstraint", true)

	if ancestorModel.Name == "Key0" or "Key1" or "Key2" or "Key3" then
		--ancestorModel.PrimaryPart:SetNetworkOwner(nil)
		local KeyTool = ReplicatedStorage.Key:Clone()
		wait(0.5)
		KeyTool.Parent = player.Backpack
	else
		print("No KEY")
	end
	
	
	if ancestorModel.Name == "PadLock" then
		
		local FindTool = player.Backpack:FindFirstChild("Key")
		--ancestorModel.PrimaryPart:SetNetworkOwner(nil)
		if FindTool then
			ancestorModel.MeshesPadLock.Transparency = 1
			
			ancestorModel.Parent.PadUnLock.MeshesLockUNLOCKED.Transparency = 0
			ancestorModel.MeshesPadLock.Attachment.ProximityPrompt.Enabled = false
			wait(0.5)
			ancestorModel.Parent.FireDoor.CanCollide = false
			ancestorModel.Parent.FireDoor.Transparency = 1
		else
			print('Player does not have tool')
		end
	end
	
end 

return ObjectActions

Have you checked developer console?

I haven’t, I will try that now

Most likely the issue is here, the first condition works properly, but the other 3 will check if they’re truthy, Strings are always truthy so the condition will always pass regardless, you have to do ancestorModel.Name == for every condition.

If you plan to add more keys, it’s best to turn it into an array and do table.find instead

local keys = {"Key0", "Key1", "Key2", "Key3"}

-- Later on

if table.find(keys, ancestorModel.Name) then

Nothing appears in the Dev console

I have put this in the script but now the key picking up Prompt no longer works

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local ObjectActions = {}

function  ObjectActions.promptHoldEndedActions(promptObject, player)
	
	local ancestorModel = promptObject:FindFirstAncestorWhichIsA("Model")
	local constraint = ancestorModel:FindFirstChildWhichIsA("HingeConstraint", true)
	local keys = ["Key0", "Key1", "Key2", "Key3"]

	if table.find(keys, ancestorModel.Name) then
		local KeyTool = ReplicatedStorage.Key:Clone()
		wait(0.5)
		KeyTool.Parent = player.Backpack
	else
		print("No KEY")
	end

	
	if ancestorModel.Name == "PadLock" then
		
		local FindTool = player.Backpack:FindFirstChild("Key")
		--ancestorModel.PrimaryPart:SetNetworkOwner(nil)
		if FindTool then
			ancestorModel.MeshesPadLock.Transparency = 1
			
			ancestorModel.Parent.PadUnLock.MeshesLockUNLOCKED.Transparency = 0
			ancestorModel.MeshesPadLock.Attachment.ProximityPrompt.Enabled = false
			wait(0.5)
			ancestorModel.Parent.FireDoor.CanCollide = false
			ancestorModel.Parent.FireDoor.Transparency = 1
		else
			print('Player does not have tool')
		end
	end
	
end 

return ObjectActions

image

Oh wait my bad, use {} not []

1 Like

It worked thank you so much for helping me!

1 Like