Error relating making a table value based on a function

Creating a class, and I wanted to try setting my table value to a function that would run when I call the value (I don’t know if that is optimal). When I try to call the table for later use I get an error stating I am attempting to get the length of a function value. Is what I am trying to achieve impossible?

local Debris = game:GetService("Debris")
local Run = game:GetService("RunService")

local Bolt = {}
Bolt.__index = Bolt

local Tool = script.Parent.Parent

function Bolt.new(StartPos, EndPos, Segments)
	local NewBolt = setmetatable({}, Bolt)
	
	NewBolt.StartPos = StartPos
	NewBolt.EndPos = EndPos
	NewBolt.Segments = Segments
	NewBolt.Points = function() --Table in question
		print("Points being created")
		local RNG = Random.new()
		local Points = {}

		for i = 1, Segments do
			if i == 1 then
				Points[#Points+1] = StartPos
			elseif i == 10 then
				Points[#Points+1] = EndPos
			else
				local Position = StartPos + ((EndPos - StartPos).Unit*i*(EndPos - StartPos).Magnitude / Segments)
				local Offset = Vector3.new(RNG:NextNumber(-1,1),RNG:NextNumber(-1,1),RNG:NextNumber(-1,1))
				Points[#Points+1] = Position + Offset
			end	
		end

		return Points
	end
	
	return NewBolt
end

function Bolt:Draw()
	local Folder = Tool:FindFirstChild("LightningBolt") or Instance.new("Model", Tool)
	Folder.Name = "LightningBolt"
	
	local Points = self.Points --Error
	
	for i = 1, #Points do
		if Points[i+1] ~= nil then
			local Segment = script.Bolt:Clone()
			Segment.Size = Vector3.new(0.2, 0.2, (Points[i]-Points[i+1]).Magnitude)
			Segment.CFrame = CFrame.new(Points[i]:Lerp(Points[i+1],0.5), Points[i+1])
			Segment.Parent = Folder
			Run.RenderStepped:Wait()
		end
	end
	
	return Folder
end

return Bolt

Try changing local Points = self.Points to local Points = self.Points()

The reason why you’re having the error is that you are calling the function itself, not the result of the function.

1 Like

Ahhhhhhh, I missed that. That time of the night I guess, thanks so much!