When returning an instance from a table via module, it loses all of it's functions

Hello Roblox Game Developers

I’ve seen as my animate script has taken up too much space I would use a module to get the instances instead of storing them as variables. It’s just an opinion thing, but I haven’t found a get around of this one error.

(Property or Function) is not a valid member of …

Any idea as why this is happening or any solutions to this that don’t include discarding the whole module and putting the animations in the main code?

Module:

local Ancestor = script.Parent
local MovementAnimations = Ancestor.MovementAnimations
local WeaponAnimations = Ancestor.WeaponAnimations

local AnimationSettings = {
	["MovementAnimations"] = {
		["Idle"] = MovementAnimations.Idle,
		["Walk"] = MovementAnimations.Walk
	},
	
	["WeaponAnimations"] = {
		["Idle"] = WeaponAnimations.Idle,
		["Mouse1"] = WeaponAnimations.Mouse1,
		["Parry"] = WeaponAnimations.Parry
	}
}

function AnimationSettings.fetchMovementAnimation(Name)
	return AnimationSettings.MovementAnimations[Name]
end

function AnimationSettings.fetchWeaponAnimation(Name)
	return AnimationSettings.WeaponAnimations[Name]
end

return AnimationSettings

Example code of the use of the function in the main code:

Animations.fetchMovementAnimation("Idle").IsPlaying
1 Like

Did you require the module in the main code?

2 Likes

Yes, otherwise it wouldn’t be returning anything

2 Likes

image

2 Likes

Would using WaitForChild work? It could be that the Animation objects themselves have not loaded it yet, and the ModuleScript fired too early.

2 Likes

It may be that IsPlaying is exclusive to animations, and not animation tracks, hence why you wouldn’t find it in animation tracks.

2 Likes

WaitForChild inside of the module script or the main code?

2 Likes

Every Function and Property is nil

3 Likes

In the ModuleScript, for each animation.

2 Likes

I did think that this was going to work, because honestly I did forget about that but sadly it didn’t change much :frowning:

2 Likes

Oh, I read the error one more time and decided to check the docs. Animation and AnimationTracks are different, with AnimationTracks possessing IsPlaying.

Only then the said property will be accessible upon calling LoadAnimation, which returns an AnimationTrack.

See this.

2 Likes

That’s what I thought of, didn’t give me a different output

2 Likes

How did you set up your ModuleScript with LoadAnimation?

2 Likes

image
image

2 Likes

it’s 2 seperate screenshots, the bottom one is above

2 Likes

The LoadAnimation function itself returns an AnimationTrack which you should catch. And also, I don’t believe loading the Animations every time they are fetched is good practice. Try this (code untested):

local Ancestor = script.Parent
local MovementAnimations = Ancestor.MovementAnimations
local WeaponAnimations = Ancestor.WeaponAnimations

local Players = game:GetService("Players")

local Client = Players.LocalPlayer
local Client_Character = Client.Character or Client.CharacterAdded:Wait()
local Client_Humanoid = Client_Character:FindFirstChildWhichIsA("Humanoid")
local Client_Animator = Client_Humanoid:FindFirstChildWhichIsA("Animator")

local AnimationSettings = {
	["MovementAnimations"] = {
		["Idle"] = MovementAnimations.Idle,
		["Walk"] = MovementAnimations.Walk
	},

	["WeaponAnimations"] = {
		["Idle"] = WeaponAnimations.Idle,
		["Mouse1"] = WeaponAnimations.Mouse1,
		["Parry"] = WeaponAnimations.Parry
	}
}

local function loadAnimations(t)
	local Contents = {}
	
	for Name, Value in t do
		local Type = typeof(Value)
		
		if Type == "table" then
			Contents[Name] = loadAnimations(Value)
		elseif Type == "Instance" then
			if Value:IsA("Animation") then
				Contents[Name] = Client_Animator:LoadAnimation(Value)
			end
		end
	end
	
	return Contents
end

AnimationSettings = loadAnimations(AnimationSettings)

function AnimationSettings.fetchMovementAnimation(Name)
	return AnimationSettings.MovementAnimations[Name]
end

function AnimationSettings.fetchWeaponAnimation(Name)
	return AnimationSettings.WeaponAnimations[Name]
end

return AnimationSettings
2 Likes

image

2 Likes

There should be an error before that, showing the actual reason to the issue. Can you send again?

2 Likes

I lied. (Line 35)
image

2 Likes

They have asset ids on the instances, so a lil confused

2 Likes