Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

how do you make a function accessable by other scripts?

Asked by 6 years ago
Edited 6 years ago

i have a server script that has a function but i want other scripts to be able to call that function i heard of using _G but i dont know how to use it can anyone help do i do this

local _G  = function()
--function contents
end

and how do i call it

0
You would use module scripts instead since module scripts > _G. Zafirua 1348 — 6y
0
^ Correct. Griffi0n 315 — 6y

2 answers

Log in to vote
2
Answered by
amanda 1059 Moderation Voter
6 years ago

You can insert methods into the global _G table like you would any other table.

In case you did not know _G was a reference to a table, or didn't know how to add methods(functions) to a table, see below code for an example.

function _G.Print(str)
    print("SERVER: "..str)
end

That creates a custom print method called Print, and can be used in other scripts like so:

wait(1)
_G.Print("Hello") --> SERVER: Hello

The reason I added a wait to the top of the script, is because it is not guaranteed that the other script will have added it by the time this script tries to index it. Not having a wait may result in an error.

Note, I used standard function notation, you can also define it using a variable format:

_G.Print = function(str)
    print("SERVER: "..str)
end

It can be considered poor practice to fill the _G table with tons of methods, rather it is widely accepted to use ModuleScripts instead. (As has been pointed out by other members)

ModuleScripts run once they are required, and they must return exactly one value. This is commonly a table, but can be anything such as a function.

What I would recommend doing, since you already are attempting to use a table and load it with methods, is do just that, but in a ModuleScript. How this would work, is inside the module you would create a table, and then insert the methods similar to how I showed above.

Then you would return the table.

Inside any of your scripts, you would require the module and store it in a variable in each of the scripts you want to use it for. After that point, the usage is the same.

ModuleScript:

local library = {}

function library.Print(str)
    print("SERVER: "..str)
end

return library

Script:

local library = require(script.ModuleScript)
--script.ModuleScript is the path to the module

library.Print("Hello") --> SERVER: Hello

A wait is not needed for modules, because requiring them ensures they are loaded.

0
that was really helpful mattchew1010 396 — 6y
Ad
Log in to vote
1
Answered by
Griffi0n 315 Moderation Voter
6 years ago
Edited 6 years ago

Don't use _G or shared instead use module scripts. Create a module script in the Replicated First if it's for the client, Replicated Storage if it's for the client and the server, and Server Storage/Server Script Service if it's for the server. Then name of the module script could be the name of your function or something like that. In this case I will use MyFunction.

Example module script code:

local myFunction = function(part)
    part.Position = Vector3.new(0, 0, 0)
end

return myFunction

Then put something like this in your main script

Example regular script code:

local myFunction = require(game:GetService("ReplicatedStorage").MyFunction) -- In this case I placed the module in the Replicated Storage

myFunction(workspace.Baseplate)

Basically return myFunction returns the function to the script that's requiring the module. You can return any type in a module script. So you can return strings, integers, booleans, and tables.

0
i know about those, you're saying i can't make just one function be like a module script because the rest of the script wont work with a module scrpt mattchew1010 396 — 6y
0
I have no clue what you mean by that. Griffi0n 315 — 6y
0
If you mean you want multiple functions in a module script then return a table of functions (although that's the reason why I don't like _G) Griffi0n 315 — 6y
0
i just want one function in a regular script can i do that mattchew1010 396 — 6y
View all comments (2 more)
0
You should still you use modules over _G. Griffi0n 315 — 6y
0
ok mattchew1010 396 — 6y

Answer this question