ModuleScript returns Instance, but I can't use it. (attempt to index nil with <property>)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to make a glass-like fracture system. What will I use it for? I’m making an attack for a boss that once it punches the ground, it fractures the ground, and stepping on the lines that fracture the ground damages the player.

  1. What is the issue? Include screenshots / videos if possible!

The ModuleScript returns a Model, but trying to use it errors, as if the Instance was nil.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried, found alot of topics but none of them matched the problem I was having.

ModuleScript code:

local rng = Random.new()

local lightning_part = Instance.new("Part")
lightning_part.Anchored = true
lightning_part.CanCollide = false
lightning_part.Size = Vector3.new(0.25, 0.25, 0.25)
lightning_part.Material = Enum.Material.Neon
lightning_part.Color = Color3.fromRGB(160, 128, 255)

return function(startpos, fractures, thickness, xOffset, yOffset, zOffset)
	local points = {}
	local model = Instance.new("Model")

	for i = 1, fractures do
		local offset = Vector3.new(rng:NextNumber(xOffset[1], xOffset[2]), rng:NextNumber(yOffset[1], yOffset[2]), rng:NextNumber(zOffset[1], zOffset[2]))

		if i == 1 then
			offset = Vector3.new(0, 0, 0)
		end

		points[#points + 1] = startpos + offset * i
	end

	for index, point in points do
		if points[index + 1] ~= nil then
			local beam = lightning_part:Clone()
			
			beam.Size = Vector3.new(thickness, thickness, (points[index] - points[index + 1]).Magnitude)			
			beam.CFrame = CFrame.new((points[index] + points[index + 1]) / 2, points[index + 1])

			beam.Name = "Fracture"..index
			beam.Parent = model
		end
	end
	
	return model
end

What I’m trying to do with the Model:

local model = require(workspace["Sublevel 90"].Deepbound_Winner.Modules.FractureModule)(workspace.TemplateR6.HumanoidRootPart.Position, 16, 0.25, {0, 15}, {0, 0}, {0, 15}) 
model.Name = "Fracture" 
model.Parent = workspace

Hieraches: (straightforward to the ModuleScript)
image

Error message: (I split the console code into lines so it’s not bunched up together)

Things I’m gonna clarify:

The model is not deleted
The TemplateR6 rig DOES exist

Any help is appreciated.

it should work

try seeing if the require is working separately

local getModel = require(workspace["Sublevel 90"].Deepbound_Winner.Modules.FractureModule)
print(getModel)

local model = getModel(workspace.TemplateR6.HumanoidRootPart.Position, 16, 0.25, {0, 15}, {0, 0}, {0, 15}) 

print(model)

model.Name = "Fracture" 
model.Parent = workspace

Nope, still errors…

maybe its getting garbage collected or something

Try this

local model = Instance.new("Model", workspace)
print(model) -- make sure it exists

Uh, nope. Still errors… Frustrating, I know.

try cloning it instead with :Clone() also make sure your actually calling the correct model aswell

it errors when printing? that could mean the model doesn’t exist

where are you running it from?

in the command bar its usually slow to update modulescripts

Command bar. Im testing the offsets

Try moving the module to replicated storage

No, I tried to put the print in the moduleScript yet didnt change anything with the error :person_shrugging: (Nothing printed as well)

in the module script or before you used require()?

I tried to print the model after defining it in the moduleScript,
Nothing printed.

Same problem
(Also tried :Clone()-ing after the Instance.new but nothing new (inside the module))

Took the function from the module (Including the variables so it won’t error) and it worked.
Really wanted it to be inside the module but I guess I’ll have to stick to pasting the function where I want to use it.
Thanks for the help everybody, but I found the solution now.

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