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

How to make it when a key is down one function goes on and when the same key is up other one does?

Asked by
Echtic 128
6 years ago

This is a local script that is supposed to trigger one sever event if the key q is down and the other one if the key q is up, but all it does is activates the first event. Here is the script:

wait()
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Mouse = Player:GetMouse()
local event = workspace.Events.DemonAbilities["Dark Nebula"]
Enabled = true
ButtonDown = true

Mouse.KeyDown:Connect(function(key)
    if Enabled == false then return end


        Mouse.KeyUp:Connect(function(key)

        key = key:lower()
        if key == "q" then
            ButtonDown = false
        end

    end)

    key = key:lower()
    if key == "q" then

        Enabled = false

        if ButtonDown == true then

        event:FireServer()




    end
    end



        if ButtonDown == false then


        workspace.Events.DemonAbilities.darknebulaweldoff:FireServer()

    end




    wait(15)
    Enabled = true



end)

0
i thought local scripts cannot change the server things now tonyv537 95 — 6y
0
Keys being pressed is local stuff, server scripts can't mess with mouse crap unless a local script gives them the info. Knineteen19 307 — 6y

2 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Well first of all, I still don't have a clue what this means at all, but apparently KeyDown is "deprecated". It would be a good idea to try and figure out User Input Service instead, and get a hang of that, but here's an example of how I would do it with UIS.

local players = game:GetService("Players")
local player = players.LocalPlayer
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key,isTyping) -- The key parameter gets filled with whatever key is pressed, and isTyping, or the second parameter is whether or not the player currently has the chat open.

    if key.KeyCode == Enum.KeyCode.Q and isTyping == false then
        -- Script for when key is pressed here.
    end

end)

UIS.InputEnded:Connect(function(key,isTyping)

    if key.KeyCode == Enum.KeyCode.Q and isTyping == false then -- Don't mind all the weird stuff with enum and crap, and just put the key you want after KeyCode.
        -- When key is let go of script here.
    end

end)

Also, with how your script is, I think the reason it didn't work is because you put the KeyUp inside of the KeyDown event. You would need to separate the two so they can both function properly. The KeyUp can only be activated after a key is pressed down and the key comes back up, so you don't need to put it in the other event.

Hope I helped!

0
you are a god, thanks a lot Echtic 128 — 6y
0
No problem. Glad I helped! Knineteen19 307 — 6y
0
Good answer. i already upvote, but use 'not isTyping' yHasteeD 1819 — 6y
0
not isTyping? What do you mean? I don't fully understand the second parameter, but I just do that cause it works lol. Knineteen19 307 — 6y
View all comments (4 more)
0
'not isTyping' is the same as 'isTyping == false' but is more simple yHasteeD 1819 — 6y
0
oh, I see what you were saying, I thought you were saying not to use isTyping. I've messed around with just using not, but I've had bugs with it, and just decided to stick with ~= instead. Knineteen19 307 — 6y
0
no, instead of using 'isTyping == false' just use 'not isTyping' that's what I mean, i dont speak english xD yHasteeD 1819 — 6y
0
Yeah I got that, the first part anyway. I already knew that you could just do "if not isTyping then". Knineteen19 307 — 6y
Ad
Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

Using the KeyDown event from the Mouse is deprecated. See this to know how deprecation effects your code.

You should instead use UserInputService to handle player input.

Example code of what it would look like in your case:


--Get the service. local UIS = game:GetService("UserInputService") --Make a function and use the first parameter for the input object. function onPlayerInput(key, processed) if key.KeyCode == Enum.KeyCode.Q then print("Letter Q was pressed!") end end --Connect our function to input began. UIS.InputBegan:Connect(onPlayerInput)

Note that there is also ContextActionService that allows you to bind more keys and gamepads to functions.

Edit:

For checking if the Q key was released you would use the InputEnded event.

0
I don't get what you people mean by deprecated and all, but keydown works just fine if you use it right, the problem with their script is that they put the KeyUp event inside of KeyDown, and if you did that with UIS, it would still have the same problem. Knineteen19 307 — 6y
0
Also you forgot .KeyDown after key. Knineteen19 307 — 6y
0
Deprecation means that the api is no longer supported. It is also likely to be removed from the future which can break your games. See the link I posted in my answer for more on deprecation. xPolarium 1388 — 6y
0
Ah mk, that makes sense, thanks for the info! Knineteen19 307 — 6y

Answer this question