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

How does one key-bind in a script?

So lets say you have a tool that is regularly used by clicking the mouse, but can also be used with another key (such as F). I'm guessing its a built in function, but which one?

1 answer

What I'd do is insert a LocalScript into StarterGui, that way it'll get cloned into every player. Keybinding wise, you'd do something like this-

wait() -- delay to load LocalPlayer
local plr = Game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(key)
	print(key .. " was pressed.")
	-- key binding code here
	if key == "r" then
		-- reload() etc.
	end
end)

mouse.Button1Down:connect(function()
	print("left click")
end)

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