Hey all ! I’m back again with another resource. This time I am sharing remote environments. At the time of this post, it is in early stages of development and likely to receive numerous changes. Additionally, you are encouraged to contribute by forking and submitting pull requests !
Github
Description
This module creates a shared environment (a table) that automatically transmits any changes made to the table via metaevents and RemoteEvents. The table has a strict owner, meaning that only one machine is allowed to modify the table, others may only listen to changes that are made. The server or a client can be a host (be very cautious about the data you read from a client-owned environment - I use it to simply track various states of the user, for example if they’re in the main menu).
Example Code
Server :
local RemoteEnvironment = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
game.Players.PlayerAdded:Connect(function(plr)
-- Server Owned Environment
local activeTable = RemoteEnvironment:CreateServerHost("Test", {TestT = {Test = 0}, Test = 0})
RemoteEnvironment:Subscribe(activeTable, plr)
task.spawn(function()
while true do
activeTable.TestT.Test += 1
wait()
end
end)
-- Client Owned Environment
local listeningTable = RemoteEnvironment:CreateClientHost(plr, "Test", {TestT = {Test = 0}, Test = 0})
-- Listens to changes made by the client. It's assumed that there will only be one client copy of each EnvironmentType.
RemoteEnvironment:ConnectToClient(plr, "Test", function(p, k, v)
print(k, v)
end)
while true do
print(listeningTable)
wait(1)
end
end)
Client :
local RemoteEnvironment = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
local activeTable = RemoteEnvironment:WaitForClient("Test")
local listeningTable = RemoteEnvironment:WaitForServer("Test")
while true do
activeTable.Test += 1
print(listeningTable, listeningTable.TestT.Test)
wait(1)
end
Plans
- Document with Moonwave.
- Implement callbacks for when users disconnect.
- Implement a strict environment for clients that must adhere to the datatypes of the initial table provided. It is currently possible for clients to flood the server’s memory.