How do i make a code that detects if a player holds a weapon?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a script to know when a player presses E when holding a tool at the same time to do an action with the tool

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do that

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried making a bool that turns on when the player equips a tool and I don’t know if its useless or not

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

LOCAL SCRIPT :

local BloxFlakesWorkspace = game.Workspace:WaitForChild(“BloxFlakes”)
local BloxFlakesProx = BloxFlakesWorkspace:FindFirstChild(“ProximityPrompt”)
local BloxFlakesReplicated = game.ReplicatedStorage:WaitForChild(“BloxFlakes”)
local PlayerPressedBackspace = game.ReplicatedStorage:WaitForChild(“PlayerPressedBackspace”)
local ContextActionService = game:GetService(“ContextActionService”)
local PlayerCanDrop = game.ReplicatedStorage.PlayerCanDrop
local Players = game:GetService(“Players”)

local ProxActivated = BloxFlakesProx.Triggered:Connect(function(player)
BloxFlakesWorkspace:Destroy()
local PlayerCanDropFunction = PlayerCanDrop:Clone()
PlayerCanDropFunction.Parent = player.Character
local CopiedFlakes = BloxFlakesReplicated:Clone()
CopiedFlakes.CanBeDropped = false
CopiedFlakes.Parent = player:FindFirstChild(“Backpack”)
CopiedFlakes.Equipped:Connect(function(mouse)
PlayerCanDropFunction.Value = true
CopiedFlakes.Unequipped:Connect(function(mouse)
PlayerCanDropFunction.Value = false

	end)
end) 

end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Just check if the tool you want is in the character. FindFirstChildOfClass(“Tool”) and check the tool’s name if a non-nil instance is returned. Check if it’s the tool you want then commit the action. Use ContextActionService or UserInputService to detect input.

1 Like
local Tool = script.Parent -- This Local Script is Inside the Tool
local EquippedTool = false  -- A Bool Type of Variable

Tool.Equipped:Connect(function() -- When Player Equips
   EquippedTool = true -- Bool Sets to True
end)

Tool.Unequipped:Connect(function() -- When Player Unequipped 
   EquippedTool = false -- Bool Sets to False
end)

-- // Now We Want to Check When the Player Presses "E"

local UserInputService = game:GetService("UserInputService")  -- We Define UserInputService

UserInputService.InputBegan:Connect(function(Input,Engine) -- When An Input Begins
   if not Engine then -- This Line Checks if the Player is Not Typing in chat / etc
       if Input.KeyCode == Enum.KeyCode.E then -- Is Player Pressed 'E'
          if EquippedTool == true then -- This Checks if the Tool Is Equipped
             print("Player Pressed E While Tool Equipped")
             -- What you want to do 
          end
       end
   end
end)

This should be a LocalScript Inside the Tool.

You can Use UserInputService to Check when the Player Hits the Key “E” And Check if
The Tool is Currently Equipped :happy2:

:wave:t2:Good luck friend, I see there are some other suggestions but this should detail everything you need for using ContextActionService to achieve your goal.

ContextActionService can be used to bind and unbind a function when the tool is equipped and unequipped.

How to use ContextActionService:
https://developer.roblox.com/en-us/api-reference/class/ContextActionService

How to detect if a tool is Equipped:
https://developer.roblox.com/en-us/api-reference/event/Tool/Equipped

How to detect if a tool is UnEquipped:
https://developer.roblox.com/en-us/api-reference/event/Tool/Unequipped

Code that should work, use it study it, do both:
This is a local script that goes in the tool.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local ContextAction = game:GetService("ContextActionService")
local Tool = script.Parent
-------------------------------------
local USE_E_TOOL= "UseE"
local Key = Enum.KeyCode.E
 
function YourAction (Player,Character)
	--Put the action you want done when E is pressed here.
end
-------------------------------------
--Do not edit below this line until you understand what you are doing.
local function DoAction(action, inputState, inputObject)
	if action == USE_E_TOOL and inputState == Enum.UserInputState.Begin and Player.Character then
		if Player.Character:FindFirstChild(Tool.Name) then
			YourAction(Player,Character)
		end
	end
end
 
Tool.Equipped:Connect(function ()
	ContextAction:BindAction(USE_E_TOOL, DoAction, true, Key)
end)
Tool.Unequipped:Connect(function ()
	ContextAction:UnbindAction(USE_E_TOOL)
end)