Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

New to animationst, no matter what i do the animations will not play when player clicks a key, help?

Just started using animations, ok back to question, I want someone in my game to click a key and it trigger an animation, its not working, dont remember where i snagged this script from but no matter what i change/do it doesnt work.

heres the script

local animkey = "Q"
local Player = game.Players.LocalPlayer
local Character = Player.Character
local AnimationID = "3823158750"
local CharAnimation
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode[animkey] then
  animation()
end
end)
function animation()
  if Character then
    local UseLessAnimation = Character:FindFirstChild("AnimationCharacter")
    if CharAnimation then
      CharAnimation:Stop()
    end
    if UseLessAnimation then
      if UseLessAnimation.AnimationId=="rbxassetid://"..AnimationID then
        UseLessAnimation:Destroy()
        return
      end
      UseLessAnimation:Destroy()
    end
    local Animation =Instance.new("Animation",Character)
    Animation.Name= "AnimationCharacter"
    Animation.AnimationId = "rbxassetid://"..AnimationID
    CharAnimation= Character.Humanoid:LoadAnimation(Animation)
    CharAnimation:Play()
  end
end

2 answers

Note that your function calls and variables will only work if they are defined already above the call, not below. Also, you can make an animation by this way:

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

-- create the animation:
local Animation = Instance.new("Animation")

-- Set animation ID:
Animation.AnimationId = "00000000"

-- create the track:
AnimationTrack = Humanoid:LoadAnimation(Animation)

-- Inputservice:
UIS.InputBegan:Connect(function(key)
	if input.KeyCode == Enum.KeyCode.Q then
		-- Play animation:
		AnimationTrack:Play()
	end
end)

NOTES:

  • The animation will not play properly if its priority is less then "movement", sinse the default idle animation have "idle" priority. Change your anim's priority to "Movement" at least. (See more at https://developer.roblox.com/en-us/api-reference/property/AnimationTrack/Priority) Animations with same priority can play together in the same char.

  • Check if the animation is looped or not if you want it to loop or not to loop automatically.

  • Just use "AnimationTrack:Pause()" if you want it to stop by an input.

  • If you call :Play() again in the same animation, already playing, it will stop the previous one and start it again.

4 comments

0

Opps i had forgotten the animation ID, but fixed now.

0

Still doesnt seem to work, I have the script in a local script in starter player scripts, is that the right place?

0

You will have to put inside Starter Character Scripts. If you put it in starterplayer you will have some errors in getting the char with this script, it would had to be written in a different way, and the script would stop working once the char dies.

0

I just saw I forgot to write "local" before the variable "AnimationTrack" in line 11. Hope you noticed that.

You have to make the character's Humanoid load your animation. You have written the script's function name first, then have written the function. It's like trying to eat a banana before you get the banana.

Re-Write your script with the following:

local Player = game.Players.LocalPlayer
local Character = Player.Character

local newA = Instance.new("Animation", script)
newA.Animationid = "3823158750"

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		if Input.Keycode == Enum.KeyCode.Q then
			local Anim = Character.Humanoid:LoadAnimation(newA)
			Anim:Play()
		end
	end
end)

This is a 100% working simple script. Replace this script with your one and it will be working fine.

1 comment

1

You wrote "InputBegan" like "InpputBegan" with two p's, just a small note.

This discussion is preserved as it was. New answers, comments, and votes are closed.