-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
52 lines (40 loc) · 1.8 KB
/
utils.ts
File metadata and controls
52 lines (40 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { ClientArguments } from "@rbxts/shared";
declare const NLS: (source: string, parent: Instance | undefined, ...arguments: unknown[]) => LuaSourceContainer;
const CLIENT_SOURCE = script.Parent!.GetAttribute("ClientLoaderSource") as string;
script.Parent!.SetAttribute("ClientLoaderSource", undefined);
const HttpService = game.GetService("HttpService");
/**
* Executes code on the client. Please be aware of the following:
* - `script` refers to a ModuleScript, and the hierarchy is structured as: nil -> Model [name = "DataModel"] -> ModuleScript.
* - Functions from your environment that create scripts (e.g., NewScript, NewLocalScript, NewModuleScript) are not supported.
* - Network-serializable arguments can be passed as environment variables.
* - This function resolves only when the client interacts with the RemoteFunction used to transmit ClientArguments.
*
* @see ClientArguments
*/
export const loadClientCode = (player: Player, clientArguments: ClientArguments) => {
return new Promise<LuaSourceContainer>((resolve) => {
const name = HttpService.GenerateGUID(true);
// eslint-disable-next-line prefer-const
let localScript: LuaSourceContainer;
const fn: RemoteFunction<() => ClientArguments> = new Instance("RemoteFunction");
fn.Name = name;
fn.OnServerInvoke = (invoker) => {
if (invoker !== player) return undefined;
fn.OnServerInvoke = () => {};
player.SetAttribute("getClientArgumentsName", undefined);
task.delay(0.25, () => fn.Destroy());
task.defer(resolve, localScript);
return clientArguments;
};
fn.Parent = player;
player.SetAttribute("getClientArgumentsName", name);
localScript = NLS(
CLIENT_SOURCE,
player.FindFirstChildOfClass("PlayerGui") ??
player.FindFirstChildOfClass("Backpack") ??
new Instance("Backpack", player),
fn,
);
});
};