Why my animation isn't play?

heres my script:

local player = game:FindFirstChild("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local debounce = false
local hit_C = 1
local Humanoid = player.Character:FindFirstChild("Humanoid")
local animationTrack = Humanoid:LoadAnimation(script.Animation)


local function client_a (Input, IS)
	if IS == true then
		return
	else
		if Input.KeyCode == Enum.KeyCode.E  then
			if debounce == true then return end
				debounce = true
			game.ReplicatedStorage.CHit:FireServer()
			if hit_C == 1 then 
				hit_C = 2
				script.Animation.AnimationId = "rbxassetid://16166244060"
			elseif hit_C == 2 then
				hit_C = 1
				script.Animation.AnimationId = "rbxassetid://16166263881"
			elseif hit_C == 3 then
				return
			end
			animationTrack:Play()
			wait(0.2)
			debounce = false
			print("A")
		end
	end 
end 

UserInputService.InputBegan:Connect(client_a)

Could be that it hasn’t loaded yet.

What if you try deleting the “local animationTrack = Humanoid:LoadAnimation(script.Animation)” up

And placing it again before animationTrack:Play()

local animationTrack = Humanoid:LoadAnimation(script.Animation)
	animationTrack:Play()
			wait(0.2)
			debounce = false
			print("A")

Not sure if this helps but i use a ModuleScript to play some animations etc.

And inside the functions i use something like this:

local Hold = viewmodel.AnimationController:LoadAnimation(hold)
Hold:Play()

Try what @SpookStrikeBack said that might work!

2 Likes

Animation still doesn’t work. I don’t know what will i do.

This is just a Guess, but i don’t think you can change a AnimationId Tru the Client, if you can, only put A unique id on the animation object, but i think that won’t what you want, as you need multiple animations

Maybe Try Putting a RemoteEvent on ReplicatedStorage, and Manage the Animation ID changes on the server

Something like this?

local rep = game:GetService("ReplicatedStorage")
local remote = rep:WaitForChild("animchange")

remote.OnServerEvent:Connect(function(animationObject, animid)
     if animationObject then  
            animationObject.AnimationId = "rbxassetid://"..tostring(animid)
     end
end)

Also, it prints any errors on the console?

it doesn’t print any error on console.

do you own the animation? because if you dont own it it wont play so that could be it

I believe you can’t change the AnimationId after loading. LoadAnimation literally loads the current AnimationId’s animation and creates a track from it, so it makes sense the ID can’t be changed after without loading it again.

Here is some code to illustrate what to do instead:

local player = game:FindFirstChild("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local debounce = false
local hit_C = 1
local Humanoid = player.Character:FindFirstChild("Humanoid")

-- You need to make animation tracks for the animations
local anim1 = script.Animation:Clone()
anim1.AnimationId = "rbxassetid://16166244060"
anim1.Parent = script
local track1 = Humanoid:LoadAnimation(anim1)

local anim2 = script.Animation:Clone()
anim2.AnimationId = "rbxassetid://16166263881"
anim2.Parent = script
local track2 = Humanoid:LoadAnimation(anim2)



local function client_a (Input, IS)
	if IS == true then
		return
	else
		if Input.KeyCode == Enum.KeyCode.E  then
			if debounce == true then return end
				debounce = true
			game.ReplicatedStorage.CHit:FireServer()
			if hit_C == 1 then 
				hit_C = 2
				anim1:Play()
			elseif hit_C == 2 then
				hit_C = 1
				anim2:Play()
			elseif hit_C == 3 then
				return
			end
			wait(0.2)
			debounce = false
			print("A")
		end
	end 
end 

UserInputService.InputBegan:Connect(client_a)

I may be wrong, but this is probably because you define it at the start; while it already has a ID, you should define it at the Hit_C.

Example: if hit_C == 1 then

hit_C = 2

script.Animation.AnimationId = “rbxassetid://16166244060”

animationTrack = Humanoid:LoadAnimation(script.Animation)

Someone correct me if I am wrong, though.