You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I am trying to make a custom animationHandler that plays animations from a folder.
-
What is the issue? Include screenshots / videos if possible!
The problem im currently experiencing is that the animations are playing for the NPCs, but not for player characters. -
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I have implemented checks for: Valid Animation ID, Existing Animator, Animation Track Priority.
I have re-uploaded all the animations with track priority and set the permissions properly.
Animation Handler:
local Debris = game:GetService(âDebrisâ)
local Assets = game.ReplicatedStorage.Assets
local AnimationsFolder = Assets.Animations
local AnimationHandler = {}
AnimationHandler.__index = AnimationHandler
function AnimationHandler.new(CharacterModel)
local OB = {
Character = CharacterModel,
ItemAnimations = {},
}
setmetatable(OB, AnimationHandler)
return OB
end
function AnimationHandler:Init()
local Character = self.Character
self:OnCharacterAdded(Character)
--Load Animations
local AnimContextFolder = AnimationsFolder.Item
local function ForItemCategory(CategoryFolder)
local CategoryName = CategoryFolder.Name
local StuffInFolder = CategoryFolder:GetChildren()
for _, Animation: Animation in pairs(StuffInFolder) do
local ActionName = Animation.Name
local AnimationName = CategoryName..ActionName --MeleeSwing, ToolEquip, StaffAttack
self.ItemAnimations[AnimationName] = Animation
end
end
for _, CategoryFolder in pairs(AnimContextFolder:GetChildren()) do
ForItemCategory(CategoryFolder)
end
end
function AnimationHandler:OnCharacterAdded(Character)
local Humanoid: Humanoid = Character:WaitForChild(âHumanoidâ)
self.Character = Character
local Animator = Humanoid:WaitForChild('Animator')
if not Animator then
print('bah')
--Animator = Instance.new('Animator', Humanoid)
end
self.Animator = Animator
end
function AnimationHandler:PlayAnimation(AnimationObject: Animation, specificTime: number)
local Animator: Animator = self.Animator
if not Animator then
print('Invalid animator for: ', self.Character)
end
local ID = AnimationObject.AnimationId;
if (ID == '') or (string.len(ID) == 0) then
--print('Animation has no ID: ', AnimationObject, ' Character: ', self.Character)
return
end
local Track: AnimationTrack = Animator:LoadAnimation(AnimationObject); Track.Priority = Enum.AnimationPriority.Action2
if specificTime then
local AnimationDefaultSpeed = Track.Length
Track:AdjustSpeed(specificTime/AnimationDefaultSpeed)
print('adjusting animatoin spead')
end
print('ANIMATION TRACLK: ', Track)
game.ReplicatedStorage.PlayPlayerAnimation:FireAllClients(self.Character, AnimationObject)
task.wait()
Track:Play()
--Debris:AddItem(Track, Track.Length)
end
function AnimationHandler:PlayLoadedAnimation(ActionName)
local AnimationObject = self.ItemAnimations[ActionName]; if not AnimationObject then error(â[AnimationHandler]: Animation Does not exist:â..ActionName) return end
self:PlayAnimation(AnimationObject)
end
return AnimationHandler