Animation LocalScript Not Working

Hello everyone,

So, I have a small problem. So, what’s the problem? I have a LocalScript in StarterPlayer > StarterPlayerScripts and I have RunAnim in StarterPlayer > StarterCharacterScripts. So, when I enter the game and press the button “K” it doesn’t work at all. So, anyone tell me where am I missing this up exactly and how do I fix it? :confused:

image

This is the LocalScript.

local UIS = game:GetService("UserInputService")
local Playing = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode = Enum.KeyCode.K then
        if Playing == false then
            Playing = true
            Run.RunAnim:Play()
        elseif Playing == true then
            Playing = false
            Run.RunAnim:Stop()
        end
    end
end)

That’s all.

1 Like

This is supposed be like this:

 if Input.KeyCode == Enum.KeyCode.K then

take note of the two equal signs. This is used to compare if both values are the same.

local UIS = game:GetService("UserInputService")
local Playing = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.K then
        Playing = not Playing
        if Playing then
            Run.RunAnim:Play()
        else
            Run.RunAnim:Stop()
        end
    end
end)

Cleaned it up a bit

2 Likes

Umm, I have no idea what your trying to say… Could you explain in more details??

Note: (I’m beginner in scripting.)

1 Like

Ohh, okay. So, it was just a sign problem?

1 Like

In the code you posted, it had this:

if Input.KeyCode = Enum.KeyCode.K then

This is not correct because you’re using only one equals sign. When checking if the key code or any value is equal to another value, you have to use == this will check if both are the same.

Bool = true --this will set the variable "Bool" to true
Bool == true --this will check if the variable "Bool" is true

See the difference

2 Likes

https://www.lua.org/pil/3.2.html

Single equals is an assignment, two is the equality relational operator. This was explained right after the correction was suggested. You should’ve also received a red underline for this. If so, make sure to hover over it to check what the issue is.

2 Likes

Okay, I fixed it but, this is showing up… What does blue underline mean?

1 Like

Hover over it to check for the issue. Run is not a variable declared in your script. Create a local variable called Run and define it to an AnimationTrack, which can be fetched by calling LoadAnimation on the Humanoid with an Animation object passed.

2 Likes

Woah! Never knew these is a feature like that. Thanks for telling me about it. O_O

You should look into this post!

It includes pretty much everything you should know.

Here is what I think you’re tryna do.

local Player = game.Players.LocalPlayer 
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Playing = false

local Animation = ANIMATION_HERE

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.K then
        if not Playing then
            Playing = true 
            local Anim = Humanoid:LoadAnimation(Animation)
            Anim:Play()
        else
            Anim:Stop()
        end
    end 
end)
1 Like

You have to define what Run is in your script for it to work. Since the animation is in StarterCharacter, it will get cloned into the character upon spawning

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Character:WaitForChild("RunAnim")
local RunAnim = Humanoid:LoadAnimation(Animation)

local Playing = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.K then
        Playing = not Playing
        if Playing then
            RunAnim:Play()
        else
            RunAnim:Stop()
        end
    end
end)

Take note that when the character dies, you will have to redefine the Character , Animation , RunAnim and Humanoid variable because the character model will change upon respawning.

1 Like

Thanks! it works i figured out how by seeing your script and the animation i have in the game. :heart:

1 Like

Define the Run Variable, with this:

local Run = game.StarterPlayer.StarterCharacterScripts.RunAnim

1 Like