Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

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.

3 answers

✓ Accepted answer
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.

10 comments

1

:wait() and :connect() were deprecated. Use :Wait() and :Connect() instead

0

True that, however :wait() and :connect() still work fine. Good catch tho!

0

It doesn't seem to work both in Studio and in game. It didn't work when you sent that script.

0

Hmmm, could you check for any error calls given in the Output? For diagnosing purpose.

0

There aren't any errors.

0

Alright, fixed the script once again, hopefully its good to go now.

0

Still broken

0

I'm sure :wait() and :connect() serve the same purpose as :Wait() and :Connect(). Unless I'm missing something..

0

Nvm, its working! :D

0

Happy to hear that! :)

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)

2 comments

0

Isn't supposed to be left shift key?

0

The .KeyDown function is deprecated, wouldn't suggest using it.

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)

2 comments

0

09:42:49.204 - LoadAnimation requires the Humanoid object (SilverCreeper58.Humanoid) to be a descendant of the game object

0

Try using :WaitForChild() on the Player's Humanoid.

This discussion is preserved as it was. New answers, comments, and votes are closed.