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

How do I make this script so that if you hold left shift, you will run and play an animation?

I got this from a tutorial, since I'm a beginner scripter, but I tried to script it so when you hold it, you'll run, and an animation plays, and when you stop, it goes back to the default animation and walk speed, but I can't seem to script it that way, can someone help me? That would be nice. If you think this question is not constructive, I'll take it down, here is the script.

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://5044642525"

function onKeyPressed(inputObject, gameProcessEvent)
	if inputObject.KeyCode == Enum.KeyCode then
		humanoid:LoadAnimation(anim):Play()
		humanoid.WalkSpeed = 16*2
	end
end
uis.InputBegan:Connect(onKeyPressed)

1 answer

✓ Accepted answer

Paste this into a starter charcter script:

local character = game:GetService("Players").LocalPlayer.Character

local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
			character.Humanoid.WalkSpeed = 32
		end
	end
end)

userInputService.InputEnded:connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
			character.Humanoid.WalkSpeed = 16
		end
	end
end)

9 comments

0

I get these three errors: 19:47:49.343 - Walkspeed is not a valid member of Humanoid, 19:47:50.894 - Keycode is not a valid member of InputObject and 19:48:43.826 - Touched is not a valid member of Script

0

Actually just ignore the "Touched" error, that's from another script.

0

First you have to check if you have a local script inside of the startercharacterscripts folder

0

also you can add a wait(10) on the top of the script

0

That's not what I meant, I need to remove these errors, and if i add wait(10), nothing different happens.

0

Oh, and I forgot to tell you, you need to run an animation. Sorry!

0

i told you to use wait becauuse you said that you got an error called Walkspeed is not a valid member of humanoid

0

Also have you even tried how it look when you just change the walkspeed

0

oh my gosh, i am so dumb, i must have made an error in my code, sorry!

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