Function to Return a metaTable Value prints Memory Adress Instead of Value

Hello!

I’m making a cart ride game for fun, But this is my first time trying to use MetaTables for something.
I think i’ve done everything Correctly, but when i call print in the :Init() function, it prints the value.
But if i return that same value in a function, or just plain try to print it in a different function in the metatable it returns the Memory Adress and Not the value.

I’m completely lost since to me metatables are still really confusing.

Here’s the ModuleScript:

local cartFolder = workspace.Map.Carts

local CollectionService = game:GetService("CollectionService")



-- || Module
local cartManager = {
	Tag = "Cart"
}

function cartManager:Init()
	local self = setmetatable({
		Tag = cartManager.Tag,
		
		List = CollectionService:GetTagged(self.Tag),
		Amount = #CollectionService:GetTagged(self.Tag),
	}, cartManager)

	print(self.List) -- Prints Amount
	print(self.Amount) -- Prints Amount

	-- || Setup Watch Connections for Cart Creation
	CollectionService:GetInstanceAddedSignal(self.Tag):Connect(function(newInstance :Instance)
		self.List[newInstance.Name] = {
			Owner = nil, 
			Model = newInstance,	
		}
	end)
	
	CollectionService:GetInstanceRemovedSignal(self.Tag):Connect(function(oldInstance :Instance)
		self.List[oldInstance.Name] = nil
	end)
	-- || End of Watch Connections
	
	return self
end

function cartManager:testPrint()
	return self.List
end

function cartManager:IsPlayerInCart(player :Player?)
	return self.List[self.Tag.. player.Name] or false
end

function cartManager:DestroyAll()
	for _, Cart :Model in pairs(cartFolder:GetChildren()) do
		Cart:Destroy()
	end
	
	self.List = {}
end

function cartManager:MakeCart(TargetLocation :CFrame, TypeOfCart :string)	
	local newCart :Model = script[TypeOfCart]:Clone()
	
	if newCart then
		newCart:PivotTo(TargetLocation)
		
		-- || Do This Last to Ensure Script Completion
		newCart:AddTag(self.Tag)
	end
	
	return newCart
end


-- || Cosmetic Functions
function cartManager:Amount()
	print(self.Amount) -- Prints Memory Adress
	return self.Amount
end

function cartManager:List()
	print(self.List) -- Prints Memory Adress
	return self.List
end

return cartManager

And Here’s the server script that calls the module, [this was just testing]:

local CollectionService = game:GetService("CollectionService")
local cartFolder = workspace.Map.Carts

local cart = require(script.Carts)
cart:Init()


cart:MakeCart(CFrame.new(-23.9415703, 2.32410991, 39.5611038, 1, 0, 0, 0, 0, -1, 0, 1, 0), "DefaultCart")

while task.wait(5) do
	print(cart:List()) -- Prints Memory Adress
	print(cart:Amount()) -- Prints Memory Adress
end

And Here’s the output:
image

Any help is greatly appreciated!

If you need any more information, let me know.
i’ll be happy to provide!

1 Like

i might have a fix for your script. So i patched some stuff up which might cause the problem.

local cartFolder = workspace.Map.Carts
local CollectionService = game:GetService("CollectionService")

-- || Module
local cartManager = {
	Tag = "Cart"
}

function cartManager:Init()
	local obj = {
		Tag = cartManager.Tag,
		List = CollectionService:GetTagged(self.Tag),
		Amount = #CollectionService:GetTagged(self.Tag),
	}
	setmetatable(obj, {__index = cartManager})

	print(obj.List) -- Prints List
	print(obj.Amount) -- Prints Amount

	-- || Setup Watch Connections for Cart Creation
	CollectionService:GetInstanceAddedSignal(self.Tag):Connect(function(newInstance)
		obj.List[newInstance.Name] = {
			Owner = nil, 
			Model = newInstance,	
		}
	end)
	
	CollectionService:GetInstanceRemovedSignal(self.Tag):Connect(function(oldInstance)
		obj.List[oldInstance.Name] = nil
	end)
	-- || End of Watch Connections
	
	return obj
end

function cartManager:testPrint()
	return self.List
end

function cartManager:IsPlayerInCart(player)
	return self.List[self.Tag.. player.Name] or false
end

