generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
We use cookies to offer you a better experience. By using Roblox.com, you are agreeing to our Privacy and Cookie Policy.
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Aware of KeyDown and KeyUp events...

Previous Thread :: Next Thread 
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
15 Mar 2016 03:44 PM
But, is there some sort of way I could have a key being held (not just pressed, or released) to be fired?

For example, I'm trying to increase a player's WalkSpeed as they keep moving forward, and have it decrease when they are not pushing the button. My idea involved the traditional KeyDown thing, but it only worked when you were pushing it down, making button-mashing the standard to increase speed. I've had my head wrapped around this for a while, and I've been having trouble figuring out a way to solve this problem.

If you can help me, I'd be glad to take any sort of advice.
Report Abuse
superpants2020 is not online. superpants2020
Joined: 10 Feb 2013
Total Posts: 804
15 Mar 2016 04:02 PM
All I know is that it's possible because my friend knows. If you want help message him his IGN is "oldboy"
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
15 Mar 2016 04:18 PM
Try this, haven't tested anything yet though.

local down = false

game:GetService("UserInputService").InputBegan:connect(function(inp,gpe)
if not gpe then
if inp.UserInputType == Enum.UserInputType.KeyBoard then
if inp.KeyCode == Enum.KeyCode.Up then
down = true
end
end
end
end)

game:GetService("UserInputService").InputEnded:connect(function(inp,gpe)
if not gpe then
if inp.UserInputType == Enum.UserInputType.KeyBoard then
if inp.KeyCode == Enum.KeyCode.Up then
down = false
end
end
end
end)

function Held()
if down then
Humanoid.WalkSpeed = 30
else
repeat wait() until down
end
Held()
end

Held()
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
15 Mar 2016 04:20 PM
Forgot to return the speed to 16..

local down = false

game:GetService("UserInputService").InputBegan:connect(function(inp,gpe)
if not gpe then
if inp.UserInputType == Enum.UserInputType.KeyBoard then
if inp.KeyCode == Enum.KeyCode.Up then
down = true
end
end
end
end)

game:GetService("UserInputService").InputEnded:connect(function(inp,gpe)
if not gpe then
if inp.UserInputType == Enum.UserInputType.KeyBoard then
if inp.KeyCode == Enum.KeyCode.Up then
down = false
end
end
end
end)

function Held()
if down then
Humanoid.WalkSpeed = 30
else
Humanoid.WalkSpeed = 16
repeat wait() until down
end
Held()
end

Held()
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
15 Mar 2016 05:03 PM
KLGA, the script is nice and all, but the problem is, it doesn't work! Using it in Studio gives an error.

"KeyBoard is not a valid EnumItem"
Report Abuse
iFallenAtlantis is not online. iFallenAtlantis
Joined: 29 Feb 2012
Total Posts: 240
15 Mar 2016 05:27 PM
Just set up two functions that run when the key is down, and when the key is up. When the key is pressed down, set the player's walkspeed to the run speed, and when the key is let up, set the players walkspeed back to 16.
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
15 Mar 2016 05:46 PM
Something like this, then? I'm not the most skilled scripter, but this is what I've come up with.

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

mouse.KeyDown:connect(function(key)
if key == "w" then
player.Humanoid.WalkSpeed = 32
end
end)

mouse.KeyUp:connect(function(key)
if key == "w" then
player.Humanoid.WalkSpeed = 32
end
end)
Report Abuse
KLGA is not online. KLGA
Joined: 19 Apr 2014
Total Posts: 2571
15 Mar 2016 05:48 PM
Oops, it should be "Keyboard" not "KeyBoard"
Report Abuse
WolfgangVonPrinz is not online. WolfgangVonPrinz
Joined: 24 Oct 2013
Total Posts: 4656
15 Mar 2016 05:56 PM
Unfortunately, you're going about this all wrong.

http://wiki.roblox.com/index.php?title=API:Class/Humanoid/Running

You're WELCOME :O
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
15 Mar 2016 06:15 PM
Oh my god, I never knew this part of Humanoid existed. THANKS, bro. I'm very GRATEFUL.

Right, then, I tried utilizing this new concept in this script. It's supposed to make the player go faster, when running. Check it out:

player.Humanoid.Running:connect(function(Speed)
if Speed >= 16 and Speed ~= 32 then
player.Humanoid.WalkSpeed = player.Humanoid.WalkSpeed + 0.1
end
end)

