Why is does number keep coming out nil?

I have a settings module, and a createpart module.

The createpart module is supposed to clone the part the amount of times it says in the settings module.

For some reason when i call the createpart function it keeps erroring that the number is nil.

Any help?

createpart function module:

local module = {}
module.__index = module

function module:CreatePart(Settings,Part,Amount)
	self.settings = Settings
	self.part = Part
	for i=1,self.Settings.Amount do
		local ok = self.Part:Clone()
		ok.Parent = workspace
	end
	
end

return module

Settings module:

local module = {
	Hey = true;
	Amount = 10;
	sentence = "sup";
}

return module

1 Like

You need to use require(Settings) in order for that to work rather than referring to the object.

Now the cloning is nil but why? i defined those objects in this script:

local Settings = script.ok
local main = require(script.ModuleScript)
local Part = Instance.new("Part")

game.Players.PlayerAdded:Connect(function()
	main:CreatePart(Settings,Part)
end)


Also the part creation is not well secured in terms of memory management. I am not quite sure what exactly you’re having issues with. :man_shrugging:

Those three variables.

local Settings = script.ok
local main = require(script.ModuleScript)
local Part = Instance.new("Part")

Only two matter:

The Settings variable and the Part variable.

The settings variable is defined as the settings module and the Part is defined as the part.

function module:CreatePart(Settings,Part)
	self.Settings = require(script.Parent.ok)
	self.part = Part
	for i=1,self.Settings.Amount do
		local ee = self.Part:Clone()
		ee.Parent = workspace
	end
	
end

For some reason they’re nil and i wanna know why.

I even set the arguments to these two variables.

game.Players.PlayerAdded:Connect(function()
	main:CreatePart(Settings,Part) -- here
end)

You could otherwise change the last part into

game:GetService("Players").PlayerAdded:Connect(function()
    main:CreatePart(require(Settings), Part)
end)

and revert previous change.

16:18:56.084 - Workspace.Script.ModuleScript:9: attempt to index nil with ‘Clone’

Just saw the issue!
Make sure to have your capitalization correctly!

Compare your code (broken)

local module = {}
module.__index = module

function module:CreatePart(Settings,Part,Amount)
	self.settings = Settings
	self.part = Part
	for i=1,self.Settings.Amount do
		local ok = self.Part:Clone()
		ok.Parent = workspace
	end
	
end

return module

with this one (that is probably fixed)

local module = {}
module.__index = module

function module:CreatePart(Settings,Part,Amount)
	self.settings = Settings
	self.part = Part
	for i=1,self.settings.Amount do
		local ok = self.part:Clone()
		ok.Parent = workspace
	end
	
end

return module

Issue: you set

self.settings = Settings
self.part = Part

and when you referenced those two you capitalized the letter “s” in “settings” and the letter “p” in “part”.

Hey, @vovcher! Make sure to mark my suggestion as a solution if your problem has been fixed.