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

Local script runs even when the tool is not equipped?

A LocalScript in a tool I made has functions that run onKeyPress. The functions and everything run fine, but even when the tool is unequipped the function still runs onKeyPress. How can I fix that?

1 answer

✓ Accepted answer

Make a variable to track the equipping / unequipping of the tool.

Then you can check the variable prior to your keypresses.

Example;

local eq = false;
local tool = script.Parent;

--Variable tracking
tool.Equipped:connect(function()
	eq = true;
	tool.Unequipped:connect(function()
		eq = false;
	end)
end)

--Keypresses
game:GetService("UserInputService").InputBegan:connect(function(key,process)
	if not process then --Prevent firing while chatting
		if eq then	--This is your check.
			
			if key.KeyCode == Enum.KeyCode.M then
				print("M pressed");
			end

		end
	end
end)

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