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

How to make a Global Function? [closed]

Asked by
Bman8765 270 Moderation Voter
9 years ago

I wanted to make a global function called reset that will basically reset all gui's. I am just unaware of how to do this. Now I'm not an idiot (sorry if you thought I was). I know what functions are, how to make events like such happen and so on but what I don't know is how to make a function that can be called in different local and normal scripts. Is there a way, and if so how do you make a function that can be called from any script in the game?

Locked by Redbullusa and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
2
Answered by 9 years ago

There are a few ways you can create a global function. The first is simply using _G. _G is a global table that you can put functions inside. The _G table is not completely global though. There is an _G table for the server and then individual _G tables for each client.

function _G.Bobert() --In normal script
print("HI")
end

_G.Bobert() --In another server script

_G.Bobert () --In a local script this will not work unless a local script on the same client defines it.

So to make the function available in every localscript/client you could put this localscript inside StarterGui. So then this script is replicated to every client where the function is defined in every client's _G table.

function _G.Bobert()
print("HI")
end

If you just want a function accessible everywhere you could also use a ModuleScript.

In Module Script:

function ClearGui(Player)

end

return ClearGui

In Another script:

local ClearGui = require(Workspace.ClearGuiModuleScript)

ClearGui(BOBERT)
0
Okay thanks for explaining, this makes more sense! Bman8765 270 — 9y
Ad
Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

The way I suggest to do this is not with global variables but rather with a RemoteEvent. These allow you to communicate across scopes (server/client). This allows the server to call one function that then resets all players GUIs. The useful thing about these is that you can fire the function for all clients or for a particular player only.

Server Script:

local remoteEvent = game.ReplicatedStorage.RemoteEvent -- pointer to your remote event object

-- When you want to reset the gui's
-- All Clients
remoteEvent:FireAllClients(...) -- The ... stands for any arguments you may need

-- Single Player
remoteEvent:FireAllClients(player, ...) -- Player needs to be defined

Local Script:

local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent.OnClientEvent:connect(function()
    -- Do something to make you GUIs reset
    -- You could clear all GUIs then copy new ones in for example
end)