Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Requiring a table and printing any value in the table prints nil?

So I'm making a lighting system with 5 preset colours that are editable by the user in studio via a settings module script. Just to test to make sure it works, I set up this bit of code;

local Settings = require(script.Settings)
print(Settings[1])

Whenever I run this code, it returns nil. My table does not list any of my values as nil, and I have tried inside the modulescript to print the first value, and it works fine. My module is as follows;

local module = {}

local table_PresetColours = {
'255,255,255',
'255,255,252',
'255,255,255',
'255,255,255',
'255,255,255',	
}
print(table_PresetColours[1])


return module

How can I fix this?

1 answer

✓ Accepted answer

You are doing it wrong atm you are return an empty array

I modified it to return a function that returns your array

local module = {}

function module.GetPresetColours()
	local table_PresetColours = {
		'255,255,255',
		'255,255,252',
		'255,255,255',
		'255,255,255',
		'255,255,255',  
	}
	return table_PresetColours
end

return module

after requiring you get it like this

local Settings = require(script.Settings)
print(Settings.GetPresetColours()[1])

see this article about modules

1 comment

1

You don't even need to make it a function, you can just use a metamethod or just make it a module variable.

This discussion is preserved as it was. New answers, comments, and votes are closed.