Help fixing this spring script

I have a spring script and a local script. I’m really new to springs and really want to learn how to use them for my FPS framework. The problem is, the spring module gives me the error: attempt to call a nil value line 18. Here’s my code:

local spring = {};

function spring.new(position, velocity, target)
	local self = setmetatable({}, {__index = spring});
	
	self.position = position;
	self.velocity = velocity;
	self.target = target;
	self.k = 1;
	self.d = 1; -- dampening

	return self;
end;

function spring:update()
	local d = (self.target - self.position);
	local f = d * self.k;
	self.velocity = self:gravTo(self.target + Vector3.new(0, self.distance, 0)) + (self.velocity * (1 - self.d)) + f;
	self.position = self.position + self.velocity;
end;

return spring;

And yes, this from is ego moose’s spring post.

Thanks in advance!

1 Like

Don’t know anything about springs, but looks like you’re creating a spring object through the use of lua metatables in the style of OOP.

You’re getting the ‘attempt to call a nil value’, because you’re trying to call a function which doesn’t exist. This one specifically

self:gravTo()

To get past this, you need to define the function gravTo() as a spring method.

function spring:gravTo(x)

Ahh okay. Since I have absolutely no idea what gravTo is supposed to be, I’ll try using a different spring script. Thanks!