Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I call a function from a module script?

So I have this camera spring script, which goes into a module script. The thing is, I'm don't know how to call a function from a module script. I feel pretty stupid right now, but I seriously dunno. Here's the script.

local Spring = {}
Spring.__index = Spring
 
function Spring:Update(dt)
	local t, k, d, x0, v0 = self.t, self.k, self.d, self.x, self.v
	local a0 = k*(t - x0) + v0*d
	local v1 = v0 + a0*(dt/2)
	local a1 = k*(t - (x0 + v0*(dt/2))) + v1*d
	local v2 = v0 + a1*(dt/2)
	local a2 = k*(t - (x0 + v1*(dt/2))) + v2*d
	local v3 = v0 + a2*dt
	local x4 = x0 + (v0 + 2*(v1 + v2) + v3)*(dt/6)
	self.x, self.v = x4, v0 + (a0 + 2*(a1 + a2) + k*(t - (x0 + v2*dt)) + v3*d)*(dt/6)
	return x4
end
 
function Spring.new(stiffness, dampingCoeff, dampingRatio, initialPos)
	local self = setmetatable({}, Spring)
 
	dampingRatio = dampingRatio or 1
	local m = dampingCoeff*dampingCoeff/(4*stiffness*dampingRatio*dampingRatio)
	self.k = stiffness/m
	self.d = -dampingCoeff/m
	self.x = initialPos
	self.t = initialPos
	self.v = initialPos*0
 
	return self
end
 
return Spring

Any help would be great.

7 comments

0

These formulas can be simplified my guy.

0

local m = (dampingCoeff ^ 2) / ((stiffness * 4) * ( dampingRatio ^ 2))

0

self.v is an unnecessary calculation too.

0

Mmm, thanks for the advice. But I still dunno how to call functions from a module script. Do I need a remote function thing?

0

Spring.new(0,0,0,0):Update(0). The methods are bound to the class that it's created for, create an Instance of the Object and call the function. Use require() to include the Object in another script.

0

It's also best you don't write these variables in an algebraic form. Try to give them names that correlate to it's purpose. It'll help you in the future.

0

Okay, thanks. I'm trying some stuff out rn

1 answer

calling a module script function looks hard, but it's actually easy

So for now let's assume this is the script where you want to call your ModuleScript

--Change script.Parent.moduleScript to your moduleScript path
local moduleScript = require(script.Parent.moduleScript) 

--To call them
moduleScript.yourFunctionName()

This discussion is preserved as it was. New answers, comments, and votes are closed.