function cartManager:DestroyAll()
	for _, Cart in pairs(cartFolder:GetChildren()) do
		Cart:Destroy()
	end
	
	self.List = {}
end

function cartManager:MakeCart(TargetLocation, TypeOfCart)
	local newCart = script[TypeOfCart]:Clone()
	
	if newCart then
		newCart:PivotTo(TargetLocation)
		newCart:AddTag(self.Tag)
	end
	
	return newCart
end

-- || Cosmetic Functions
function cartManager:GetAmount()
	print(self.Amount) -- Prints Amount
	return self.Amount
end

function cartManager:GetList()
	print(self.List) -- Prints List
	return self.List
end

return cartManager

Im not sure if this works or not, since im not that big of an expert at coding, but it might work. Lmk if it works!

1 Like

and by the way, i would recommend replacing

while task.wait(5) do
	print(cart:List()) -- Prints Memory Adress
	print(cart:Amount()) -- Prints Memory Adress

with

while true do
	print(cart:GetList()) -- Prints List
	print(cart:GetAmount()) -- Prints Amount
	task.wait(5)
1 Like

Thanks so much for helping!

Sadly, the script you’ve provided results in this output:
image

Would you happen to know why?

1 Like

The error message you’re encountering, “Requiring asset [assetID]”, suggests that there’s an issue with needing a specific asset in your script. This could be due to many reasons, such as the asset not being found, incorrect asset ID, or problems with loading the asset.

1 Like

Ill give you some tips to fix it.

  1. Verify Asset ID: Ensure that the asset ID 6738245247 is correct. Double-check the asset ID to ensure there are no typos or mistakes.
  2. Check Asset Existence: Make sure that the asset with the provided ID exists in your game. You can do this by searching for the asset in the Roblox website or in your game’s assets.
  3. Asset Loading: If the asset is a model or other resource that needs to be loaded, ensure that it is correctly loaded. Check if there are any errors or warnings related to loading the asset.
  4. Permissions: Ensure that the asset is accessible by your game. Check the asset’s permissions to ensure it’s set to public or accessible by your game.
  5. Network Issues: Sometimes, network issues can cause problems with asset loading. Ensure that your internet connection is stable and that there are no network-related issues.
  6. Script Logic: Review the logic in your script that requires the asset (6738245247). Ensure that the script is correctly referencing and using the asset.
  7. Error Handling: Implement error handling in your script to catch and handle any errors that occur during asset loading or usage. This can help provide more detailed information about the issue.
1 Like

This is a plugin, this is not related to the script.

The “Error” in this case, would be it printing “nil”

Even though both values are assigned, as proved by it printing Correctly in the first line :frowning:

1 Like

My script could be or is not correct. I’m not sure since im not that good at scripting.

1 Like

Try this!

local cartManager = {}
local cartFolder = workspace.Map.Carts
local CollectionService = game:GetService("CollectionService")
local Tag = "Cart"

-- || Module
local methods = {}
methods.__index = methods

function cartManager.new()
	local self = setmetatable({
		Tag = Tag,
		List = CollectionService:GetTagged(self.Tag),
		Amount = #CollectionService:GetTagged(self.Tag),
	}, methods)

	print(self.List) -- Prints Amount
	print(self.Amount) -- Prints Amount

	-- || Setup Watch Connections for Cart Creation
	CollectionService:GetInstanceAddedSignal(self.Tag):Connect(function(newInstance :Instance)
		self.List[newInstance.Name] = {
			Owner = nil, 
			Model = newInstance,	
		}
	end)
	
	CollectionService:GetInstanceRemovedSignal(self.Tag):Connect(function(oldInstance :Instance)
		self.List[oldInstance.Name] = nil
	end)
	-- || End of Watch Connections
	
	return self
end

function methods:testPrint()
	return self.List
end

function methods:IsPlayerInCart(player :Player?)
	return self.List[self.Tag.. player.Name] or false
end

function methods:DestroyAll()
	for _, Cart :Model in pairs(cartFolder:GetChildren()) do
		Cart:Destroy()
	end
	
	self.List = {}
end

function methods:MakeCart(TargetLocation :CFrame, TypeOfCart :string)	
	local newCart :Model = script[TypeOfCart]:Clone()
	
	if newCart then
		newCart:PivotTo(TargetLocation)
		
		-- || Do This Last to Ensure Script Completion
		newCart:AddTag(self.Tag)
	end
	
	return newCart
