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

How do I add keybind based abilities to a tool?

So I tried to make a tool that when you hold it you can use these abilities that are linked to keys. I can do the keybind part but how do I make it so that it only works whilst the tool is being held. If anyone needs it, my code I currently have is below:

local equipped = false local tool = script.Parent

local re = game.ReplicatedStorage.EarthMagic:WaitForChild("EarthWallRE")

local mouse = game.Players.LocalPlayer:GetMouse()

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(inp, processed) tool.Equipped:Connect(function() if equipped == true and inp.KeyCode == Enum.KeyCode.R then

		re:FireServer(mouse.Hit)
	end
end)	

end)

uis.InputBegan:Connect(function(inp, processed) tool.Unequipped:Connect(function() if equipped == false and inp.KeyCode == Enum.KeyCode.R then

	end
end)

end)

I also might need to mention I am not used to coding in this language.

1 answer

Here I've corrected the mistakes you've made in your script while giving an explanation. Also please put the Lua code block properly, almost half of your code is not in it and it's really hard to see plus I can't easily copy the code.

local Tool = script.Parent -- Tool that will be used give you abilities.

local UserInputService = game:GetService("UserInputService") -- User input service that detects any player input. (Key presses, mouse key presses etc.)

local Equipped = false

Tool.Equipped:Connect(function() -- When the tool is equipped the function provided will run.
	Equipped = true
	UserInputService.InputBegan:Connect(function(InputObject, gameProcessed)
		if (not gameProcessed) then -- This will check if the player is typing or not, if not then the if statement will run.
			if InputObject.KeyCode == Enum.KeyCode.R and Equipped then -- If the player presser R then the if statement will run.
				
				print('u dumb') -- Put the code you want in here.
				
			end
		end
	end)
end)

Tool.Unequipped:Connect(function()
	Equipped = false -- Setting the equpped variable to false so that it won't print 'u dumb' anymore.
end)

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