Question about _G. functions

If a player on client side calls a _G. function, for example, _G.CanUse(), is the player that called it automatically sent as the 1st parameter in the function, like remote events?

1 Like

Yes It’s the same like remote events :sunglasses:

Its like the same yes ! That better for reduce exploiter for exemple

I have a server script that has:

function _G.Test(p1, p2)
	print(p1)
	print("p1 done now p2")
	print(p2)
end

and a client script that has:


local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(Key, GPE)
	if not GPE then
		if Key.KeyCode == Enum.KeyCode.E then
			_G.Test()
		end
	end
end)

But when I press E i get this error:

image

why?

Global namespace variables and functions (_G) are not shared between client and server. For this to work you will need remote functions/events.

2 Likes

Please can you show an example

Let’s say you want this _G variable to be transferred to the client, or be in both, do the following.

Insert a remoteevent and…

_G.func = function() end -- ur function
remoteevent:FireClient(plr,_G.func)

Above is server.

remoteevent.OnClientEvent:Connect(function(g)
_G.urfuncname = g
end)
--client.

Hope this helped.

1 Like

They should not be used much. They can be buggy often, and as of this I almost never use it.

Use ModuleScripts, RemoteEvents, and BindableEvents instead.

2 Likes

You can’t send functions over the network.


@Cia_Kaze
@Hypocrisism

Don’t spread misinformation. _G is nowhere near remote events, and it does not reduce exploiters(?).

You need to explicitly pass the player instance over like you would with any regular function.

I’m not sure why the first two replies stated otherwise.

Here’s a very trivial example:

function _G.getNameFromPlayer(...)
	local arguments = {...}
	return arguments[1].Name
end

local players = game:GetService("Players")
local player = players.LocalPlayer
local playerName = _G.getNameFromPlayer(player)
print(playerName)

You can’t pass function values across the client-server boundary but you can do something similar to the following.

--LOCAL

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated:WaitForChild("RemoteEvent")

remote:FireServer()
--SERVER

local replicated = game:GetService("ReplicatedStorage")
local remote = replicated.RemoteEvent

function _G.printFirstArg(...)
	local args = {...}
	print(args[1])
end

remote.OnServerEvent:Connect(_G.printFirstArg)