Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

i am trying to make a key function when the tool is equipped. the scripts seems to not run?

so i have a tool in the starterpack, and within this tool there is a localscript.

so im trying to make it so that you can ONLY activate this key function when the tool is equipped, but the script seems to not run. if there is a bug, what could it be?

local UserInputService = game:GetService("UserInputService")
local tool = script.Parent

local function onInputBegan(input,gameProcessed)
	
tool.Equipped:Connect(function()
	if input.KeyCode == Enum.KeyCode.F then
		
		print("it worked.")
				
	end
	
end)
end
UserInputService.InputBegan:connect(onInputBegan)

1 answer

Just have a variable that tells whether the tool is equipped that changes whenever it's equipped and unequipped. Here's an example:

local UserInputService = game:GetService("UserInputService")
local tool = script.Parent

local equipped = false

tool.Equipped:Connect(function()
	equipped = true
end

tool.Unequipped:Connect(function()
	equipped = false
end

UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.F and equipped == true then
		--Do stuff
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.