Functions that can be used in all scripts?

Hello!

Basically, I’m trying to make a function that can be used in all scripts without having to add the function itself. How do I do this?

I’ve tried modules but I don’t exactly know how to make it works with modules.

Just return your function!

-- Module

return function()
    -- Code
end
1 Like

How would I make it work in another script? I’m not sure how to require it :pensive:

You should be using modules lol. There’s code examples at the bottom of the page if you scroll down.

local my_functions = {}

-- Add a few functions to the table
function my_functions.foo()
	print("Foo!")
end

function functions.bar()
	print("Bar!")
end

-- ModuleScripts must return exactly one value
return my_functions

You can then call require(path) on the module. If you’re calling it from a local script, it treats it as if it were a local operation. The opposite applies with server scripts.

local my_functions = require(script.Parent.MyFunctions)
 
 -- These are some dummy functions defined in another code sample
 my_functions.foo()
 my_functions.bar()
2 Likes

A common response would be to use _G functions or Modules. I highly recommend modules, modules are more favorable rather than _G and it’s commonly noticed that global functions have an effect on performance. My advice is to learn the basics of modules, it shouldn’t take long until you know how to use modules, assuming you have some scripting knowledge.

1 Like

Thank you all! And yes, I figured _G functions would work in the same type fo scripts but it uses more RAM which could be worse.

1 Like