How can i get the table from my module script

Basically i’m really new to oop and i created a system where i store all of the pets properties in the oop like adding the multiplier, name and model in there but i don’t know how i can get that table or pet from another script

local Pets = {}
Pets.__index = Pets

function Pets.new(Name, Model, Multiplier)
	local NewPet = {}
	setmetatable(NewPet, Pets)
	
	NewPet.Name = Name
	NewPet.Model = Model
	NewPet.Multiplier = Multiplier
	
	return NewPet
end

newpet = Pets.new("Cat", script.Pets.Cat, 2)

return Pets

Help is appreciated😁

1 Like

You can just say:

return Pets.new("Cat", script.Pets.Cat, 2)

Which would return the metatable from the module

I you meant obtaining this value for a Script, use require() and Specify which ModuleScript you want to be required

local ModuleScript = Example.ModuleScript

local Data = require(ModuleScript)

print(Data)
1 Like

i’m supposed to have like 40+ pets in there and i can’t return multiple things

oof, but:

Going off of this, BindableEvents would be useful for Script to Script Communication with Data under the same Context Level.

I’m a bit confused on what you’re planning to do with the object returned, but from what I can understand you’d probably be able to use a ModuleScript which contains all the players’ data (example: the key is set to the Player object, and the value is the data stored)

So then, when you need to access the data, you can do something like this:

local playerData = PlayerDataModule[Player]
for _, pet in playerData.Pets do
	-- pet is the object made by the module
end

And for storing, you could have a Players.PlayerAdded connection such as this:

local function onPlayerAdded(player: Player)
	local playerData = {
		Pets = {}
	}

	-- do something to initialize the playerData table and put the Pet objects in
	-- playerData.Pets

	PlayerDataModule[player] = playerData
end

Just make sure to remove the entry from the module once player leaves… It’ll leak memory otherwise

You are supposed create instances of the class outside the module and add them to some sort of table or array.

local Pet = require(...)

local PetsMap = {}
PetsMap[player] = {
    Pet.new(...),
    Pet.new(...)
}