Referencing ModuleScript

Hey, could I get some help knowing if I have correctly referenced the indexes in the ModuleScript? It prints out as nil.

Local Script:

local valuesTable = lawsettings.lawsettings
local currentIndex = 1

Forward.MouseButton1Click:Connect(function()
	

	local currentValue = valuesTable[currentIndex]
	
	print(currentValue)
	currentIndex = currentIndex + 1
	
	if currentIndex > #valuesTable then
		currentIndex = 1
	end
	
end)

Module Script:

return {
	lawsettings = {
		["Law of Preservation"] = {
			MaxWarning = 0,
			Time = 300
		},
		["Jedi Force Etiquette"] = {
			MaxWarning = 0,
			Time = 300
		},
		["Law of Veneration"] = {
			MaxWarning = 1,
			Time = 300
		},
		["Law of Misconduct"] = {
			MaxWarning = 1,
			Time = 300
		},
		["Law of Territory"] = {
			MaxWarning = 1,
			Time = 300
		},
		["Law of Gathering"] = {
			MaxWarning = 2,
			Time = 300
		},
		["Law of Formality"] = {
			MaxWarning = 1,
			Time = 300
		},
		["Law of Absence"] = {
			MaxWarning = 0,
			Time = 300
		},
		["Law of Compliance"] = {
			MaxWarning = 0,
			Time = 300
		},
		["Hostile Etiquette"] = {
			MaxWarning = 1,
			Time = 300
		},
		["Law of Authority"] = {
			MaxWarning = 1,
			Time = 300
		},
		["Law of Safeguarding"] = {
			MaxWarning = 1,
			Time = 300
		},
		
	}
}

You have to use ‘require’ when you want to get the data out of a module script. Referencing it wont let you access it’s data

If lawsettings is a ModuleScript, you’ll need to require it first.

Modified version:

local lawsettings = require(Path.To.LawSettings);
local valuesTable = lawsettings.lawsettings
local currentIndex = 1

Forward.MouseButton1Click:Connect(function()
	

	local currentValue = valuesTable[currentIndex]
	
	print(currentValue)
	currentIndex = currentIndex + 1
	
	if currentIndex > #valuesTable then
		currentIndex = 1
	end
end)
1 Like

Hi, thanks for your reply. I have required it, but it still doesn’t work.

What is your module script named? Where is it stored? We need more info other than the snippet you’ve showed us.

Try adding print() at different parts of the code to make sure it even reaches there.

The Forward button function words and prints out fine. I just need to know how to flip through a table when the button is clicked.
image

image

You’re trying to index strings with number.

1 Like

alright, so how would i rewrite it?

Instead of numbers try indexing strings like valuesTable['Law of Preservation']

return {
	lawsettings = {
		[0] = {
			Name = "Law of Preservation",
			MaxWarning = 0,
			Time = 300
		},
		[1] = {
			Name = "Jedi Force Etiquette",
			MaxWarning = 0,
			Time = 300
		},
	}
}
2 Likes

Well, one thing you could do is rewrite your lawsettings table so that the index is a number. That means you’d have to change it to look like this:

{
         Name = "Law of Preservation"
			MaxWarning = 0,
			Time = 300
}

If you don’t want to do this, there is another way.

At the top of your script, you can assign a number to every string index in the form over a “number to index lookup”

That would look something like this:

local LawIndexLookup = {}

local Counter = 0
for Index, _ in valuesTable do
   Counter += 1
   LawIndexLookup[Counter] = Index
end

Then you can use this index as so:

local currentCounter = 1

Forward.MouseButton1Click:Connect(function()
	

	local currentIndex = LawIndexLookup[currentCounter]
    local currentValue = valuesTable[currentIndex]
	
	print(currentValue)

	currentCounter = currentCounter + 1	
	if currentCounter > Counter then
		currentCounter = 1
	end
end)
2 Likes

^^ I’d prefer the second method

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