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

Difference between Bindable Functions and Global Functions?

Asked by 6 years ago

I'd like to have a function in one server script, and be able to call it from another server script. My first research shows that a Global Function will do this. The wiki says I may want to consider a BindableFunctions instead of a global function.

My question is what are the differences in these and why would I use one of the other for a server to server script? To me, they appear to do the same thing in this situation and I don't see an advantage over one.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

BindableFunctions automatically delete non-integer keys in mixed tables. ex, if you attempt to Invoke({1, 2, var=3}) and then print arg.var, it will be nil. (For this reason I never use BindableFunctions.)

Global functions do not have this limitation, but I recommend using ModuleScripts instead, for organization (they also do not have the limitation). If (over time) you end up with two functions with the same name, if you have them in two different ModuleScripts you won't have any trouble, but if you try to put them both in _G then one will of course overwrite the other. In other words, using ModuleScripts means you don't need to worry about name collisions with other scripts.

[Edit]

ModuleScripts do cache the results of the script, but not the results of any functions within it. Here are some examples of what you can do with ModuleScripts:

--ModuleScript acting as a global list of items, place it in game.ServerScriptService:
return {}
--Script 1:
local list = require(game.ServerScriptService.ModuleScript)
table.insert(list, "hi")
--Script 2
wait(0.1)
local list = require(game.ServerScriptService.ModuleScript)
print(list[1]) -- will print "hi"

--ModuleScript with functions and local variables:
local list = {}
local sum = 0
local module = {}
function module.AddItem(item)
    table.insert(list, item)
    sum = sum + item
end
function module.GetSum()
    return sum
end
return module
--Script 1:
local module = require(game.ServerScriptService.ModuleScript)
module.AddItem(3)
module.AddItem(5)
--Script 2:
local module = require(game.ServerScriptService.ModuleScript)
wait(0.1)
print(module.GetSum()) -- 8
module.AddItem(10)
print(module.GetSum()) -- 18
0
I'm really not sure what the use of ModuleScripts are for either. They cache the results, so I wouldn't be able to run a modulescript and get different results based on a variable I pass it. It always returns the results from the first time it was ran. superdragonhunter64 2 — 6y
0
Edited in response to your comment. chess123mate 5873 — 6y
Ad

Answer this question