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?
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)
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)
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
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.
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.
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)
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.