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

Issue with my array within my module script, it returns the value as nil as if the table is empty?

The problem i'm having here is the array "pad_vacant_preset" is empty even though its evidently storing 2 values.

local module = {}

t1 = {}
t2 = {}

padnames = {"one","two","three"}

pad_in_use_preset = {
    brickcolor = BrickColor.new("Royal purple"),
	material = "Neon"
}
pad_vacant_preset = {
	brickcolor = BrickColor.new("Medium stone grey"),
	material = "Plastic"
}


function module:setpads()		
	local pads = script.Parent.Parent:FindFirstChild("Pads")
	for i,pad in pairs(pads:GetDescendants()) do
		if table.find(padnames, pad.Name) and pad:IsA("Part") then
			print(pad.Name)
			pad.BrickColor = self.pad_vacant_preset[1]
			
		end
	end
	return("pads are set")
end








return module
****

1 comment

0

^^ I dont have an issue with requiring it and running setpads() its just the arrays

1 answer

when you define a variable in a ModuleScript, that variable isn't automatically made part of the module—it's just like any other variable in any other script

if you want to be able to access it using self then you have to explicitly make it a value in the module, since the "module" is just like any other table

module.padnames = {"one","two","three"}

module.pad_in_use_preset = {
    brickcolor = BrickColor.new("Royal purple"),
    material = "Neon"
}
module.pad_vacant_preset = {
    brickcolor = BrickColor.new("Medium stone grey"),
    material = "Plastic"
}

alternatively you could just...not use self

also, if you didn't already know how self works, it's implicitly passed as the first argument to functions which are defined like methods, such as tbl:method

function tbl:method(arg1, arg2)
	print(self, arg1, arg2)
end
-- is the same as
function tbl.method(self, arg1, arg2)
	print(self, arg1, arg2)
end
-- which is also the same as
local function method(self, arg1, arg2)
	print(self, arg1, arg2)
end
tbl.method = method
-- except not really because the last one creates a variable for the method as well, but you get the point

when you call a function using : instead of a dot or just...nothing at all, lua passes the table (or """datum""" if you want to get technical, since values besides tables can have methods called from them using metatables) that you called the function from as the first argument

tbl:method(1, 2)
-- is the same as
tbl.method(tbl, 1, 2)
-- which is also the same as
local method = tbl.method
method(tbl, 1, 2)
-- except not really for the same reason as the first example

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