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

How to check if a player pressed a key?

Asked by 3 years ago

So, I wanna check when a player pressed a key to make a sort of "attack."

This is the current code which isn't working:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local UserInputService = game:GetService("UserInputService")

        local key = Enum.KeyCode.R
        local pressedkey = UserInputService:GetKeysPressed()

            local function forceField()
            for _, keypog in pairs(pressedkey) do
                if (pressedkey.KeyCode == key) then
                    local part = Instance.new("Part",workspace)
                    part.Shape = Enum.PartType.Ball
                    part.Color3 = Color3.new(159, 243, 233)
                    part.Position = char.Torso.Position
                    part.CanCollide = true
                    part.Anchored = true
                    wait(1)
                    part.Size = Vector3.new(4.623, 4.623, 4.623)
                    part.Transperency = 0.1
                    wait(1)
                    part.Size = Vector3.new(5.305, 5.305, 5.305)
                    part.Transperency = 0.2
                    wait(1)
                    part.Size = Vector3.new(6.304, 6.304, 6.304)
                    part.Transperency = 0.3
                    wait(1)
                    part.Size = Vector3.new(7.569, 7.569, 7.569)
                    part.Transperency = 0.4
                    wait(1)
                    part.Size = Vector3.new(8.88, 8.88, 8.88)
                    part.Transperency = 0.5
                    wait(1)
                    part.Size = Vector3.new(10.247, 10.247, 10.247)
                    part.Transperency = 0.6
                    wait(1)
                    part.Size = Vector3.new(11.69, 11.69, 11.69)
                    part.Transperency = 0.7
                    wait(1)
                    part.Size = Vector3.new(13.387, 13.387, 13.387)
                    part.Transperency = 0.8
                    wait(1)
                    part.Size = Vector3.new(15.039, 15.039, 15.039)
                    part.Transperency = 0.9
                    wait(1)
                    part:Destroy()
                end
            end
        end
    end)
end)
0
Also I just noticed your part creating and modification codes wouldn't work. Example: 'Color3' is not a property of part, it should be 'BrickColor3' Feelings_La 399 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

As DetectiveRaie said, you should use :InputBegan() to simply detect user input. Also remember UIS only works in 'LocalScript' not 'ServerScript' ( RECOMMENDED ONE )

Basic Code:

local UserInputService = game:GetService("UserInputService")
local requiredKey = Enum.KeyCode.E -- key needed to run the function

UserInputService.InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == requiredKey then
        -- Use remote event/function to comunicate with the server
        wait(10) -- cooldown
    end
end)

Next, you will need to use remote function/event since it's client sided code if you want your attacks or stuff to be seen by all players/clients ( server )

Another way of doing it by using :GetPressedKeys() ( LESS RECOMMENDED )

Your code is actually fine and correct but there is something that makes it not work.

  1. You probably used ServerScript instead of LocalScript
  2. In line 8 of your code there's a local function named forceField and your attacks code is inside it.
  3. You forgot to call the function above
  4. If you want it to be seen by the server, you must use remote event.

Before getting into the code. There' 2 ways I usually use to detect input with :GetPressedKeys(), which are UserInputService.JumpRequest and Humanoid.Running or simply just use white loop

Basic Code: ( White Loop Usage )

local UserInputService = game:GetService("UserInputService")
local requiredKey = Enum.KeyCode.R

local function getPressedKey()
    local pressedKey = UserInputService:GetKeysPressed()

    for _, key in pairs(pressedKey) do
        if key.KeyCode == requiredKey then
            print("Pressed: " , key.KeyCode)
            -- Don't forget to use remote event/function
            wait(10) -- cooldown
        end
    end
end

while wait(1) do
    getPressedKey()
end

REMINDER: ALL stuff you want to do needs to be done on the server by using remotes

FYI: You can use remote function instead of remote event to make a built in cooldown for this

0
Also I forgot, you'll need to use debounce to make the cooldown lol Feelings_La 399 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Hello, from what I can see you have the code right, but instead of trying to get when a key is pressed I would check when any input from the UserInputService is detected using :InputBegan().

I believe this is what you are looking for, take a look at lines 5-8, and let me know if I am wrong. Have a great day, Raie.

0
Doesn't work, changed from GetPressedKeys() to InputBegan(). Still doesn't. InterDwarf 14 — 3y

Answer this question