FANDOM


In this tutorial, you will learn how to make a function so if you press a key (e.g Z) a function occurs.

Getting started

First, insert a LocalScript into StarterGui. It should look like this: StarterGui - LocalScript

Code

Now lets say we want the code to make it so when you press the Q key, a message pops up saying, "Hello!" To make that work, insert the following code:

player = script.Parent.Parent
mouse = player:GetMouse()
debounce = false
function message(key)
    if (key == "q") then
      if debounce == false then
       debounce = true
       local message = Instance.new("Message",player.PlayerGui)
        message.Name = "HiMessage"
         message.Text = "Hello!"
         wait(3)
          message:Destroy()
           debounce = false
     end
   end
end
 
mouse.KeyDown:connect(message)

Now what does all of that do! Well, basically, the debounce makes sure you cannot spam it. the player defines the player and the mouse gets the player's mouse. If you press the Q key, the message should pop up.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.