Hello, I am making my own animation handler for my game, I am right now in phase of debugging it and for some reason it dosen’t work
Right now I can’t make right function "module.PlayAnimation() which is here for playing animation from table
everything works right until final step it needs to play the animation, it does nothing
I run that moduleScript from LocalScript
script:
--[[
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 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: {AnimationTrack} = {}
--TODO it was not tested yet
--[[TODO not finished
--]]
module.AnimationsEnum = {
viewModelIdleAnimation = "viewModelIdleAnimation",
viewModelFireAnimation = "viewModelFireAnimation",
viewModelReloadAnimation = "viewModelReloadAnimation",
viewModelInspectAnimation = "viewModelInspectAnimation",
modelIdleAnimation = "modelIdleAnimation",
modelFireAnimation = "modelFireAnimation",
modelReloadAnimation = "modelReloadAnimation",
modelRunAnimation = "modelRunAnimation"
}
function module.PlayAnimation(PlayAnimation: string, slot: number)
-- TODO make it so it uses only PlayAnimation parameter
if not Animations[slot][PlayAnimation] then error(PlayAnimation.." Is not an Animation in table") return end
for i, v in pairs(Animations[slot]) do
if not v.IsPlaying and i ~= PlayAnimation then
v:Stop()
end
end
if not Animations[slot][PlayAnimation].isPlaying then
print("action")
print(Animations[slot][PlayAnimation])
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 table.find(Animations[slot], Animation) then error(Animation.." Is not an Animation in table") return end
for _, v in pairs(Animations[slot]) do
for _, j: AnimationTrack in pairs(v) do
if j.IsPlaying then
return true
else
return false
end
end
end
end
function module.LoadAnimations(AnimationObject: Object)
Animations[AnimationObject.gunID] = {}
for i, v in pairs(AnimationObject) do
if typeof(v) == "Instance" and v:IsA("Animation") then
if v.AnimationId == "rbxassetid://0" then return end
if i:find("viewModel") then
local AnimationLoaded = ViewModelAnimator:LoadAnimation(v)
Animations[AnimationObject.gunID][i] = AnimationLoaded
elseif i:find("model") then
local AnimationLoaded = HumanoidAnimator:LoadAnimation(v)
Animations[AnimationObject.gunID][i] = AnimationLoaded
end
end
end
print(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
this is the Class which is it working with:
function Gun:new(gunID: number, plrName: string, plrSlot: number, name: string, damage: number, distanceModifier: number, maxAmmo: number, ammoInMagazine: number, reloadTime: number, fireRate: number, inaccuracy: number, bulletsPerShot: number, range, viewmodelIdleAnimation, viewmodelFireAnimation, viewmodelReloadAnimation, viewmodelInspectAnimation, modelIdleAnimation, ModelFireAnimation, modelReloadAnimation, runAnim, gunModel, viewModel, description)
local obj = setmetatable({},self)
obj.gunID = gunID -- gun ID
obj.PlrName = plrName -- name of player that have this gun in his inventory
obj.plrSlot = plrSlot -- player slot
obj.name = name -- gun name
obj.damage = damage -- how much damage player gets per bullet
obj.distanceModifier = distanceModifier -- used for damage couting: damage/(distance*modifier)
obj.maxAmmo = maxAmmo -- Maximal Ammo what player can carry with it self(dosen't count with ammo in magazine)
obj.ammoInMagazine = ammoInMagazine
obj.reloadTime = reloadTime -- max Ammo player can use without reloading
obj.fireRate = fireRate -- every X seconds gun can fire agin
obj.inaccuracy = inaccuracy -- possible maximal inaccuarcy higher = less accurate
obj.bulletsPerShot = bulletsPerShot -- how many bullets are fired per one shot
obj.viewModelIdleAnimation = viewmodelIdleAnimation -- idle animation
obj.viewModelFireAnimation = viewmodelFireAnimation -- fire animation
obj.viewModelReloadAnimation = viewmodelReloadAnimation -- reload aniation
obj.viewModelInspectAnimation = viewmodelInspectAnimation -- inspect animation
obj.modelIdleAnimation = modelIdleAnimation -- idle animation
obj.modelFireAnimation = ModelFireAnimation -- fire animation
obj.modelReloadAnimation = modelReloadAnimation -- reload aniation
obj.modelRunAnimation = runAnim
obj.isReloading = false
obj.isInspecting = false
obj.isFiring = false
obj.isAiming = false
obj.isEquipped = false
obj.gunModel = gunModel
obj.viewModel = viewModel
obj.range = range
obj.description = description
return obj
end
Thanks for any help!