0

# How do I make functions global?

wrenzh 65
7 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.

1
Answered by 7 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()

0
MixCorp 70
7 years ago

Very simple,

_G["FunctionName"] = function(argument1,argument2)
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 — 7y
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 — 7y
0
Hmm MixCorp 70 — 7y
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 — 7y
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 18044 — 7y