I am not quite sure how I would create a sprinting script. I read the wiki a few times but it doesn't quite explain how I would do this. I somewhat know what to do but I am not quite sure if I am doing this right or if this is the most efficient way to do this. I have what I think is close to correct below, but any help would be appreciated.
1 | player = game.Players.LocalPlayer |
2 |
3 | while wait() do |
4 | if (key:byte() = = 48 or key:byte() = = 47 ) and (key = = "w" or key = = "a" or key = = "s" or key = = "d" or key:byte() = = 17 or key:byte() = = 18 or key:byte() = = 19 or key:byte() = = 20 ) then --checks to see if they have either of the shift keys and any directional key held down |
5 | player.Character.Humanoid.Walkspeed = 18 |
6 | else |
7 | player.Character.Humanoid.Walkspeed = 16 |
8 | end |
9 | end |
As I stated before, I'm not quite sure if this is relatively right, but I think it's close. Any help is welcome.
Don't worry about the directional keys. If they aren't walking, they won't walk, so there's no real problem of caring what keys they're pressing.
Instead of listening when the keys are pressed, we'll use the humanoid Running event to track it.
key
isn't defined in this script. You would have to use KeyUp
or KeyDown
events on a Mouse
object to find keys being pressed.
In a LocalScript, you can get a mouse handle using
1 | playerMouse = player:GetMouse() |
When someone presses shift, we'll increase the WalkSpeed, and when they release shift we'll turn it back down:
01 | sprinting = false |
02 |
03 | playerMouse.KeyDown:connect( |
04 | function (key) |
05 | -- key is which key is pressed |
06 | if key:byte() = = 48 or key:byte() = = 47 then |
07 | sprinting = true |
08 | end |
09 | end |
10 | ) |
11 |
12 | playerMouse.KeyUp:connect( |
13 | function (key) |
14 | -- key is which key is pressed |
15 | if key:byte() = = 48 or key:byte() = = 47 then |
01 | player = game.Players.LocalPlayer |
02 | Mouse = player:GetMouse() |
03 |
04 | function thing(key) |
05 | Key = key:lower() |
06 | if Key = = "w" then |
07 | print ( "W was pressed" ) |
08 | end |
09 | end ) |
10 |
11 | Mouse.KeyDown:connect(thing) |