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

How can i change animation while sprinting?

Asked by
yayachik1 -11
5 years ago

My game like a lot of games has a shift to sprint script which does nothing except speed the player up when shift is held down and I was wondering how i could make it so that when i hold shift another running animation plays here is the current shift to sprint script I'm using: there is a regular script:

function onPlayerEntered(player)
    repeat wait () until player.Character ~= nil
    local s = script.SprintScript:clone()
    s.Parent = player.Character
    s.Disabled = false
    player.CharacterAdded:connect(function (char)
        local s = script.SprintScript:clone()
        s.Parent = char
        s.Disabled = false      
    end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

and inside of the regular script there is a local script:

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()  
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end


mouse.KeyDown:connect(function (key) -- Run function
    key = string.lower(key)
    if string.byte(key) == 48 then
        running = true
        local keyConnection = mouse.KeyUp:connect(function (key)
            if string.byte(key) == 48 then
                running = false
            end
        end)
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24
        repeat wait () until running == false
        keyConnection:disconnect()
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
            wait()
        end
    end
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Here's your script with a few changes:

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()  
    for _, kid in ipairs(script.Parent:GetChildren()) do
        if kid.className == "Tool" then return kid end
    end
    return nil
end


mouse.KeyDown:connect(function (key) -- Run function
    local oldId = game.Players.LocalPlayer.Character.Animate.Run.RunAnim.AnimationId
    key = string.lower(key)
    if string.byte(key) == 48 then
        running = true
        local keyConnection = mouse.KeyUp:connect(function (key)
            if string.byte(key) == 48 then
                running = false
            end
        end)
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
            wait()
        end
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24
    game.Players.LocalPlayer.Character.Animate.Run.RunAnim.AnimationId = "" --                      animation ID while running
        repeat wait () until running == false
        keyConnection:disconnect()
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
    game.Players.LocalPlayer.Character.Animate.Run.RunAnim.AnimationId = oldId
        for i = 1,5 do
            game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
            wait()
        end
    end
end)

Hope this helps!

Ad

Answer this question