Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I use :LoadAnimation() inside a module script?

I'm trying to make an efficient tool system for my game. I want to load animations through a tool module in the module script, but when I try to do it, I get the error:

LoadAnimation requires an Animation object

I can't think of any other reason this is happening other than it being a module, as this works just fine on local and server scripts. If anyone knows a way I can do this inside the script, that'd be great.

Here's my code:

local replicatedStorage = game.ReplicatedStorage
local toolFolder = replicatedStorage.Tools

local entries = {
	["GLOCK20"] = {
		tool = toolFolder.GLOCK20,
	}
}

function entries.GLOCK20.start(entry,player,tool)
	local entry = entry
	local tool = tool
	local animations = tool.Animations:GetChildren()
	local character = player.Character
	
	character.Humanoid.Animator:LoadAnimation(animations.HOLD):Play()
	character.Humanoid.Animator:LoadAnimation(animations.HOLDIDLE):Play()
end

function entries.start(entry,player,tool)
	
	if player and player.Parent == game.Players then
		local tool = tool
		local char = player.Character
		
		entries[tool.Name].start(entry,player,tool)
	end
end

return entries

1 answer

✓ Accepted answer

The problem is that Animator:LoadAnimation() requires Animation to work.

local animations = tool.Animations:GetChildren()

In this line, you are getting children of tool.Animations which returns a table containing children

animations.HOLD

and after that you reference the animation like this which returns nil and spits the error "LoadAnimation requires an Animation object".

An easy solution for this would be to remove :GetChildren() at line 13 and it should work because then it wouldn't return nil and you would get your AnimationObject.

2 comments

0

Thank you so much. I wrote this script at like 3 in the morning yesterday, but I still have no clue how I didn't notice that. Thanks for the help, and sorry for wasting your time.

0

Everybody makes stilly mistake like that especially at that time. You are not wasting anybody's time as we are volunteers and we are glad to help.

This discussion is preserved as it was. New answers, comments, and votes are closed.