Returning a useable Instance from a Module

Hello fellow Roblox Game Developers

I am currently trying to use multiple instances referenced inside of a module. The instances are in a table and when I use a function in my main script to get one of them, it does not seem to have any properties or functions or just seems to be in-existent. Never has returned nil, but the properties and functions have.

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"][self.Name]
end

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

return AnimationSettings

The self keyword requires a metatable. Using : is only for Object Oriented Programming. What I believe you are trying to do is this:

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

I will try it. But i believe it didn’t work either way.

1 Like

Nothing changed, functions still return null :frowning:

1 Like

Let me see the script calling the modulescript

local Ancestor = script

local Animations = require(Ancestor:WaitForChild("AnimationSettings"))

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local Events = ReplicatedStorage.Events
local Events_ServerEvents = Events.Server:GetChildren()

local Client = Players.LocalPlayer
local Client_Character = Client.Character or Client.CharacterAdded:Wait()
local Client_Humanoid = Client_Character:WaitForChild("Humanoid")
local Client_Animations = Client_Humanoid:GetPlayingAnimationTracks()

local Tick = tick()

Client_Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if Client_Humanoid.MoveDirection.X ~= 0 or Client_Humanoid.MoveDirection.Z ~= 0 then
		if Animations.fetchMovementAnimation("Idle").IsPlaying then
			Animations.fetchMovementAnimation("Idle"):Stop()
		end
		
		if not Animations.fetchMovementAnimation("Movement").IsPlaying then
			Animations.fetchMovementAnimation("Walk"):Play()
		end
	else
		if Client_Humanoid.MoveDirection.Y ~= 0 then return end
		    Animations.fetchMovementAnimation("Walk"):Stop()
	        Animations.fetchMovementAnimation("Idle"):Play()
	end
end)
1 Like

Actually, have you made sure that the animations exist? Try doing this:

print(AnimationSettings)

return AnimationSettings

Also, I don’t know if a modulescript is really necessary for only two rather simple functions. I’d personally either make the entire animation controller a modulescript or just add those functions into the script you sent above.

You are trying to pass Name as self.
function AnimationSettings:fetchMovementAnimation(Name)
return AnimationSettings[“MovementAnimations”][Name]
end

function AnimationSettings:fetchWeaponAnimation(Name)
return AnimationSettings[“WeaponAnimations”][Name]
end