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

How do I use a function when a player presses a button?

Asked by
lucas4114 607 Moderation Voter
8 years ago

Title says it all, so how do I fire a function when the player presses a button on their keyboard, I couldn't find any events in the play about this... Idk how to do this...

1 answer

Log in to vote
0
Answered by
funyun 958 Moderation Voter
8 years ago

You would use a LocalScript in StarterPlayerScripts, under StarterPlayer.

You could then get the Mouse object from the LocalPlayer, and connect the KeyDown event to a function.

--LocalScript in StarterPlayerScripts in StarterPlayer folder

player = game.Players.LocalPlayer
mouse = Player:GetMouse()

function onKeyDown(key)
    print(key)
    if key == "e" then
        player.Character.Humanoid.Walkspeed = 30 --Or something like that
    end
end

mouse.KeyDown:connect(onKeyDown)

An alternative is to use the UserInputService:

--Same thing as last time, a LocalScript in StarterPlayerScripts in StarterPlayer

player = game.Players.LocalPlayer
inputservice = game:GetService("UserInputService")

function onInputBegan(input)
    if input == Enum.KeyCode.E then
        player.Character.Humanoid.Walkspeed = 30
    end
end

inputservice.InputBegan:connect(onInputBegan)
1
Lol, I didn't know the mouse was for the keyboard too... lucas4114 607 — 8y
Ad

Answer this question