How do I search for a variable name in a module?

I’m trying to look for a variable’s value in a module, heres a snippet of my code to explain what I mean.

So, I’m trying to look for the ID of a block the player inputs. So when this function is called, it will search the BlockIDs module to find the ID of the block we input.

For example, if I inputted BlockID.BLOCK_AIR, it would print “0” because in the BlockID module, BLOCK_AIR = 0

But, instead of searching for what it’s supposed to, it’s searching for the variable called “ID” instead of the actual one.

local placement = {}

local blocks = require(script.Parent.Blocks)
local blockIDs = require(script.Parent.BlockIDs)

function placement.setBlockProperties(ID)
	local block = blocks[blockIDs.ID]
	print(block)
end

return placement

I don’t want it to search for “ID,” as that’s just a variable for what the player inputted, which may be BLOCK_AIR

1 Like

Could you show how the tables in the modules looks like?

If the table in the module says

BLOCK_AIR = 0

And you try to print the value of that key BLOCK_AIR is not printing 0 ?

How the player send that input text? BlockID.BLOCK_AIR ? you are using string formatting to get the characters from the period?

I tried to print blockIDs.BLOCK_AIR and it worked, but when I tried to do it using the variable ID it didn’t work

If ID is the key, you can just use square brackets when indexing the blockIDs module:

function placement.setBlockProperties(ID)
	local block = blocks[blockIDs[ID]]
	print(block)
end
1 Like

So you mean, that your module looks something like this? you mean variables in the module?

local blockIDs = {}

blockIDs.BLOCK_AIR = 0
blockIDs.BLOCK_WATER = 1

return blockIDs

First, why not using tables?

local blockIDs = {}

blockIDs.Blocks = {
	["BLOCK_AIR"] = 0,
	["BLOCK_WATER"] = 1,
}

return blockIDs

In that way with the function on the script you can easily find the key of each element in the table.

In that way you could merge both modules too in the same module, the:

local blocks = require(script.Parent.Blocks)
local blockIDs = require(script.Parent.BlockIDs)

And when your function looks for the key I dont see any problem to find it, or perhaps im really understanding your issue

No, its searching for the word ID in the blockIDs module, instead of searching for what the “ID” variable would be

Finally understood your issue, then do what @ProgrammingRottie told you.
And check what I meant too, its improvement

Nvm. It’s printing nil for some reason.

SetBlockProperties code:

local placement = {}

local blocks = require(script.Parent.Blocks)
local blockIDs = require(script.Parent.BlockIDs)

function placement.setBlockProperties(ID)
	local block = blocks[blockIDs[ID]]
	print(block)
end

return placement

BlockIDs code:

local blockIDs = {}

blockIDs.Blocks = {
	["BLOCK_AIR"] = 0,
	["BLOCK_STONE"] = 1,
	["BLOCK_GRASS"] = 2,
	["BLOCK_DIRT"] = 3,
	["BLOCK_COBBLE"] = 4,
	["BLOCK_OAK_LOG"] = 5,
	["BLOCK_OAK_PLANKS"] = 6,
	["BLOCK_OAK_LEAVES"] = 7,
	["BLOCK_SNOW"] = 8,
}
	
return blockIDs

normal script

local blocks = require(game.ReplicatedStorage.BlockPlacement)

blocks.setBlockProperties(1)

image

local placement = {}

local blockIDs = require(script.Parent.blockIDs) -- the right name of your module?

function placement.setBlockProperties(ID)
	local block = blockIDs.Blocks[ID] -- now the table is different since you used the table approach
	print(block)
end

return placement
1 Like

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