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:

01wait()
02local Players = game:GetService("Players")
03local Player = Players.LocalPlayer
04local Character = Player.Character
05local Mouse = Player:GetMouse()
06local event = workspace.Events.DemonAbilities["Dark Nebula"]
07Enabled = true
08ButtonDown = true
09 
10Mouse.KeyDown:Connect(function(key)
11    if Enabled == false then return end
12 
13 
14        Mouse.KeyUp:Connect(function(key)
15 
View all 55 lines...
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.

01local players = game:GetService("Players")
02local player = players.LocalPlayer
03local char = player.Character
04local humanoid = char:WaitForChild("Humanoid")
05 
06local UIS = game:GetService("UserInputService")
07 
08UIS.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.
09 
10    if key.KeyCode == Enum.KeyCode.Q and isTyping == false then
11        -- Script for when key is pressed here.
12    end
13 
14end)
15 
View all 22 lines...

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:

01--Get the service.
02local UIS = game:GetService("UserInputService")
03 
04--Make a function and use the first parameter for the input object.
05function onPlayerInput(key, processed)
06    if key.KeyCode == Enum.KeyCode.Q then
07        print("Letter Q was pressed!")
08    end
09end
10 
11--Connect our function to input began.
12UIS.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