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

Sprinting Script Help?

Asked by 10 years ago

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.

1player = game.Players.LocalPlayer
2 
3while 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
9end

As I stated before, I'm not quite sure if this is relatively right, but I think it's close. Any help is welcome.

2 answers

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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

1playerMouse = player:GetMouse()

When someone presses shift, we'll increase the WalkSpeed, and when they release shift we'll turn it back down:

01sprinting = false
02 
03playerMouse.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 
12playerMouse.KeyUp:connect(
13    function(key)
14        -- key is which key is pressed
15        if key:byte() == 48 or key:byte() == 47 then
View all 45 lines...
1
sorry, I forgot to include that part of the code. The reason I wanted to see if they were moving was because I was going to decrease their stamina. I should've explained myself a bit more thoroughly. FearMeIAmLag 1161 — 10y
0
Edited my answer to include that in a hopefully elegant solution BlueTaslem 18071 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
01player = game.Players.LocalPlayer
02Mouse = player:GetMouse()
03 
04function thing(key)
05    Key = key:lower()
06    if Key == "w" then
07        print("W was pressed")
08    end
09end)
10 
11Mouse.KeyDown:connect(thing)
1
That's only for pressing down a key, that's not what I am looking for FearMeIAmLag 1161 — 10y
0
Animations, Running? BosswalrusTheCoder 88 — 10y

Answer this question