ContextActionService doesn't want to work in a tool

I’ve been working with ContextActionService using a tool. When the tool is equipped and the player presses “R” the output should print “Reloading!” like the documentation says… But it doesn’t for some reason! It’s the same script as it is in the documentation. I’ve tried all different places to put the LocalScript (in the tool itself, in the StarterPlayerScripts). But it doesn’t work. It simply doesn’t. The ContextActionService :BindAction function only worked for me when I was putting it alone not inside an equip function or something. But when when I do the opposite my ContextActionService vanishes…

Here’s the script:

local ContextActionService = game:GetService("ContextActionService")

local ACTION_RELOAD = "Reload"

local tool = game:GetService("StarterPack").Tool

local function handleAction(actionName, inputState, _inputObject)
	if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
		print("Reloading!")
	end
	print("reached")
end

tool.Equipped:Connect(function()
	ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)

tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction(ACTION_RELOAD)
end)

What could be the issue?

The example does work. What’s not mentioned is that tool requires a part named Handle unless tool.RequiresHandle is unticked.

Second problem:

StarterPack is a container. Once character spawns, all those tools are cloned into player.Backpack.

Code
local ContextActionService = game:GetService("ContextActionService")

local ACTION_RELOAD = "Reload"

local player = game:GetService("Players").LocalPlayer
local tool = player.Backpack:WaitForChild("Tool")

local function handleAction(actionName, inputState, _inputObject)
	if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
		print("Reloading!")
	end
end

tool.Equipped:Connect(function()
	ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)

tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction(ACTION_RELOAD)
end)
1 Like

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