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

How do I make functions global?

Asked by
wrenzh 65
10 years ago

So that I may use them in any script in the place, including LocalScripts and server Scripts.

I made a global function using _G in a LocalScript, but when I tried to call it on a server Script, it failed.

2 answers

Log in to vote
1
Answered by 10 years ago

(I would post this in a comment if I had 10+ reputation) I'm fairly sure you cannot specify a global functions (and global variables) in a localscript. Try putting it into a normal one:

_G["hellomynameisfunction"]=function()
print("WHOA MAN I JUST DID SOMETHING")
end
_G.hellomynameisfunction()
Ad
Log in to vote
0
Answered by
MixCorp 70
10 years ago

Very simple,

_G["FunctionName"] = function(argument1,argument2)
    print("Code Goes Here Yada Yada")
end

Then to call it, You use...

print("GUYS IMA DO SOMETHING GLOBAL!")
_G.FunctionName("Somethingthere","somethinghere")
print("I Did It")

You could also do something like,

_G["IsOwnerHere"] = function()
is = false
for i,v in pairs(game.Players:children() do
if v.userId == game.CreatorId then
is = true
end
end
return is
end

Then if your in the game

B = _G.IsOwnerHere()
--B = true
0
If this helped, An Upvote would be greatly appriciated. MixCorp 70 — 10y
0
That is exactly what I did. I put code like the first snippet into a LocalScript then attempted something like the second snippet in a server script, and it did not work. wrenzh 65 — 10y
0
Hmm MixCorp 70 — 10y
0
The only issue I could imagine you having, is an error in the function or code. Post the code in your q MixCorp 70 — 10y
1
As far as I am aware, global functions defined in LocalScript's cannot be called in a Server Script, but the opposite does work. BlueTaslem 18071 — 10y

Answer this question