local G=(require(script.Parent.loader)).load(script)local N=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local t=game:GetService("\082\117\110\083\101\114\118\105\099\101")local c=G("\082\101\115\111\117\114\099\101\067\111\110\115\116\097\110\116\115")if not t:IsRunning()then return function(G)local N=Instance.new("\082\101\109\111\116\101\070\117\110\099\116\105\111\110")N.Name="\077\111\099\107"..G N.Archivable=false return N end elseif t:IsServer()then return function(G)assert(type(G)=="\115\116\114\105\110\103","\066\097\100 \110\097\109\101")local t=N:FindFirstChild(c.REMOTE_FUNCTION_STORAGE_NAME)if not t then t=Instance.new("\070\111\108\100\101\114")t.Name=c.REMOTE_FUNCTION_STORAGE_NAME t.Archivable=false t.Parent=N end local j=t:FindFirstChild(G)if j then return j end j=Instance.new("\082\101\109\111\116\101\070\117\110\099\116\105\111\110")j.Name=G j.Archivable=false j.Parent=t return j end else return function(G)assert(type(G)=="\115\116\114\105\110\103","\066\097\100 \110\097\109\101")return(N:WaitForChild(c.REMOTE_FUNCTION_STORAGE_NAME)):WaitForChild(G)end end
--[=[
	Retrieves a global remote function from the store. On the server, it constructs a new one,
	and on the client, it waits for it to exist.

	:::tip
	Consider using [PromiseGetRemoteFunction] for a non-yielding version
	:::

	```lua
	-- server.lua
	local GetRemoteFunction = require("GetRemoteFunction")

	local remoteFunction = GetRemoteFunction("testing")
	remoteFunction.OnServerInvoke = function(_player, text)
		return "HI " .. tostring(text)
	end

	-- client.lua
	local GetRemoteFunction = require("GetRemoteFunction")

	local remoteFunction = GetRemoteFunction("testing")
	print(remoteFunction:InvokeServer("Bob")) --> HI Bob
	```

	:::info
	If the game is not running, then a mock remote function will be created
	for use in testing.
	:::

	@yields
	@function GetRemoteFunction
	@within GetRemoteFunction
	@param name string
	@return RemoteFunction
]=]

local require = require(script.Parent.loader).load(script)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local ResourceConstants = require("ResourceConstants")

if not RunService:IsRunning() then
	return function(name)
		local func = Instance.new("RemoteFunction")
		func.Name = "Mock" .. name
		func.Archivable = false

		return func
	end
elseif RunService:IsServer() then
	return function(name)
		assert(type(name) == "string", "Bad name")

		local storage = ReplicatedStorage:FindFirstChild(ResourceConstants.REMOTE_FUNCTION_STORAGE_NAME)
		if not storage then
			storage = Instance.new("Folder")
			storage.Name = ResourceConstants.REMOTE_FUNCTION_STORAGE_NAME
			storage.Archivable = false
			storage.Parent = ReplicatedStorage
		end

		local func = storage:FindFirstChild(name)
		if func then
			return func
		end

		func = Instance.new("RemoteFunction")
		func.Name = name
		func.Archivable = false
		func.Parent = storage

		return func
	end
else -- RunService:IsClient()
	return function(name)
		assert(type(name) == "string", "Bad name")

		return ReplicatedStorage:WaitForChild(ResourceConstants.REMOTE_FUNCTION_STORAGE_NAME):WaitForChild(name)
	end
end
