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
},
}
}
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)
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)