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

How do I make it so when the player is running it dose a costom run animation?

Asked by 3 years ago
local plr = game.Players.LocalPlayer
local cam = game.Workspace.Camera
cam.CameraType = "Scriptable"
-- Start Sprint Values
local Value1 = 70
local add = 1
local inf = math.huge
local add1 = 1
local newval = 100
-- End sprint Values
local Value2 = 1
local minus = 1
local maxVal = 70
local Running = false

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        plr.Character.Humanoid.WalkSpeed = 26
        while true do
            Running = true
            add = 1
            Value1 = Value1 + add
            print(Value1)
            wait()
            cam.FieldOfView = (Value1)
            if Running == false then
                cam.FieldOfView = 70
                plr.Character.Humanoid.WalkSpeed = 16
                break
            end
            if Value1 == 120 then
                add = 0
                Value1 = 70
                break
            end
        end
    end 
end)

game:GetService("UserInputService").InputEnded:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        plr.Character.Humanoid.WalkSpeed = 16
        cam.FieldOfView = 70
        add = 0
        Value1 = 70
        Running = false
        game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
            if inputObject.KeyCode == Enum.KeyCode.LeftShift then
                print("no run xd")
                Running = false
            end
        end)
    end
end)

1 answer

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

If you want to add animation for running then you have to create the animation first by using Instance.new and then load the animation using the LoadAnimation() function. You can play the animation when the input begins by using Play(). The animation then will be stopped when the input has ended by using Stop().

Please note that the animation is bad but I custom made it just for this code example. You can probably see my dedication to this question.

==== CODE ====

local plr = game.Players.LocalPlayer
local cam = game.Workspace.Camera
cam.CameraType = "Scriptable"

-- Run Animation (Add to your script)
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://6687967125'
RAnimation = plr.Character:WaitForChild("Humanoid"):LoadAnimation(RunAnimation)

-- Start Sprint Values
local Value1 = 70
local add = 1
local inf = math.huge
local add1 = 1
local newval = 100
-- End sprint Values
local Value2 = 1
local minus = 1
local maxVal = 70
local Running = false

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        plr.Character.Humanoid.WalkSpeed = 26
        RAnimation:Play() -- Also add this to the script
        while true do
            Running = true
            add = 1
            Value1 = Value1 + add
            print(Value1)
            wait()
            cam.FieldOfView = (Value1)
            if Running == false then
                cam.FieldOfView = 70
                plr.Character.Humanoid.WalkSpeed = 16
                break
            end
            if Value1 == 120 then
                add = 0
                Value1 = 70
                break
            end
        end
    end 
end)

game:GetService("UserInputService").InputEnded:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        plr.Character.Humanoid.WalkSpeed = 16
        RAnimation:Stop() -- And also add this to the script
        cam.FieldOfView = 70
        add = 0
        Value1 = 70
        Running = false
        game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
            if inputObject.KeyCode == Enum.KeyCode.LeftShift then
                print("no run xd")
                Running = false
            end
        end)
    end
end)

NOTE: Loop has to be enabled AND priority must be movement.

In the future, you probably want to research how animation works and how it played OR stopped. You can also create the animation by using Animation Editor or use the animation of others by simply loading the asset ID.

Happy Scripting!

0
If you found this answer helpful then you can accept this answer and help others who are having the same problem. Somone_exe 224 — 3y
0
It's you again. And guess what, i was just looking on how to do this. iMazariuz 36 — 3y
0
Anyhow i got a problem. The track wouldn't stop. Even though it seems like the Track:Stop() code is executed. I have the track Looped to be enable and the priority is Action. Do you know how to solve it? iMazariuz 36 — 3y
0
you have to use Movement actually. Not action because it will repeat the same track. Somone_exe 224 — 3y
Ad

Answer this question