Just to tread ground waters, I wanted to see if at least moving would increase speed, but it only does so until it reaches 16.400001525879 (the farthest a script would print the walkspeed at, and it ain't even adding 0.1 right) So, I'm wondering, what could I do to make it so? Y'know, make my hopes come true.
Report Abuse
iFallenAtlantis is not online. iFallenAtlantis
Joined: 29 Feb 2012
Total Posts: 240
15 Mar 2016 07:05 PM
The 'player.Humanoid.Running:connect(function(speed) end)' function is not a loop, so you would need to add a loop.

local startSpeed = 16
local endSpeed = 32
local speedIncrement = 0.1

for i = startSpeed, endSpeed do
player.Humanoid.Walkspeed = i
i = i + speedIncrement
wait(0.05)
end

Put the loop in your function, and also make a variable that stores whether or not the humanoid is running. The wiki says that the function will fire twice, once when the humanoid starts walking, and when it stops. So change your variable accordingly, inside of an if statement.

Also, this would only work for speeding up. You would need to modify the loop for slowing down.
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
15 Mar 2016 07:22 PM
Thanks for helping, but the script doesn't work as well as it should, at least if I have put it down right. Probably not, though.

player = game.Players.LocalPlayer.Character

player.Humanoid.Running:connect(function()
local Running = true
local StartSpeed = 16
local EndSpeed = 32
local SpeedIncrement = 0.1
if Running then
for i = StartSpeed, EndSpeed do
player.Humanoid.WalkSpeed = i
i = i + SpeedIncrement
wait(0.05)
end
local Running = false
else
end
end)


The output (which prints the WalkSpeed of the humanoid) has inconsistent results.
Here's the results of just spawning, and then a few moments of constant running:

16
20
19
21
17
16
16
27
22
16
32

What the hell's going on, here? Those are some of the most inconsistent outcomes I've ever seen!
Report Abuse
iFallenAtlantis is not online. iFallenAtlantis
Joined: 29 Feb 2012
Total Posts: 240
15 Mar 2016 07:58 PM
The if statement should look like this:

if Running == true then
-- Code
end

Currently, you are just checking if the variable exists. What you want, is to check if the variable is true. Also, remember, the function only fires when the Humanoid starts walking and when it stops.
Report Abuse
cgjnm is not online. cgjnm
Joined: 22 Dec 2011
Total Posts: 2347
15 Mar 2016 08:05 PM
run = false
UIS.InputBegan:connect(function(iO,gPE)
if not gPE and iO.KeyCode == Enum.KeyCode.LeftShift then
run = true
end
end)
UIS.InputBegan:connect(function(iO,gPE)
if iO.KeyCode == Enum.KeyCode.LeftShift then
run = false (I'm including gPE because some people will hold shift, then type and they'll keep running, even without button)
end
end
while wait() do
if run==true then
--script
end
end
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
16 Mar 2016 09:42 PM
Bumping the thread. I've worked on a solution (it works, somewhat!)

player = game.Players.LocalPlayer.Character

player.Humanoid.Running:connect(function()
local Running = true
local Debounce = false

if not Debounce then
if Running and player.Humanoid.WalkSpeed <= 32 then
player.Humanoid.WalkSpeed = player.Humanoid.WalkSpeed + 0.05
local Running = true
elseif not Running then
player.Humanoid.WalkSpeed = 16
local Running = false
end
wait(2.5)
end

Running = false
Debounce = true

end)


...Though, it's still now what I desire. Having the WalkSpeed increase DURING the running, seems to be the problem. Seems like it's either "on/off" with scripts, as I view it. No "during!" That's annoying. There's a solution out there, I just need to find it.

To cgjnm, your script might work, but what is "UIS?" Does it stand for something, e.g, "User Input System?" I'm certainly no big-shot developer, as I think getting this to work on a computer is first priority, rather than another device.
Report Abuse
iFallenAtlantis is not online. iFallenAtlantis
Joined: 29 Feb 2012
Total Posts: 240
17 Mar 2016 01:39 AM
player = game.Players.LocalPlayer.Character
local Running = false


player.Humanoid.Running:connect(function()
if Running == false then
local Running = true
for s = 16, 32 do
player.Humanoid.WalkSpeed = player.Humanoid.WalkSpeed + 0.25
wait(0.05)
end
elseif Running == true then
local Running = false
for s = 32, 16 do
player.Humanoid.WalkSpeed = player.Humanoid.WalkSpeed - 0.25
wait(0.05)
end
end
end)


Keep in mind that this function will fire EVERY time you start walking around with your character. I would advise that you bind this function with the KeyDown and KeyUp events.



--I can put my waits wherever I want.
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
17 Mar 2016 09:22 PM
Alright, well, I tried implementing the code, altering it, praying for success. Not sure if hitting speeds up to 2915.5 is most optimal, but it is pretty fun launching myself off the baseplate. I haven't utilized KeyUp yet, because first thing's first I need to make sure KeyDown is working. Check it out for yourself, see what I screwed up:


player = game.Players.LocalPlayer.Character
mouse = game.Players.LocalPlayer:GetMouse()
local Running = false

player.Humanoid.Running:connect(function()
mouse.KeyDown:connect(function(key)
if Running == false and key == "w" and player.Humanoid.WalkSpeed ~= 32 then
local Running = true
for s = 16, 32 do
player.Humanoid.WalkSpeed = player.Humanoid.WalkSpeed + 0.25
wait(0.5)
end
elseif Running == true and key == "w" and player.Humanoid.WalkSpeed ~= 16 then
local Running = false
for s = 32, 16 do
player.Humanoid.WalkSpeed = player.Humanoid.WalkSpeed - 0.25
wait(0.05)
end
end
end)
end)
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
18 Mar 2016 06:47 PM
Bumpin'
Report Abuse
SenseiWarrior is online. SenseiWarrior
Joined: 09 Apr 2011
Total Posts: 12140
18 Mar 2016 07:28 PM
local player = game.Players.LocalPlayer repeat wait() until player
local mouse = player:GetMouse()

local wDown = false

local function increaseSpeed()
while wDown do
player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed + 1
wait(1)
end
end

mouse.KeyDown:connect(function(key)
wDown = true
increaseSpeed()
end)

mouse.KeyUp:connect(function(key)
wDown = false
end)


Instance.new("BodyThrust",SenseiWarrior).position = CFrame.new(SenseiWarrior,YourGirlsDMs)
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
18 Mar 2016 07:36 PM
@Northadox


And nice and all, but the problem is, it doesn't work is KLGA, the script. How long have you been using it in Studio gives an error ? What is a valid EnumItem"?
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
18 Mar 2016 07:58 PM
@superpants2020


Under what circumstances? If I had help message me my IGN is "oldboy" what would I do with it?
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
18 Mar 2016 08:15 PM
Good work, SamuraiWarrior. You did it, the absolute madman. It's the best code so far for this problem.

Problem is, I made some modifications, and now it's pooped the bed. It works relatively nice, yes, but jumping just absolutely decimates your running speed. That ain't good. When people want to go fast, they want to jump fast while going fast.

Here's the code. I made some changes to default speed, and the max speed. 16 -> 20 and 32 -> 40, respectively.

local player = game.Players.LocalPlayer
player.Character.Humanoid.WalkSpeed = 20 repeat wait() until player
local mouse = game.Players.LocalPlayer:GetMouse()
local wDown = false

local function increaseSpeed()
while wDown and player.Character.Humanoid.WalkSpeed <= 40 do
player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed + 1
wait(0.75)
if player.Character.Humanoid.WalkSpeed > 38 then
player.Character.Humanoid.WalkSpeed = 40
end
end
end

local function decreaseSpeed()
while not wDown and player.Character.Humanoid.WalkSpeed >= 16 do
player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 2
wait(0.75)
if player.Character.Humanoid.WalkSpeed < 22 then
player.Character.Humanoid.WalkSpeed = 20
end
end
end

mouse.KeyDown:connect(function(key)
wDown = true
increaseSpeed()
end)


mouse.KeyUp:connect(function(key)
wDown = false
decreaseSpeed()
end)


To rtifg, I can't really understand you. Look at the other people's programming posted in the thread, see if that cures what ails ya.
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
18 Mar 2016 08:31 PM
@Northadox


Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
18 Mar 2016 08:32 PM
what
Report Abuse
Northadox is not online. Northadox
Joined: 18 Jan 2014
Total Posts: 48
19 Mar 2016 10:22 PM
Yet again, bumping. I'm never going to make it, am I?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image