Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How does one key-bind in a script?

Asked by
Xduel 211 Moderation Voter
10 years ago

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

Log in to vote
0
Answered by
Ekkoh 635 Moderation Voter
10 years ago

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)
Ad

Answer this question