Forest Logo
search
package_2

subscription

By @neonifieddev

Roblox

Mirrored

Subscription

Path-based state subscriptions for Roblox, powered by neonifieddev/network.

Install

[dependencies]
Network = "neonifieddev/network@1.0.0"
Subscription = "neonifieddev/subscription@0.1.0"

Subscription also declares Network in its own wally.toml, so consumers only need to add Subscription if they are not using Network directly elsewhere.

Server

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

local Subscription = require(ReplicatedStorage.Packages.Subscription)

local subscriptions = {}

Players.PlayerAdded:Connect(function(player)
	local data = {
		leaderstats = {
			Cash = 100,
			Gems = 5,
		},
		inventory = {},
	}

	local playerSubscription = Subscription.ForPlayer(player, "Data", data)
	subscriptions[player] = playerSubscription

	playerSubscription:Activate()

	playerSubscription:Set("leaderstats.Cash", 250)
	playerSubscription:Add("leaderstats.Cash", 50)
	playerSubscription:TableInsert("inventory", {
		id = "Sword",
		amount = 1,
	})
end)

Players.PlayerRemoving:Connect(function(player)
	local playerSubscription = subscriptions[player]
	if playerSubscription then
		playerSubscription:Destroy()
		subscriptions[player] = nil
	end
end)

Client

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Subscription = require(ReplicatedStorage.Packages.Subscription)

local playerSubscription = Subscription.ForPlayer("Data")

playerSubscription:OnReady(function(data)
	print("initial cash", data.leaderstats.Cash)
end)

playerSubscription:OnChanged(function(newValue, oldValue, changedPath, op)
	print("changed", table.concat(changedPath, "."), op, oldValue, newValue)
end)

playerSubscription:OnPathChanged("leaderstats.Cash", function(newCash, oldCash, changedPath, op)
	print("cash changed", oldCash, "->", newCash, op)
end, true)

playerSubscription:OnPathChanged({ "inventory" }, function(inventory, oldInventory, changedPath, op)
	print("inventory changed", op, #inventory)
end)

The server sends one initial snapshot on :Activate(), then sends only operations such as set, insert, remove, and clear. Direct table mutations do not replicate; use the subscription methods for changes that should reach clients.

Package Details

Install command (Click to copy)


Version

1.0.0

License

MIT

check_circle

Safe for commercial use

infoThe package archive does not include its license text; the license is declared in its manifest metadata.

Automated license review — not legal advice.