Should I handle animations and combos in modules?

Hey, I’m looking to see what is the best option for my combat game. Should i handle Animations in module scripts or the traditional local scripts?

Example:

local module = {}

local animations = {
   ["Animation1"] = [EXAMPLE1]
   ["Animation2"] = [EXAMPLE2]
   ["Animation3"] = [EXAMPLE3]
}

return module

Modules are superior but that one isn’t useful.

local animations = {}
for _, animation in animationFolder:GetChildren() do
    animations[animation.Name] = animation
end

I store my animations in an animation database which is a module, this makes ot easy for me to preload animations and call on animations in any context i need thrm.

1 Like

I recommend using a ModuleScript that loads all the AnimationTracks onto the current humanoid, and reloads them when the character respawns, so that you can use them on the client to play the animations

So i could load them like this?

local module = {}

function module.LoadAnimations(hum)
	local loadedAnimations = {}

	for animName, animId in pairs(module.Bat) do
		local animation = Instance.new("Animation")
		
		animation.AnimationId = animId
		loadedAnimations[animName] = hum:LoadAnimation(animation)
	end

	return loadedAnimations
end

module["Bat"] = {
	M1 = "rbxassetid://136802863065287",
	M2 = "rbxassetid://89876719415286",
	Finisher = "rbxassetid://111012784297932",
	Equip = "rbxassetid://110749480818182",
	Idle = "rbxassetid://88373637089906"
}

return module

I’d do something like:

local loadedAnimationTracks: {[string]: {[string]: AnimationTrack}} = {}

do
	local animationsToLoad: {[string]: {[string]: Animation}} = {}
	do
		local animationIdsToLoad = {
			Bat = {
				M1 = "rbxassetid://136802863065287",
				M2 = "rbxassetid://89876719415286",
				Finisher = "rbxassetid://111012784297932",
				Equip = "rbxassetid://110749480818182",
				Idle = "rbxassetid://88373637089906"
			}
		}
		for groupName, animations in animationIdsToLoad do
			local group = {}
			animationsToLoad[groupName] = group

			for animationName, animationId in animations do
				local animation = Instance.new('Animation')
				animation.AnimationId = animationId
				
				animationsToLoad[animationName] = animation
			end
		end
	end

	local Players = game:GetService('Players')
	Players.LocalPlayer.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChildWhichIsA('Humanoid')
		if humanoid == nil then return end

		for groupName, animations in animationsToLoad do
			local group = {}
			loadedAnimationTracks[groupName] = group

			for animationName, animation in animations do
				group[animationName] = humanoid:LoadAnimation(animation)
			end
		end
	end)
end

return loadedAnimationTracks

That way, it just automatically loads all animations when the character spawns
and also doesn’t leak memory by creating multiple animations for the same thing

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