Script prints out nil

So I am trying to get the number of values in this dictionary/table but I can’t, so I just put the number to 1000 and would check if it is nil. Sadly, it is nil and I have no clue why.

I need this to create a textbutton for every value in “Ducks” so the player can check what badges they need and so on.

1st script (Nil error)

local System = require(game:GetService("ReplicatedStorage"):FindFirstChild("MainModule"))
local Info = System.Ducks
local BadgeService = game:GetService("BadgeService")

local function getIcon(BadgeId)
	local success, badge = pcall(function()
		return BadgeService:GetBadgeInfoAsync(BadgeId)
	end)

	if success then
		return badge
	else

	end
end

for i = 1, 1000 do
	print("1")
	print(Info[1]) --           It prints nil :((((((((((((
	if Info[i] ~= nil then
		print("2")
		local badge = getIcon(Info[i].BadgeId)
		local Button = script.ImageButton:Clone()
		Button.Name = Info[i]
		Button.TextLabel.Text = Info[i].Name
		pcall(function()
			Button.Image = "rbxassetid://"..badge
			Button.Parent = script.Parent.Parent.Parent
		end)
	else
		break
	end
end

2nd mainmodule:

local Ducks = {}

local BadgeService = game:GetService("BadgeService")

Ducks["Ducks"] = {
	
	["Duck"] = {
		Name = "Duck";
		Description = "Your average duck. :)";
		Hint = "At spawn";
		Stars = 1;
		Color = {255, 255, 0};
		BadgeId = 2124823088;
	},
	
	["PirateDuck"] = {
		Name = "Pirate Duck";
		Description = "Weird, eh?";
		Hint = "Near the seas";
		Stars = 1;
		Color = {0, 0, 0};
		BadgeId = 2124823089;
	},
	
	["DominusDuck"] = {
		Name = "Dominus Duck";
		Description = "A very rich duck!";
		Hint = "Up in the sky";
		Stars = 3;
		Color = {189, 243, 255};
		BadgeId = 2124824184;
	},
	
}

Ducks["StarSystem"] = {
	
	["1"] = {
		Info = "Easy difficulty";
	},
	
	["2"] = {
		Info = "Normal difficulty";
	},
	
	["3"] = {
		Info = "Hard difficulty";
	},
	
	["4"] = {
		Info = "Insanely hard difficulty";
	},
	
	["5"] = {
		Info = "0.05% of people can find these ducks";
	},
}

I don’t think I am doing this correctly. Please help!

Try this instead:

local System = require(game:GetService("ReplicatedStorage"):WaitForChild("MainModule"))
local Info = System.Ducks
1 Like

Try print(Info[“Duck”])

This should work i am not entirley sure since its formated strange.

1 Like

This works but I want to loop through every value in it so its easier for me to create more ducks.

In module scripts, you need to return something, and from what I can infer you’re not returning anything. To fix your problem all you have to do is simply add a return Ducks to the end of the module and instead of referencing Info as System.Ducks, it’ll simply just be System as Ducks is the returned value.

I have this inside of my mm script, I just never showed it because I have a award function before it.

Nope, nothing yet. Seems to act the same when I do :FindFirstChild

print this instead and see if there’s difference

print(Info.Duck) 

If so what it means is the default index is a string so when u index it with its default it would be nil.

1 Like

It prints nil even when I put the # infront of it, but when I put a # it says it can’t get the length of nil.

I see, change the variable value from this:

to:

Ducks.Ducks = {

Or from:

To:

local Info = System["Ducks"]

Just to see if there’s any difference when the variables are excatly the same.

1 Like

Sadly, nope. Still printed nil.

Hey,
You could put a wait at the start of the script, before the i = 1, 1000 thing:

local System = require(game:GetService("ReplicatedStorage"):FindFirstChild("MainModule"))
local BadgeService = game:GetService("BadgeService")

local function getIcon(BadgeId)
	local success, badge = pcall(function()
		return BadgeService:GetBadgeInfoAsync(BadgeId)
	end)

	if success then
		return badge
	else

	end
end

repeat wait() until System.Ducks[1] --   Add this in!
local Info = System.Ducks --This might help as well


for i = 1, 1000 do
	print("1")
	print(Info[1]) --           It prints nil :((((((((((((
	if Info[i] ~= nil then
		print("2")
		local badge = getIcon(Info[i].BadgeId)
		local Button = script.ImageButton:Clone()
		Button.Name = Info[i]
		Button.TextLabel.Text = Info[i].Name
		pcall(function()
			Button.Image = "rbxassetid://"..badge
			Button.Parent = script.Parent.Parent.Parent
		end)
	else
		break
	end
end

I dont use module scripts at all, so I may be completely wrong.
BUT it may be worth a try
Addtionally, you could try re-stating the value of ducks after the wait

1 Like

I don’t think its because i’m not waiting for it.

Just tested it and it won’t stop looping.
I’m starting to think its because I have a table inside of a table.

Ill test the scripts then get back to you

1 Like

If that were the case it would print the table not nil

(I don’t know.) I feel like it’s a studio bug. Have you tried restarting studio?

Also just wanna test one more thing:

print the System variable

local System = require(game:GetService("ReplicatedStorage"):FindFirstChild("MainModule"))

print(System) 

local Info = System.Ducks

Just to see if it actually requires the module.

1 Like

It does require the module because when I get the table name it works perfectly fine.

Not a studio bug, tested it in game aswell. :frowning:

1 Like

Also might I ask, Is the Info Variable ~= nil? if not you might wanna add a for loop instead of indexing.