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

How can i change the running animation of a player when they press shift?

Asked by 1 year ago

so basically i have this code and i want the change the running animation when i press shift but it wont work.

(this is a local script)

local runningAnimation = Instance.new('Animation')
local jumpingAnimation = Instance.new('Animation')
local keyboardButtonPressed = false
local controllerButtonPressed = false


local defaultSpeed = 10
local sprintSpeed = 24 
local FOV = 70 
local walkingFOV = {FieldOfView = FOV} 
local sprintFOV = {FieldOfView = FOV + 10} 
local tweenTime = 1
local keyboardButton = Enum.KeyCode.LeftShift 
local controllerButton = Enum.KeyCode.ButtonL2
local tweenEasingStyle = Enum.EasingStyle.Exponential
local tweenEasingDirection = Enum.EasingDirection.Out 
runningAnimation.AnimationId = 'rbxassetid://9913645916'
jumpingAnimation.AnimationId = 'rbxassetid://0'
local keepJumpingAnimation = false


local player = script.Parent
local humanoid = player:WaitForChild("Humanoid",5)
local camera = game.workspace.CurrentCamera
local animator = humanoid:WaitForChild("Animator") 
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local playRunning = humanoid:LoadAnimation(runningAnimation)
local playJumping = humanoid:LoadAnimation(jumpingAnimation)

local Info = TweenInfo.new(tweenTime, tweenEasingStyle, tweenEasingDirection)
local runningT = TweenService:Create(camera, Info, sprintFOV)
local walkingT = TweenService:Create(camera, Info, walkingFOV)


UserInputService.InputBegan:Connect(function(input,gameProcessedEvent)
    if input.KeyCode == keyboardButton then
        keyboardButtonPressed = true
        while keyboardButtonPressed == true do
            wait()
            humanoid.WalkSpeed = sprintSpeed
        end
    end
    if input.KeyCode == controllerButton then
        controllerButtonPressed = true
        while controllerButtonPressed == true do
            wait()
            humanoid.WalkSpeed = sprintSpeed
        end
    end
end)

UserInputService.InputEnded:Connect(function(input,gameProcessedEvent)
    if input.KeyCode == keyboardButton then
        keyboardButtonPressed = false
        while keyboardButtonPressed == false do
            wait()
            humanoid.WalkSpeed = defaultSpeed
        end
    end
    if input.KeyCode == controllerButton then
        controllerButtonPressed = false
        while controllerButtonPressed == false do
            wait()
            humanoid.WalkSpeed = defaultSpeed
            end
        end
end)

-- Checks if your speed is over than the defaultSpeed by 1, Which it assumes that you're running, and plays the animation.
humanoid.Running:Connect(function(Speed)
    if Speed > defaultSpeed + 1 then
            if not playRunning.IsPlaying then
                runningT:Play()
                playRunning:Play()
        end
    else
        walkingT:Play()
        playRunning:Stop()
    end
end)


humanoid.Jumping:Connect(function()
    if playRunning.IsPlaying then
        playRunning:Stop()
        if keepJumpingAnimation == true then
            playJumping:Play()
        end
    end
end)
0
Can I give u a suggest. I would recommend you just have a sprint button, because mobile players won't be able to use sprint. blue_bunny0fficl 98 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago

Put this local script in StarterCharacterScripts

local UIS = game:GetService('UserInputService')

local Player = game.Players.LocalPlayer

local Character = Player.Character





UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.

 if input.KeyCode == Enum.KeyCode.LeftShift then

  Character.Humanoid.WalkSpeed = 26--change walk speed

  local Anim = Instance.new('Animation')

        Anim.AnimationId = 'rbxassetid://9040323497'--put animation id

  PlayAnim = Character.Humanoid:LoadAnimation(Anim)

  PlayAnim:Play()

 end

end)





UIS.InputEnded:connect(function(input)

 if input.KeyCode == Enum.KeyCode.LeftShift then

  Character.Humanoid.WalkSpeed = 16--change walk speed

  PlayAnim:Stop()

 end

end)

Hope this helps!!

Ad

Answer this question