Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- AUTHOR: kirda @imKirda
- DATE: April 7 2021
- DESC:
- Loads framework on the client
- API:
- SHARED:
- void fireRemoteEvent(string, any...)
- - Fires remote event with given name to the server if found
- - Passes next parameters as tuple to the fire parameter
- any... awaitRemoteEvent(string)
- - Yields until remote event with given name fires
- - Returns what the remote event yielded
- any... invokeRemoteFunction(string, any...)
- - Invokes server with function with given name if found
- - Passes next parameters as tuple to invoke parameter and returns result
- RBXScriptConnection onRemoteEvent(string, function)
- - Connects given function to event with given name
- - Returns the connection
- void onRemoteFunction(string, function)
- - Sets function with given name's invoke callback to given function
- --]]
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Players = game:GetService("Players")
- local Framework do
- local framework_module = ReplicatedStorage:FindFirstChild("@framework")
- if framework_module then
- Framework = require(framework_module)
- else
- error("Could not find '@framework' module in 'ReplicatedStorage'")
- end
- end
- local Internal = Framework.getInternal()
- local communication_folder = Internal.assertFolder("Communication", ReplicatedStorage.Client)
- local modules_folder = Internal.assertFolder("Modules", ReplicatedStorage.Client)
- local Shared = Internal.getShared()
- local Client = {
- Functions = {},
- Bindable = {
- Functions = {},
- Events = {},
- },
- Modules = {},
- CommunicationFolder = communication_folder,
- ModulesFolder = modules_folder,
- }
- function Client.Functions.fireRemoteEvent(event_name, ...)
- Framework.assertType(event_name, "string", 1)
- local event = Shared.Functions.getRemoteEvent(event_name)
- if event then
- event:FireServer(...)
- else
- error(("'%s' is not an existing remote event"):format(event_name), 2)
- end
- end
- function Client.Functions.awaitRemoteEvent(event_name)
- Framework.assertType(event_name, "string", 1)
- local event = Shared.Functions.getRemoteEvent(event_name)
- if event then
- return event.OnClientEvent:Wait()
- else
- error(("'%s' is not an existing remote event"):format(event_name), 2)
- end
- end
- function Client.Functions.invokeRemoteFunction(function_name, ...)
- Framework.assertType(function_name, "string", 1)
- local _function = Shared.Functions.getRemoteFunction(function_name)
- if _function then
- return _function:InvokeServer(...)
- else
- error(("'%s' is not an existing remote function"):format(function_name), 2)
- end
- end
- function Client.Functions.onRemoteEvent(event_name, callback)
- Framework.assertType(event_name, "string", 1)
- Framework.assertType(callback, "function", 2)
- local event = Shared.Functions.getRemoteEvent(event_name)
- if event then
- return event.OnClientEvent:Connect(callback)
- else
- error(("'%s' is not an existing remote event"):format(event_name), 2)
- end
- end
- function Client.Functions.onRemoteFunction(function_name, callback)
- Framework.assertType(function_name, "string", 1)
- Framework.assertType(callback, "function", 2)
- local _function = Shared.Functions.getRemoteFunction(function_name)
- if _function then
- _function.OnClientInvoke = callback
- else
- error(("'%s' is not an existing remote function"):format(function_name), 2)
- end
- end
- --[[
- Main entry function
- Loads framework on the client
- --]]
- local function init()
- Internal.lockInternal()
- Internal.initialize()
- --[[
- Fires when server registers new event
- Registers the event on client
- --]]
- Shared.Network.Events["@register_remote"].OnClientEvent:Connect(function(remote)
- local remote_directory
- local remote_name = remote.Name
- if remote:IsA("RemoteEvent") then
- remote_directory = Shared.Network.Events
- elseif remote:IsA("RemoteFunction") then
- remote_directory = Shared.Network.Functions
- else
- error("Attempt to register non-remote event")
- end
- remote_directory[remote_name] = remote
- end)
- Internal.connect(Client)
- Internal.finishInitializationPeriod()
- Internal.start()
- Internal.clear()
- end
- init()
Add Comment
Please, Sign In to add comment