Animation in Module script is not working

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!

3 Likes

The i ~= PlayAnimation condition compares the animation name to the key, not to the AnimationTrack object? Why not v ~= PlayAnimation?
Isn’t that the problem? Arrange the print(). Also, please use clear names in the code, otherwise this code is not particularly readable.

I advise you to use my animation playback utility so that they do not depend on roblox servers. and in principle it is cooler :b
Animator plus | Custom, optimized animator - Resources / Community Resources - Developer Forum | Roblox

function module.PlayAnimation(animationName: string, slot: number)
	local animationTrack = Animations[slot] and Animations[slot][animationName]
	if not animationTrack then
		error(animationName .. " is not an Animation in table")
		return
	end

	for animationName2, animationTrack2 in (Animations[slot]) do
		if animationName2 ~= animationName and animationTrack2 and animationTrack2:IsPlaying() then
			animationTrack2:Stop()
		end
	end

	if animationTrack then
		if not animationTrack.IsPlaying() then -- idk
			print("play")
			animationTrack:Play()
		end
	else
		warn("idk")
	end
end
1 Like

It might look weird but this is right, it even prints the stuff but it fails to play that animation.

1 Like
function module.PlayAnimation(animationName: string, slot: number)
	local animationTrack = Animations[slot] and Animations[slot][animationName]
	if not animationTrack then
		error(animationName .. " is not an Animation in table")
		return
	end

	for animationName2, animationTrack2 in (Animations[slot]) do
		if animationName2 ~= animationName and animationTrack2 and animationTrack2:IsPlaying() then
			animationTrack2:Stop()
			print("stopped",animationName2)
		end
	end

	if animationTrack then
		if not animationTrack.IsPlaying(animationName,slot) then
			print("Playing animation: " .. animationName)
			animationTrack:Play()
		end
	else
		warn("Animation " .. animationName .. slot)
	end
end

function module.LoadAnimations(AnimationObject: Object)
	local gunID = AnimationObject.gunID
	Animations[gunID] = Animations[gunID] or {}

	for i, v in pairs(AnimationObject) do
		if typeof(v) == "Instance" and v:IsA("Animation") then
			if v.AnimationId == "rbxassetid://0" or v.AnimationId == "" then 
				warn("Animation ID is empty or invalid for animation: " .. i .. " in gun: " .. gunID)
				return
			end
			local animator = nil
			if i:find("viewModel") then
				animator = ViewModelAnimator
			elseif i:find("model") then
				animator = HumanoidAnimator
			else
				warn("Unknown animation type!")
				continue
			end

			if animator then
				local AnimationLoaded = animator:LoadAnimation(v)
				if AnimationLoaded then
					Animations[gunID][i] = AnimationLoaded
				else
					warn("Failed to load animation")
				end
			end
		end
	end
	print("Animations loaded for gunID:", gunID, Animations[gunID])
end


function module.IsPlaying(animationName: string, slot: number)
	local animationTrack = Animations[slot] and Animations[slot][animationName]
	return animationTrack and animationTrack:IsPlaying()
end

you just renamed variables no?

Heya, could I get some more info on what the player slot is?

nothing much, it should represent in which slot is it and its assigned automaticlly according to how many weapons player already have, its just number so I can assign correct animation to correct gun.
Imagine it as some unique value that represents that object

I see, does the ‘slot’ parameter inside PlayAnimation() get plrSlot passed into it?

yup, It does, I just confirmed that

Inside LoadAnimations(), you create a new Animations entry and set the key to ‘gunID’ while in PlayAnimation() you reference the entry using ‘slot’ , which you have confirmed to be plrSlot.
Could this have anything to do with the issue?

1 Like

omg thanks, it was probably the issue, like right now I have diffrent error where before when I played the animation and it stayed with the ones i had before
now it plays like nothing, the player dosent have animations but thx a lot, it finally works yay

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.