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