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

Why is my Shift to Run Script not working? When I press shift, it doesn't do anything!

Asked by 7 years ago

Hello, fellow helpers! I have a problem... I made a Shift to Run script but it doesn't seem to be working even though it looks fine to me. I've tried to put it in a StarterGUI too, but it doesn't work either.

Here's the LocalScript:

01local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:wait()
02local char = plr.Character or plr.Character.CharacterAdded:wait()
03local humanoid = char.Humanoid
04local RunR6 = script.Parent["Run (R6)"]
05local RunR15 = script.Parent["Run (R15)"]
06local PlayRunR6 = humanoid:LoadCharacter(RunR6)
07local PlayRunR15 = humanoid:LoadCharacter(RunR15)
08local UserInputService = game:GetService('UserInputService')
09 
10UserInputService.InputBegan:connect(function(input)
11    if input.KeyCode == Enum.KeyCode.LeftShift then
12        if Enum.HumanoidRigType.R6 then
13            humanoid.WalkSpeed = 24
14            PlayRunR6:Play()
15        elseif Enum.HumanoidRigType.R15 then
16            humanoid.WalkSpeed = 24
17            PlayRunR15:Play()
18        end
19    end
20end)

This checks if you have pressed the Shift key on the left side of keyboard. If it is pressed, it triggers another if statement about detecting what RigType you're avatar is to determine what animation needs to be played (I made an R6 and R15 version of a running animation). When it sees this, it changes the humanoid's (player's) walkspeed to 24 and it plays the animation.

Hierarchy (View)

1
You don't need the game.Players.PlayerAdded:wait(), LocalPlayer is always there when a localScript starts to run because LocalScripts are always within the player. theCJarmy7 1293 — 7y

3 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
01local plr = game.Players.LocalPlayer
02local char = plr.Character or plr.CharacterAdded:Wait()
03local humanoid = char:WaitForChild("Humanoid")
04local RunR6 = script.Parent["Run (R6)"]
05local RunR15 = script.Parent["Run (R15)"]
06local PlayRunR6 = humanoid:LoadAnimation(RunR6) -- Notice
07local PlayRunR15 = humanoid:LoadAnimation(RunR15) -- Notice
08local UserInputService = game:GetService('UserInputService')
09 
10UserInputService.InputBegan:Connect(function(input)
11    if input.KeyCode == Enum.KeyCode.LeftShift then
12        if humanoid.RigType == Enum.HumanoidRigType.R6 then
13            humanoid.WalkSpeed = 24
14            PlayRunR6:Play()
15        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
16            humanoid.WalkSpeed = 24
17            PlayRunR15:Play()
18        end
19    end
20end)

In the lines where I wrote the last two "Notice" comments I made in your script, you forgot to write "humanoid.RigType ==" before your "Enum.HumanoidRigType".

Also on the first "Notice", you write the .CharacterAdded event next to your player itself (plr in your script), not next to its Character.

Finally I added a WaitForChild() in the next "Notice" line, its a good practice to use the WaitForChild() function on instances you are not sure have loaded yet (the player's humanoid in this case) since your script will often break otherwise.

Hope this answers your question, best of lucks!

EDIT:

Didn't see that on line 6 and 7 you used the :LoadCharacter() function when you should use :LoadAnimation(). Hopefully the script will work now.

1
:wait() and :connect() were deprecated. Use :Wait() and :Connect() instead Gey4Jesus69 2705 — 7y
0
True that, however :wait() and :connect() still work fine. Good catch tho! Le_Teapots 913 — 7y
0
It doesn't seem to work both in Studio and in game. It didn't work when you sent that script. SilverCreeper58 23 — 7y
0
Hmmm, could you check for any error calls given in the Output? For diagnosing purpose. Le_Teapots 913 — 7y
View all comments (6 more)
0
There aren't any errors. SilverCreeper58 23 — 7y
0
Alright, fixed the script once again, hopefully its good to go now. Le_Teapots 913 — 7y
0
Still broken SilverCreeper58 23 — 7y
0
I'm sure :wait() and :connect() serve the same purpose as :Wait() and :Connect(). Unless I'm missing something.. MarceloFBentley 13 — 7y
0
Nvm, its working! :D SilverCreeper58 23 — 7y
0
Happy to hear that! :) Le_Teapots 913 — 7y
Ad
Log in to vote
0
Answered by
Kblow1 53
7 years ago

You could also just use this... Put this in a LocalScript inside of StarterCharacterScripts

01-- Locals
02local Mouse = game.Players.LocalPlayer:GetMouse()
03local char = game.Players.LocalPlayer.Character
04local sb = string.byte
05 
06-- Main
07Mouse.KeyDown:Connect(function(keybyte)
08    Key = string.lower(keybyte)
09        if sb(keybyte) == 48 then
10            char.Humanoid.WalkSpeed = 21
11        end
12    Mouse.KeyUp:Connect(function(keybyte)
13        if sb(keybyte) == 48 then
14            char.Humanoid.WalkSpeed = 16
15        end
16    end)
17end)
0
Isn't supposed to be left shift key? ILikeTofuuJoe 1 — 7y
0
The .KeyDown function is deprecated, wouldn't suggest using it. Le_Teapots 913 — 7y
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Try this, if you still have a problem occurring, I'll properly debug it.

Make sure this is in a LocalScript in StarterPack or StarterGui.

01local plr = game.Players.LocalPlayer
02local char = plr.Character
03local humanoid = char:WaitForChild("Humanoid")
04local RunR6 = script.Parent["Run (R6)"]
05local RunR15 = script.Parent["Run (R15)"]
06local PlayRunR6 = humanoid:LoadAnimation(RunR6) -- Notice
07local PlayRunR15 = humanoid:LoadAnimation(RunR15) -- Notice
08local UserInputService = game:GetService('UserInputService')
09 
10UserInputService.InputBegan:connect(function(input)
11    if input.KeyCode == Enum.KeyCode.LeftShift then
12        if humanoid.RigType == Enum.HumanoidRigType.R6 then
13            humanoid.WalkSpeed = 24
14            PlayRunR6:Play()
15        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
View all 32 lines...
0
09:42:49.204 - LoadAnimation requires the Humanoid object (SilverCreeper58.Humanoid) to be a descendant of the game object SilverCreeper58 23 — 7y
0
Try using :WaitForChild() on the Player's Humanoid. MarceloFBentley 13 — 7y

Answer this question