Sharing functions between 2 scripts?

is it possible to share/transfer functions (use one function from a script in another script?)
i know i can use remote events or functions, but they are used to transfer info from a local script to a server script or from a server script to a local script, and what i want to do is use one function from a local script in other local scripts. is that possible?

1 Like

Server-to-server and client self-to-self is possible through shared or _G. However, it would create an issue of race conditions if they both are writing to the variable. Possibly might cause some instability. It is possible to write everything in one single script instead of two or more.

2 Likes

Module scripts my friend, They’re the most useful thing in studio IMO

1 Like

Only if the reply above wasn’t clear:

Create a ModuleScript, define the function inside of it. Once you’re done, be sure to return it too. Then then invoke require(), pass the path to the ModuleScript as its argument, in any LocalScripts that need to use the function.

Edit: Parent the ModuleScript to the ReplicatedStorage service directly / indirectly.

If you want to read more about them:
https://developer.roblox.com/en-us/api-reference/class/ModuleScript

If you don’t like reading, maybe watch this YouTube video:
(Don’t worry about the title of the YouTube video, ModuleScripts are not an "advanced’ concept, it’s simple.)

1 Like

Create a modulescript in replicatedstorage(recommended if you want clients and servers to use the function)

-- inside modulescript
local module = {}
function module.myfunction() 
  print("Hello world!")
end
function module.anotherfunction()
-- another code logic
end
return module
-

-- in a serverscript or localscript
local mod = require(game.ReplicatedStorage.ModuleScript) -- define the path to ur module
mod.myfunction() 

im not really good at explaining but you could watch some tutorials.
module scripts are one example

1 Like

Changes made in modulescript on server doesn’t even replicate to client and this is not the problem here in the first place.

1 Like

There are 3 ways to do it

  • Using _G: Set in some script _G.FuncName = function() end and now u can use that function in any other script!

  • Bindable events: Works the same a remote event but only between it’s own side, for example, allows firing to one script the function and in the other you wait for the event or connect a function that sets a variable the function u fired!

  • Module script: You return a table that contains the function from the module script, in any other script now you just need to require(path) it in a variable and now you can use it by variableName.funcName()

2 Likes

Although there are multiple ways to do this, ModuleScripts were designed with this specific purpose in mind. Here is the difference between a function in a script and a function made using a ModuleScript.


Regular function:

  • Code
function Test(Message)
	print(string.upper(Message))
end

Test("hello world")
  • Output

HELLO WORLD


The same function from before, but done with a ModuleScript:
  • Location

    image

  • Code inside ModuleScript

return function(Message)
	print(string.upper(Message))
end
  • Code inside the script that had the function before
local Test = require(game:GetService("ReplicatedStorage"):FindFirstChild("ModuleScript"))

Test("hello world")
  • Output

HELLO WORLD


ModuleScripts can return any single data type, in this case returning a function. That's really all there is to understand with them. They are extremely useful because they allow sharing of code not only between scripts, but also between both server- and client-sided networks as well. Good luck!
1 Like