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:
01 | local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:wait() |
02 | local char = plr.Character or plr.Character.CharacterAdded:wait() |
03 | local humanoid = char.Humanoid |
04 | local RunR 6 = script.Parent [ "Run (R6)" ] |
05 | local RunR 15 = script.Parent [ "Run (R15)" ] |
06 | local PlayRunR 6 = humanoid:LoadCharacter(RunR 6 ) |
07 | local PlayRunR 15 = humanoid:LoadCharacter(RunR 15 ) |
08 | local UserInputService = game:GetService( 'UserInputService' ) |
09 |
10 | UserInputService.InputBegan:connect( function (input) |
11 | if input.KeyCode = = Enum.KeyCode.LeftShift then |
12 | if Enum.HumanoidRigType.R 6 then |
13 | humanoid.WalkSpeed = 24 |
14 | PlayRunR 6 :Play() |
15 | elseif Enum.HumanoidRigType.R 15 then |
16 | humanoid.WalkSpeed = 24 |
17 | PlayRunR 15 :Play() |
18 | end |
19 | end |
20 | 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.
01 | local plr = game.Players.LocalPlayer |
02 | local char = plr.Character or plr.CharacterAdded:Wait() |
03 | local humanoid = char:WaitForChild( "Humanoid" ) |
04 | local RunR 6 = script.Parent [ "Run (R6)" ] |
05 | local RunR 15 = script.Parent [ "Run (R15)" ] |
06 | local PlayRunR 6 = humanoid:LoadAnimation(RunR 6 ) -- Notice |
07 | local PlayRunR 15 = humanoid:LoadAnimation(RunR 15 ) -- Notice |
08 | local UserInputService = game:GetService( 'UserInputService' ) |
09 |
10 | UserInputService.InputBegan:Connect( function (input) |
11 | if input.KeyCode = = Enum.KeyCode.LeftShift then |
12 | if humanoid.RigType = = Enum.HumanoidRigType.R 6 then |
13 | humanoid.WalkSpeed = 24 |
14 | PlayRunR 6 :Play() |
15 | elseif humanoid.RigType = = Enum.HumanoidRigType.R 15 then |
16 | humanoid.WalkSpeed = 24 |
17 | PlayRunR 15 :Play() |
18 | end |
19 | end |
20 | 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.
You could also just use this... Put this in a LocalScript inside of StarterCharacterScripts
01 | -- Locals |
02 | local Mouse = game.Players.LocalPlayer:GetMouse() |
03 | local char = game.Players.LocalPlayer.Character |
04 | local sb = string.byte |
05 |
06 | -- Main |
07 | Mouse.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 ) |
17 | end ) |
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.
01 | local plr = game.Players.LocalPlayer |
02 | local char = plr.Character |
03 | local humanoid = char:WaitForChild( "Humanoid" ) |
04 | local RunR 6 = script.Parent [ "Run (R6)" ] |
05 | local RunR 15 = script.Parent [ "Run (R15)" ] |
06 | local PlayRunR 6 = humanoid:LoadAnimation(RunR 6 ) -- Notice |
07 | local PlayRunR 15 = humanoid:LoadAnimation(RunR 15 ) -- Notice |
08 | local UserInputService = game:GetService( 'UserInputService' ) |
09 |
10 | UserInputService.InputBegan:connect( function (input) |
11 | if input.KeyCode = = Enum.KeyCode.LeftShift then |
12 | if humanoid.RigType = = Enum.HumanoidRigType.R 6 then |
13 | humanoid.WalkSpeed = 24 |
14 | PlayRunR 6 :Play() |
15 | elseif humanoid.RigType = = Enum.HumanoidRigType.R 15 then |