Table Contents Returning Nil?

Hi Other Developers,
I’ve been running into this issue where when I get a table from a Module Script and then try to take contents out of that table it doesn’t work.

Here is my Code:

PetPlaceHolder.PetImageButton.Activated:Connect(function()
	local PetStats = MainModuleScript.Eggs.Basic[PetPlaceHolder.Name]
	local PetMulti = PetStats
	PetImageHolder.Image = PetPlaceHolder.PetImageButton.Image
		
	PetPlaceHolderInfo.InfoBox.ClickXLabel.Text = "Click Multipler: x" .. tostring(PetMulti)
	PetPlaceHolderInfo.InfoBox.RebirthXLabel.Text = "Rebirth Multipler: x" .. tostring(PetStats.RebirthMultiplier)
	PetPlaceHolderInfo.InfoBox.GemXLabel.Text = "Gem Multipler: x" .. tostring(PetStats.GemMultiplier)
end)

Here is the Table that variable PetStats prints out:
image

ModuleScript the code is retrieving from:

local MainModule = {
	Eggs = {
		Basic = {
			Common = {
				PetStats = {
					ClickMultiplier = 1.2,
					RebirthMultiplier = 1.05,
					GemMultiplier = 1,
				}
			},
		}
	}
}

return MainModule

Thank you!

1 Like

I think it makes sense because you assigned a table to the PetStats variable and then PetStats to PetMulti (Still a table) and after that, you are converting that table to string, which of course will return Nil.

needs to be changed to require (because it’s a module script)


local PetStats = require(MainModuleScript).Eggs.Basic[PetPlaceHolder.Name]

I would do this though

local MainModuleScript = require(PATHWAY TO MODULE SCRIPT like script.Parent.ModuleScript) --makes module script accessible PUT THIS AT LINE 1

--later in the script:
local PetStats = MainModuleScript.Eggs.Basic[PetPlaceHolder.Name]

I set the Modulecript variable in a different place like above it.

tostring(PetStats.RebirthMultiplier)

is not a table, but I dont know why its returning nil

After you reference basic you just have (pet placeholder name) instead of .Common.PetStats which means the wrong table is being accessed. Access it exactly how it says

Did you tried updating this line? I believe it should be

PetPlaceHolderInfo.InfoBox.ClickXLabel.Text = "Click Multipler: x" .. tostring(PetStats.ClickMultiplier)

Yes i’ve tried that but it still returns nil

Additionally you can concatenate strings and numbers without tostring()

Can you show your module script? I don’t think that should return nil if you are directly accessing the module script table values

yes its above,
maybe this will help?

local function AddPetInventoryBasic(Type, AssetID, Amount)

	if PetIndex >= 24 then return end

	local PetPlaceHolder = ScrollingPetInv["PetPlaceHolder" .. PetIndex + 1]
	PetPlaceHolder.PetImageButton.Image = AssetID
	PetPlaceHolder.Name = Type

	PetIndex += 1
	PetPlaceHolder.PetImageButton.Activated:Connect(function()
		local PetStats = MainModuleScript.Eggs.Basic[PetPlaceHolder.Name]
		local PetMulti = PetStats
		print(PetStats["ClickMultiplier"], PetStats, Type)
		
		print("Pet Info Shown")
		PetImageHolder.Image = PetPlaceHolder.PetImageButton.Image
		print(PetMulti)
		
		PetPlaceHolderInfo.InfoBox.ClickXLabel.Text = "Click Multipler: x" .. tostring(PetMulti)
		PetPlaceHolderInfo.InfoBox.RebirthXLabel.Text = "Rebirth Multipler: x" .. tostring(PetStats.RebirthMultiplier)
		PetPlaceHolderInfo.InfoBox.GemXLabel.Text = "Gem Multipler: x" .. tostring(PetStats.GemMultiplier)
		
	end)
end

Its the same code but with more code

PetPlaceHolderInfo.InfoBox.ClickXLabel.Text = "Click Multipler: x" .. tostring(PetMulti)

So far, I am pretty sure that the error should be on this line because you have clearly attempted to convert a table to string.

oh whoops, I changed it but it still returns nil
Earlier I was testing it around I think I forgot to change it back but its still nil mb

The table data is accessed via MainModule.Eggs.Basic.Common.PetStats[key] not MainModule.Eggs.Basic[PetPlaceholder.Name]

1 Like

Oh yeah exactly, It should be:

local PetStats = MainModuleScript.Eggs.Basic[PetPlaceHolder.Name]["PetStats"]

1 Like

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