Table indexing issue

-- module script excerpt 1

local TS = game:GetService("TweenService")
local ToolM = require(game.ReplicatedStorage.ToolsM)

local function NewItemFrame(Item)
	local NewItem = script.HotBarFrame:Clone()
	print(ToolM.TColors)
	NewItem.Image = "rbxassetid://"..tostring(ToolM.ImageIds[Item])
	NewItem.Frame.BackgroundColor3 = ToolM.TColors[Item]["First"]  -- ERROR LINE BEGINS HERE
	NewItem.ImageF.TextLabel.Text = "1x"
	NewItem.ImageF.TextLabel.TextColor = BrickColor.new(ToolM.TColors[Item].Second)
	NewItem.ImageLabel.ImageColor3 = ToolM.TColors[Item].First
	NewItem.Name = Item
	game:GetService("CollectionService"):AddTag(NewItem, "Draggable")
	return NewItem
end
-- module script 2 excerpt

local Tool = {}

Tool.Rarities = {		
	["Trowel"] = 30,
	["Kunai"] = 25,
	["Decelerater"] = 25,
	["SensorBeam"] = 20
}
Tool.ImageIds = {
	["Trowel"] = 96741105749397,
	["Kunai"] = 98251556478136,
	["Decelerater"] = 117610669648736,
	["SensorBeam"] = 86224704250476
}
Tool.TColors = {
	["Trowel"] = {
		["First"] = Color3.fromRGB(255, 179, 85),
		["Second"] = Color3.fromRGB(230, 169, 85)
	},
	["Kunai"] = {
		["First"] = Color3.fromRGB(0, 0, 0),
		["Second"] = Color3.fromRGB(255, 84, 84)
	},
	["Decelerater"] = {
		["First"] = Color3.fromRGB(146, 255, 210),
		["Second"] = Color3.fromRGB(146, 255, 210)
	},
	["SensorBeam"] = {
		["First"] = Color3.fromRGB(119, 162, 255),
		["Second"] = Color3.fromRGB(70, 121, 176)
	}
}

Error output:

 ReplicatedStorage.PlayerM.Hotbar:10: attempt to index nil with 'First'

the whole thing isnt complicated at all but the reason why this doesnt work seemly makes 0 sense to me. any help would be great.

Are you sure that Item is a string?

2 Likes

Can you do print(Item) in first module

1 Like

my only guess would be that the Item variable isnt inside of the table.

So too ensure its there, add a print that prints the variable itself, and then prints Item inside of the Tool.TColors.

I presume it was probably cause of a typo.

2 Likes

Make sure Item is a string, and is part of your TColors table

ToolM.TColors[Item]

Also, could we see the line in the module script where it returns the table?

1 Like

@Tomi1231 @FroDev1002 @azqjanna @xcreatee

print(Item, ToolM.TColors[Item]) -- Trowel, nil

for reference heres the table agaim

Tool.TColors = {
	["Trowel"] = {
		["First"] = Color3.fromRGB(255, 179, 85),
		["Second"] = Color3.fromRGB(230, 169, 85)
	},
	["Kunai"] = {
		["First"] = Color3.fromRGB(0, 0, 0),
		["Second"] = Color3.fromRGB(255, 84, 84)
	},
	["Decelerater"] = {
		["First"] = Color3.fromRGB(146, 255, 210),
		["Second"] = Color3.fromRGB(146, 255, 210)
	},
	["SensorBeam"] = {
		["First"] = Color3.fromRGB(119, 162, 255),
		["Second"] = Color3.fromRGB(70, 121, 176)
	}
}
1 Like

Have you tried using arrays
If there is no particular order when indexing Tool.TColors then try using array
This should directly access the index instead of finding the key with strings.

Tool.TColors = {
	["Trowel"] = {
		[1] = Color3.fromRGB(255, 179, 85),
		[2] = Color3.fromRGB(230, 169, 85)
	},
	["Kunai"] = {
		[1] = Color3.fromRGB(0, 0, 0),
		[2] = Color3.fromRGB(255, 84, 84)
	},
	["Decelerater"] = {
		[1] = Color3.fromRGB(146, 255, 210),
		[2] = Color3.fromRGB(146, 255, 210)
	},
	["SensorBeam"] = {
		[1] = Color3.fromRGB(119, 162, 255),
		[2] = Color3.fromRGB(70, 121, 176)
	}
}

Indexing using string seems redundant anyways if you are going to name them “First” and “Second”

i know you gave an excerpt, and the answer might be obvious, but just to make sure

did you put return Tool at the end of module script 2?

1 Like

I dont think thats possible considering that the error is on line 10, if a module doesnt return anything, any script requiring it breaks at the line it was required at.

That and they are using other things before line 10 that use the ToolM module.

Still is a possibility, just not sure if its right.


Anyhow this is really odd. I tried the exact same thing you did, line for line, and it worked just fine for me.

Only thing I can think of that’d be happening is that at some point you set ToolM.TColors to something else, or remove said thing from the TColors table.

Also when you prints ToolM.TColors I assume it prints the TColors correct?

Can you show us the result of

print(typeof(Item))

If Item is an instance of some sort, its name will be printed, making it look like a string

1 Like

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