Hello, I am doing my own animation handler for my gun script but right now I have problem. After trying to run one animation everything is okey but the moment I want to run another one it brokes, it won’t stop playing and it still plays the first animation
issued part
function module.PlayAnimation(PlayAnimation: string, slot: number)
if not Animations[slot][PlayAnimation] then debugPrint("animations: ") debugPrint(Animations) debugPrint(PlayAnimation.." Is not an Animation in table") return end
for i, v in pairs(Animations) do
for _, j in pairs(v) do
if not j.IsPlaying and _ ~= PlayAnimation then
j:Stop()
end
end
end
if not Animations[slot][PlayAnimation].isPlaying then
Animations[slot][PlayAnimation]:Play()
end
end
-- whole script is here:
--[[
AnimationHandler for GunScript
animationsEnum -> Enum table of all possible animations
PlayAnimation -> Simply Plays animation that is provided as parameter
ForceEndAllAnimations -> Forces to end all playing animations
IsPlaying -> Returns Boolean value(true/false) if that animation is playing
LoadAnimation -> Loads animation into handler so it can be usable
LoadViewmodelAnimator -> Locates Animator of ViewModel which is needed for this script to work correctly
--]]
local debugMode = false
local module = {}
local BooleanValue = true
local localplr = game.Players.LocalPlayer
local ViewModelAnimator: Animator = nil
local Humanoid: Humanoid = localplr.Character:WaitForChild("Humanoid")
local HumanoidAnimator = Humanoid.Animator
local Animations = {}
if debugMode == true then
warn("Debug mode of GunLocal is on, shouldn't be used in production")
end
--[[
Fully tested, everything should work but if you find any bug please report on discord
if you find any bugs write them here = {
}
and report on discord!
--]]
module.AnimationsEnum = {
viewModelIdleAnimation = "viewModelIdleAnimation",
viewModelFireAnimation = "viewModelFireAnimation",
viewModelReloadAnimation = "viewModelReloadAnimation",
viewModelInspectAnimation = "viewModelInspectAnimation",
viewModelEquipAnimation = "viewModelEquipAnimation",
modelIdleAnimation = "modelIdleAnimation",
modelFireAnimation = "modelFireAnimation",
modelReloadAnimation = "modelReloadAnimation",
modelRunAnimation = "modelRunAnimation",
modelEquipAnimation = "modelEquipAnimation"
}
function debugPrint(msg: string)
if debugMode == true then
warn(msg)
end
end
function module.PlayAnimation(PlayAnimation: string, slot: number)
if not Animations[slot][PlayAnimation] then debugPrint("animations: ") debugPrint(Animations) debugPrint(PlayAnimation.." Is not an Animation in table") return end
for i, v in pairs(Animations) do
for _, j in pairs(v) do
if not j.IsPlaying and _ ~= PlayAnimation then
j:Stop()
end
end
end
if not Animations[slot][PlayAnimation].isPlaying then
Animations[slot][PlayAnimation]:Play()
end
end
function module.ForceEndAllAnimations()
for _, v in pairs(Animations) do
for _, j: AnimationTrack in pairs(v) do
if j.IsPlaying then
j:Stop()
end
end
end
end
function module.IsPlaying(Animation: string, slot: number)
if not Animations[slot][Animation] then debugPrint("Animation "..Animation.." Dosen't exist for gun on slot ["..slot.."], this might be intentional but rather check if animations are set correctly") return false end
return Animations[slot][Animation].IsPlaying
end
function module.LoadAnimations(AnimationObject: {})
Animations[AnimationObject.plrSlot] = {}
for i, v in pairs(AnimationObject) do
if typeof(v) == "Instance" and v:IsA("Animation") then
if v.AnimationId ~= "rbxassetid://0" then
if i:find("viewModel") then
local AnimationLoaded = ViewModelAnimator:LoadAnimation(v)
Animations[AnimationObject.plrSlot][i] = AnimationLoaded
elseif i:find("model") then
local AnimationLoaded = HumanoidAnimator:LoadAnimation(v)
Animations[AnimationObject.plrSlot][i] = AnimationLoaded
end
end
end
end
debugPrint("Animations loaded into table")
debugPrint(Animations)
end
function module.LoadViewmodelAnimator(ViewM: Model)
if not ViewM:IsA("Model") then error("ViewModel is a Model and this is not a model") end
if not ViewM.AnimationController.Animator then error("Animator was not found in "..ViewM:GetFullName()..".AnimationController.Animator") return end
ViewModelAnimator = ViewM.AnimationController.Animator
end
return module