So i made my own animation manager, for my game, but iam having problems, sometimes it dont transition the walk animation to idle animation smoothly
As you can see sometimes be smoothly, sometimes no, that is the animation script:
--services
local players = game:GetService("Players")
local runService = game:GetService("RunService")
--variables
local player = players.LocalPlayer
local main = player:WaitForChild("PlayerScripts"):WaitForChild("Main")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
--folders
local managers = main.Managers
--modules
local animationManager = require(managers.AnimationManager)
repeat
runService.Heartbeat:Wait()
until animationManager.isDefaultLoaded()
local function onRunning(speed)
if speed > 0 then
animationManager.adjustAnimation({
name = "walk",
speed = speed / 1.6
})
animationManager.switchAnimation("run",0.2,true)
else
animationManager.switchAnimation("idle",0.2,true)
end
end
animationManager.switchAnimation("idle",0.2,true)
humanoid.Running:connect(onRunning)
And here have the animation manager:
--services
local players = game:GetService("Players")
--modules
local animationList = require(script:WaitForChild("AnimationList"))
--variables
local animator = nil
local player = players.LocalPlayer
local loadedAnimations = {}
local lastAnimation = nil
local defaultsLoaded = false
local AnimationManager = {}
function AnimationManager.createAnimation(id,name)
local animation = Instance.new("Animation")
animation.AnimationId = id
animation.Name = name
return animation
end
function AnimationManager.loadDefaultAnimations(element)
local animations = animationList.getAnimationList(element)
for i,v in pairs(animations) do
loadedAnimations[i] = animator:LoadAnimation(AnimationManager.createAnimation(v,i))
local animation = loadedAnimations[i]
if i == "walk" or i == "run" then
animation.Priority = Enum.AnimationPriority.Core
elseif i == "idle" then
animation.Priority = Enum.AnimationPriority.Idle
else
animation.Priority = Enum.AnimationPriority.Action
end
end
defaultsLoaded = true
end
function AnimationManager.isDefaultLoaded()
return defaultsLoaded
end
function AnimationManager.loadAnimation(id,name,priority)
local animation = loadedAnimations[name]
if not animation then
animation = animator:LoadAnimation(AnimationManager.createAnimation(id,name))
animation.Priority = Enum.AnimationPriority[priority]
end
end
function AnimationManager.playAnimation(name,transictionTime,save)
if loadedAnimations[name] then
loadedAnimations[name]:Play(transictionTime or 0)
if save then
lastAnimation = name
end
end
end
function AnimationManager.stop(name,transictionTime)
local animation = loadedAnimations[name]
if animation then
animation:Stop(transictionTime or 0)
end
end
function AnimationManager.stopAll()
for i,v in pairs(loadedAnimations) do
v:Stop()
end
end
function AnimationManager.switchAnimation(name,transictionTime,save)
if lastAnimation and lastAnimation ~= name then
AnimationManager.stop(lastAnimation,transictionTime)
end
AnimationManager.playAnimation(name,transictionTime,save)
end
function AnimationManager.adjustAnimation(args)
local animation = loadedAnimations[args["name"]]
if animation then
args["name"] = nil
for i,v in pairs(args) do
if i == "speed" then
animation:AdjustSpeed(v)
elseif i == "weight" then
animation:AdjustWeight(v)
else
animation[i] = v
end
end
end
end
function AnimationManager.getAnimator()
local character = player.Character or player.CharacterAdded:Wait()
animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
player.CharacterAdded:Connect(function(character)
if character.Parent == nil then
character.AncestryChanged:Wait()
end
animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
end)
end
function AnimationManager.addEvent(name,dataFunction,...)
local args = {...}
local animation = loadedAnimations[name]
local connection
if animation then
if args[1] == "signal" then
connection = animation:GetMarkerReachedSignal(args[2]):Connect(function(param)
dataFunction(connection,param,unpack(args[3]))
end)
else
connection = animation.Stopped:Connect(function()
dataFunction(connection,unpack(args[3]))
end)
end
end
end
return AnimationManager