Yes, that would be really helpful, thanks
Added server-side listeners as an experimental module!
This module injects the following missing methods to the ReplicaService module that are only available client-side by default:
I’ve also added a modified “AbstractExample” version that showcases server-side listeners.
You’ll need to update the ReplicaService module to the latest version as the Replica class table has been publicly exposed to make this experimental module work.
can you put an entire table into a single replica “Data” definition and the listen to any change on the entire table?
Yes, assuming you don’t violate parameter limitations.
I recommend keeping your Replica.Data
tables as flat as possible (fewer tables inside tables) - if your tables become too nested, you can break them down into several replicas by passing references to the individual tables. This recommendation is purely for the sake of clean code.
It would be nice if we could place the files for this service wherever we like in our folder organization instead of the top level as your setup instructions say is required, is this possible?
You may add them to folders that are in the respective game containers - the requires use recursive search in ReplicatedStorage and ServerScriptService.
Works! Amazing, quick question do I have to give credit for any open sourced module I use?
Credit is not necessary but highly appreciated!
Alright! Thanks this is really useful
[12/16/2020] - IMPORTANT BUG FIX - Update ReplicaService and ReplicaController modules from GitHub or Roblox library!
Fixed a bug where both ReplicaService and ReplicaController would fail to populate the Replica.Children
table with children replicas. Not updating your modules may result in failure to find expected children in replicas and memory leaks.
Check out the updated Replica Guarantees section in the official documentation!
Great library. Thank you for sharing.
How should we replicate the data after the player dies or reset its character? I think ReplicaController.RequestData() will not work, as DataRequestStarted is already set to true. Most likely I’m missing something here. Is there any other method to force the replication?
Presumably you should not be handling replicas in scripts that will be removed when a character respawns so that you do not have to request the data again. You could instead put the local script interacting with ReplicaController in StarterPlayer → StarterPlayerScripts.
[12/16/2020] - :SetParent() replication bug fix - Update your ReplicaService package from GitHub or Roblox library!
This bug inside of the ReplicaService module would make :SetParent()
fail to update reparented replicas with new replication settings of a new ancestor. Resulting behavior was failure to replicate changes for child replicas after :SetParent()
was used on them.
Various minor (non-vital) changes were included since the last update including error throwing when replication methods are called for destroyed replicas! Time to update!
Awesome library with well-written documentation. The developer has also been very helpful and patient with my questions.
I have a nested table, will this work?
I dont know if i have enough code to provide context
replica:ListenToChange({"Inventory.Hotbar"}, function(new_value)
print("Value changed:", new_value)
end)
Edit:
Only if the index in the Data table is literally “Inventory.Hotbar” like this:
Data = {
["Inventory.Hotbar"] = {}
}
But, with a table like this -
Data = {
Inventory = {
Hotbar = {}
}
}
You can’t use the dot syntax and the curly braces. It’s either one or the other.
-- Recommended
obj.Replica:ArrayInsert({"Inventory", "Hotbar"}, {Name = "Pepperoni Pizza", Description = "Nom nom nom. Pizza = delicious."})
-- Dot syntax (optional, but slower with larger replica implementations)
obj.Replica:ArrayInsert("Inventory.Hotbar", {Name = "Bloxy Cola", Description = "Warning: Bloxy Cola may be habit-forming."})
for k,v in pairs(obj.Replica.Data.Inventory.Hotbar) do
print(string.format("[Slot %i] Name: \"%s\"\t\tDescription: \"%s\"", k, v.Name, v.Description))
end
Output:
[Slot 1] Name: “Pepperoni Pizza” Description: “Nom nom nom. Pizza = delicious.”
[Slot 2] Name: “Bloxy Cola” Description: “Warning: Bloxy Cola may be habit-forming.”
I’m using this in a project and so far it’s amazing. Great Work!
I have one question, I’ve looked in the documentation and It doesn’t seem like there are any methods for listening to a replication change on the server-side. I may be just looking over it or miss something. Does this module support Server → Server replication or is it strictly Server → Client?
It’s not “officially” supported since conventionally Replicas would be a component of other custom lua objects and change handling would be handled inside said object methods - these methods can trigger custom script signals if the object has any script signals as members. (Assuming you’re using lua in an OOP style)
You can find MadworkScriptSignal module inside the ReplicaService repository for creating server-side and client-side signals yourself.
If you’re really stuck on the idea of having server-side Replica listeners then there’s an “experimental” module for just that which injects the missing listener methods into ReplicaService:
I created it in case my opinion on server-side Replica listeners could be proven wrong I do support the experimental listener module so I’ll be fixing it if errors are reported.