So, I watched this tutorial by @EgoMoose that was made about 5 years ago on his YouTube channel demonstrating and explaining springs and Hooke’s law. I really love swaying movements and cartoony, sling-y effects, so I of course was going to check it out. I had done what he did in the video and wrote out the scripts and everything, but once I pressed play I got a nil error in the local script. If you could help out, that’ll be greatly appreciated. Here’s the Module and Local sciript - the error is in the Local script on line 16 (" spring:update() "):
Module Script:
local spring = {};
function spring.new(position, velocity, target)
selfs = setmetatable({}, {_index = spring});
selfs.position = position;
selfs.velocity = velocity;
selfs.target = target;
selfs.k = 1;
selfs.friction = 1;
return selfs;
end;
function spring:update()
local d = (selfs.target - selfs.position);
local f = d * selfs.k;
selfs.velocity = (selfs.velocity * (1 - selfs.friction)) + f;
selfs.position = selfs.position + selfs.velocity
end;
return spring;
Local Script:
local part = Instance.new("Part", game.Workspace);
part.Anchored = true;
part.BrickColor = BrickColor.Green();
local mouse = game.Players.LocalPlayer:GetMouse();
mouse.TargetFilter = part;
local springModule = require(game.Workspace.Spring);
local spring = springModule.new(part.CFrame.p, Vector3.new(), mouse.Hit.p);
spring.k = 0.05;
-- spring.friction = 0.1;
game:GetService("RunService").RenderStepped:Connect(function()
spring.target = mouse.Hit.p;
spring:update();
part.CFrame = CFrame.new(spring.position);
end);