Throw animation not playing

Hi!
I just made an animation for my knife script, and it seems to not be playing.

What is wrong?
Thanks!

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local Tracks = {}
local InputType = Enum.UserInputType

local BeganConnection, EndedConnection

function playAnimation(animName, ...)
	if Tracks[animName] then
		Tracks[animName]:Play()
	else
		local anim = Tool:FindFirstChild(animName)
		if anim and Tool.Parent and Tool.Parent:FindFirstChild("Humanoid") then
			Tracks[animName] = Tool.Parent.Humanoid:LoadAnimation(anim)
			playAnimation(animName, ...)
		end
	end
end

function stopAnimation(animName)
	if Tracks[animName] then
		Tracks[animName]:Stop()
	end
end

function inputBegan(input)
	if input.UserInputType == InputType.MouseButton1 then
		Remote:FireServer("LeftDown", Mouse.Hit.p)
	end
end

function inputEnded(input)
	if input.UserInputType == InputType.MouseButton1 then
		Remote:FireServer("LeftUp")
	end
end

function onRemote(func, ...)
	if func == "PlayAnimation" then
		playAnimation(...)
	elseif func == "StopAnimation" then
		stopAnimation(...)
	end
end

function onEquip()
	BeganConnection = UIS.InputBegan:connect(inputBegan)
	EndedConnection = UIS.InputEnded:connect(inputEnded)
end

function onUnequip()
	if BeganConnection then
		BeganConnection:disconnect()
		BeganConnection = nil
	end
	
	if EndedConnection then
		EndedConnection:disconnect()
		EndedConnection = nil
	end
end

Tool.Equipped:connect(onEquip)
Tool.Unequipped:connect(onUnequip)
Remote.OnClientEvent:connect(onRemote)

image
No error

I reexported the anim, and it still does not work.

Try,

Tool.Parent.Humanoid.Animator:LoadAnimation(anim)

They’ve deprecated the old way of loading animations and now have to load it onto the Animator inside the Humanoid.

1 Like

Where exactly in the script?


Still did not work.

1 Like

You could try adding prints in the functions and test to see if they show up in the output. Wherever the prints don’t show up are the places where you should try to see what’s wrong.

1 Like


image
Something is wrong with the if statement…

Sorry for the late reply.

If the if statement isn’t working, it may be because of its requirements not being met.

You should make sure that all 3 of those requirements (anim and Tool.Parent and Tool.Parent:FindFirstChild(“Humanoid”)) are actually true (meaning that they are meeting those requirements in-game/testing).

1 Like

image
Think I found the problem, the tool doesn’t go into the Humanoid


So, I deleted the if statement, bc i thought it was causing the problems, but it caused this.

Is there just a better way of doing it all together?


New code is not working, kinda fixed it.

Did you remove the code under the if statement or just the if statement line?

The error may mean that it didn’t find a animation to play when you did LoadAnimation.

However I don’t know how you could do it better altogether.

1 Like

How could I just assign a Animation to the Humanoid while throwing

I’m not completely sure what’s wrong. Did you get any error?

Not with the code in the screenshot.

local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local Tracks = {}
local InputType = Enum.UserInputType

local BeganConnection, EndedConnection

function playAnimation(animName, ...)
	if Tracks[animName] then
		print ("e")
		Tracks[animName]:Play()
	else
		local anim = Tool.animName
		print ("found")
			print("if")
			Tracks[animName] = Tool.Parent.Humanoid.Animator:LoadAnimation(anim)
			print ("What if..")
			playAnimation(animName, ...)
			print ("This worked!")
	end
end

function stopAnimation(animName)
	if Tracks[animName] then
		Tracks[animName]:Stop()
	end
end

function inputBegan(input)
	if input.UserInputType == InputType.MouseButton1 then
		Remote:FireServer("LeftDown", Mouse.Hit.p)
	end
end

function inputEnded(input)
	if input.UserInputType == InputType.MouseButton1 then
		Remote:FireServer("LeftUp")
	end
end

function onRemote(func, ...)
	if func == "PlayAnimation" then
		print ("E PLAY CALLED OMG")
		playAnimation(...)
	elseif func == "StopAnimation" then
		stopAnimation(...)
	end
end

function onEquip()
	BeganConnection = UIS.InputBegan:connect(inputBegan)
	EndedConnection = UIS.InputEnded:connect(inputEnded)
end

function onUnequip()
	if BeganConnection then
		BeganConnection:disconnect()
		BeganConnection = nil
	end
	
	if EndedConnection then
		EndedConnection:disconnect()
		EndedConnection = nil
	end
end

Tool.Equipped:connect(onEquip)
Tool.Unequipped:connect(onUnequip)
Remote.OnClientEvent:connect(onRemote)

New full code

You could possibly play the animation right before or during the animation based on what you want to do.

A while ago, I did this with a tool and when the tool was Activated, a variable I created was set to true and when it was set to true, the animation would play (with a denounce so it doesn’t go wrong). Everything you need to do (in your example, throwing) will fire during the animation.

Can you show me an example with my code?

Here is a visualization of what I am talking about:

local activated = false

local debounce = false

— look to see if the input has begun with an if statement or the way you already did it.

— if all requirements are met, for example if the tool is in the Humanoid, etc. then you would set the debounce to true, then set activated to true.

— have a while loop that will play the animation like this:

while wait() do

if activated = true then

— play your animation here, then set activated and debounce to false.

end

Also, if this is the client, you could remove activated and the loop and send a RemoteEvent to the Server to play the animation there.

Does your animation work?

Like is it a working animation?