Search code examples
luaroblox

Show ProximityPrompt only with item held?


I would like to make a proximity prompt on an NPC but I only want it to show up when the player is holding a certain item. Does anyone know how?


Solution

  • Here's some sample code for you. This code listens for the opening of an item, and then creates a proximity prompt at the desired location of the NPC. Then, it listens for the unequipping and removes the prompt. Be sure to change the name of the NPC in line 1 to the name/location of yours.Put this as a serverscript as a child to the tool.

    local npc = Workspace:WaitForChild("NPC")
    local HumanoidRootPartPos = npc:WaitForChild("HumanoidRootPart").Position
    local tool = script.Parent
    
    tool.Equipped:Connect(function()
         local ProximityPrompt = Instance.new("ProximityPrompt")
         ProximityPrompt.Position = HumanoidRootPartPos
         ProximityPrompt.Parent = npc
         ProximityPrompt.Name  = "ToolInteractionPrompt"
         --configure the action text and prompt text here (i forgot the property names)
    end)
    
    tool.Unequipped:Connect(function()
         local prompt = npc:FindFirstChild("ToolInteractionPrompt")
         prompt:Destroy()
    end)