end


-- || Cosmetic Functions
function methods:Amount()
	print(self.Amount) -- Prints Memory Adress
	return self.Amount
end

function methods:List()
	print(self.List) -- Prints Memory Adress
	return self.List
end

return cartManager
1 Like

Thanks for responding!

I’ve implemented this but it’s resulting in the exact same behavior provided by @Kynsur’s script. :frowning:

Here’s the ouput, [I’ve also printed self, That’s the table you see]:

My Altered script:

local cartManager = {}
local cartFolder = workspace.Map.Carts
local CollectionService = game:GetService("CollectionService")
local Tag = "Cart"

-- || Module
local methods = {}
methods.__index = methods

function methods.new()
	local self = setmetatable({
		Tag = Tag,
		List = CollectionService:GetTagged(Tag),
		Amount = #CollectionService:GetTagged(Tag),
	}, methods)

	print(self.List) -- Prints Amount
	print(self.Amount) -- Prints Amount

	-- || Setup Watch Connections for Cart Creation
	CollectionService:GetInstanceAddedSignal(Tag):Connect(function(newInstance :Instance)
		self.List[newInstance.Name] = {
			Owner = nil, 
			Model = newInstance,	
		}
	end)

	CollectionService:GetInstanceRemovedSignal(Tag):Connect(function(oldInstance :Instance)
		self.List[oldInstance.Name] = nil
	end)
	-- || End of Watch Connections

	return self
end

function methods:IsPlayerInCart(player :Player?)
	return self.List[Tag.. player.Name] or false
end

function methods:DestroyAll()
	for _, Cart :Model in pairs(cartFolder:GetChildren()) do
		Cart:Destroy()
	end

	self.List = {}
end

function methods:MakeCart(TargetLocation :CFrame, TypeOfCart :string)	
	local newCart :Model = script[TypeOfCart]:Clone()

	if newCart then
		newCart:PivotTo(TargetLocation)

		-- || Do This Last to Ensure Script Completion
		newCart:AddTag(Tag)
	end

	return newCart
end


-- || Cosmetic Functions
function methods:GetAmount()
	print(self.Amount) -- Prints Nil
	
	print(self)
	return self.Amount
end

function methods:GetList()
	print(self.List) -- Prints Nil
	return self.List
end

return methods
1 Like

It looks like the issue may be caused by the script that calls the module script. I noticed that the variable “cart” is assigned to the module script itself. You then call the Init function after that, but you never assign its return value to another variable. As a result, inside of the while-loop, the code tries to return something that doesn’t exist, as the “cart” variable is just a reference to the module script. To potentially resolve this issue, I would assign the return value from the Init function to a new variable and then use that variable inside the while-loop.

1 Like

so


local returnvalue = self.List

return returnvalue

?

1 Like

Something like this:

local CollectionService = game:GetService("CollectionService")
local cartFolder = workspace.Map.Carts

local cart = require(script.Carts)

local test = cart:Init() -- new variable

cart:MakeCart(CFrame.new(-23.9415703, 2.32410991, 39.5611038, 1, 0, 0, 0, 0, -1, 0, 1, 0), "DefaultCart")

while task.wait(5) do
	print(test:List()) -- Prints Memory Adress
	print(test:Amount()) -- Prints Memory Adress
end

Or you could do this too:

local CollectionService = game:GetService("CollectionService")
local cartFolder = workspace.Map.Carts

local cart = require(script.Carts):Init()

cart:MakeCart(CFrame.new(-23.9415703, 2.32410991, 39.5611038, 1, 0, 0, 0, 0, -1, 0, 1, 0), "DefaultCart")

while task.wait(5) do
	print(cart:List()) -- Prints Memory Adress
	print(cart:Amount()) -- Prints Memory Adress
end

I’ll try this and let you know!
thanks!

1 Like

update, it has still errored:

apparently it’s just a table now?
here i’ve printed the table, it’s showing the variables:

1 Like

It’s WORKING!!!

Doing your method of using :Init() and adding this line to the module:
image

It’s printing them now! thank you so much!

2 Likes

And Here’s the output:

Its look like is resolved, but try next time in the windows output, uncheck or check log mode, or some config (in the three dots), one of them was making me unable to print tables and instead print memorys adress

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