Trying to read dictionary. getting error attempt to index nil with nil

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    how to ready this dictionary
  2. What is the issue? Include screenshots / videos if possible!
    error report on line of reading dictionary. i have read dictionaries before don’t know why this is not working
local resourceN = _G.BuildingInfo[season][build]["Resource"] -- error on this line
-- the dictionary
_G.BuildingInfo = {
	Basic = {
		House = {
			Name = "House",
			Health = 100,
			Cost = 50,000,
			Resource = "BasicCoin"
		}
	}
}


I’m going to assume that the variables season and build are both nil at that point in the script. I would suggest you look to try and identify why they are nil

You should not be using _G. Unless your codebase follows SSA (single script architecture) or any other architecture that invites a single entry-point, the order of execution of the scripts in your codebase is not guaranteed. The script you’re accessing this extension of _G from is likely executing before the script you’ve presented to us, leaving that extension of _G non-existent. This is one of the flaws of _G that is only solved by a major and imposing structural change to your codebase. Furthermore, _G’s contents cannot be known as the table can be written to from anywhere throughout the codebase. This leads to further race conditions and absent type annotations (auto-completion). You should be using a ModuleScript to implement globally accessible content for your codebase

Additionally, the following is a syntax error in your code. Should your script end up running before those that access _G, this will cause your extension of _G to fail to be completed as an error will be raised:

Cost = 50,000,

If your goal was to improve clarity, you can use the following formatting:

-- Legal syntax; do not be alarmed by absent syntax highlighting.
Cost = 50_000

there in a module script now same error. also before the line runs in the old code it runs a dictionary search for a _G. , Do you have any other ideas what wrong?

local GameInfo = {}

BuildingInfo = {
	Basic = {
		House = {
			Name = "House",
			Health = 100,
			Cost = 50000,
			Resource = "BasicCoin"
		}
	}
}

function GameInfo.getResource(s, b)
	v = BuildingInfo[s][b]["Resource"]
	return v
end

return GameInfo

i am using function with the variables caried like function(season, build)

at some point in your function, you are calling the function without the required parameters, or are trying to pass a value that is nil into the function

This code is functional. I just copy-pasted it into a ModuleScript named GameInfo, and parented it to a Script that calls it like so:

local gameInfo = require(script:FindFirstChild("GameInfo"))
print( gameInfo.getResource("Basic","House"))

It prints:

  16:39:48.774  BasicCoin  -  Server - Script:3

Your problem must be that your calling code is calling the function with a key that is not in your dictionary. Set a breakpoint where the function is called, or just print the arguments being passed into it to see what’s wrong.

You were right i found it. i must have error some where while editing. i had two variables in the function when i was supposed to be reading 4 variables.

Now i have a function building placement system that goes through a purchase process. congrats to me. (placement of building was working before, the errors where in the purchase part). thanks for the help

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