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 6 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:

local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:wait()
local char = plr.Character or plr.Character.CharacterAdded:wait()
local humanoid = char.Humanoid
local RunR6 = script.Parent["Run (R6)"]
local RunR15 = script.Parent["Run (R15)"]
local PlayRunR6 = humanoid:LoadCharacter(RunR6)
local PlayRunR15 = humanoid:LoadCharacter(RunR15)
local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if Enum.HumanoidRigType.R6 then
            humanoid.WalkSpeed = 24
            PlayRunR6:Play()
        elseif Enum.HumanoidRigType.R15 then
            humanoid.WalkSpeed = 24
            PlayRunR15:Play()
        end
    end
end)

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 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local RunR6 = script.Parent["Run (R6)"]
local RunR15 = script.Parent["Run (R15)"]
local PlayRunR6 = humanoid:LoadAnimation(RunR6) -- Notice
local PlayRunR15 = humanoid:LoadAnimation(RunR15) -- Notice
local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if humanoid.RigType == Enum.HumanoidRigType.R6 then
            humanoid.WalkSpeed = 24
            PlayRunR6:Play()
        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
            humanoid.WalkSpeed = 24
            PlayRunR15:Play()
        end
    end
end)

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

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

-- Locals
local Mouse = game.Players.LocalPlayer:GetMouse()
local char = game.Players.LocalPlayer.Character
local sb = string.byte

-- Main
Mouse.KeyDown:Connect(function(keybyte)
    Key = string.lower(keybyte)
        if sb(keybyte) == 48 then
            char.Humanoid.WalkSpeed = 21
        end
    Mouse.KeyUp:Connect(function(keybyte)
        if sb(keybyte) == 48 then
            char.Humanoid.WalkSpeed = 16
        end
    end)
end)
0
Isn't supposed to be left shift key? ILikeTofuuJoe 1 — 6y
0
The .KeyDown function is deprecated, wouldn't suggest using it. Le_Teapots 913 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 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.

local plr = game.Players.LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
local RunR6 = script.Parent["Run (R6)"]
local RunR15 = script.Parent["Run (R15)"]
local PlayRunR6 = humanoid:LoadAnimation(RunR6) -- Notice
local PlayRunR15 = humanoid:LoadAnimation(RunR15) -- Notice
local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if humanoid.RigType == Enum.HumanoidRigType.R6 then
            humanoid.WalkSpeed = 24
            PlayRunR6:Play()
        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
            humanoid.WalkSpeed = 24
            PlayRunR15:Play()
        end
    end
end)

UserInputService.InputEnded:connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if humanoid.RigType == Enum.HumanoidRigType.R6 then
            humanoid.WalkSpeed = 16
            PlayRunR6:Play()
        elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
            humanoid.WalkSpeed = 16
            PlayRunR15:Play()
        end
    end
end)
0
09:42:49.204 - LoadAnimation requires the Humanoid object (SilverCreeper58.Humanoid) to be a descendant of the game object SilverCreeper58 23 — 6y
0
Try using :WaitForChild() on the Player's Humanoid. MarceloFBentley 13 — 6y

Answer this question