Help with making a proximity prompt only activate when holding a tool

I’m struggling to find a way to make it so a proximity prompt will only follow through with a script when the player is holding a tool. Actually my question right now is quite trivial.
The issue is more of a question: why does it always only print outcome 2?

Here is the code:

local SpecificTools = {
Katana
}

script.Parent.Attack.Triggered:Connect(function(player)
local character = player.Character

if character:FindFirstChildOfClass(“Tool”) then – Checks if a player has a tool equipped.
if table.find(SpecificTools, character:FindFirstChildOfClass(“Tool”).Name) then
print(“outcome 1”)
else
print(“outcome 2”)
end
else
print(“Not holding a weapon”)
end
end)

When removing the else print(“outcome2”), it doesn’t print anything when holding the tool. Would the best course of action be to just keep the script as is, and make outcome 2 the outcome which occurs? The idea was to have the player kill an NPC when the player is holding a weapon and activates a proximity prompt in the NPC. In my vision, outcome 1 would be the outcome in which the player kills the NPC and outcome 2 would just be an else statement in case the player was not holding a weapon and the NPC would not be killed. But it seems that outcome 1 never happens, and there is another else which does work: print(“Not holding a weapon”). So would I just keep the script as is, and instead of having the NPC be killed in outcome 1, it would be killed in outcome 2?

local Tool = script.Parent
local ProximityPrompt = WhereverThisIs:WaitForChild("ProximityPrompt")

local function Equip()
    ProximityPrompt.Enabled = true
end

local function Unequip()
    ProximityPrompt.Enabled = false
end

Tool.Equipped:Connect(Equip)
Tool.Unequipped:Connect(Unequip)

I think you need something like this (added prints to debug):

local SpecificTools = {
    "Katana"
}

script.Parent.Attack.Triggered:Connect(function(player)
    print("Triggered event received.")
    local character = player.Character

    if character then
        print("Character found.")
        local tool = character:FindFirstChildOfClass("Tool")
        if tool then -- Checks if a player has a tool equipped.
            print("Tool found: " .. tool.Name)
            if table.find(SpecificTools, tool.Name) then
                print("Outcome 1: Player has the specified tool.")
            else
                print("Outcome 2: Player does not have the specified tool.")
            end
        else
            print("No tool found.")
            print("Not holding a weapon")
        end
    else
        print("No character found.")
    end
end)
1 Like

This one should work so mark the one I’m replying to

Well both work, and I can’t really mark both of them. I will use both of them however. I will just mark the one that is most similar to my original idea.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.