If you are an active Devforum viewer, you might have seen this OOP tutorial -
All about Object Oriented Programming.
He told us how to enhance and make our development process easy if we are on some really big projects. So, according to him thats how we do that, but there is one issue. That is when we use it on Script, the object will be created on Server, if done on LocalScript, it would be created on Client. So how do we sync objects? The answer is simple, And I am going to tell you.
So,
Step 1:- Create a RemoteFunction (Not Remote Event!)
Step 2:- On the Module script, bind a sync function to it. The function’s basic arguments will be, (ID: string or number)
Step 3:- In the function, work using variables like “local_machine” and “remote_machine” instead of “client” or “server”.
Why so? because in the ModuleScript, the Executing side is not known by default and it would work for both sides, for eg, Syncing from server to client or Client to server.
Step 4:- The ID Variable received from the arguments will be from the syncing side, If the Server initiates it, It will be received by the client and the ID that the client received, was generated by the server. Generate 1 more Object ID on the client side, Thats because no. of objects on the server could be more or less than on the client.
Step 5:- At the beginning, the side on the function is being called the side which don’t have the object. Let’s create the object and store it in the self variable.
Step 6:- At the end of the function, add a line like: object[LocalID] = <local_id>
and object[RemoteID] = ID
, Here replace <local_id>
with the ID generated on the client and the “ID” is the argument variable received on the function machine side.
This Assignment of Index RemoteID
and LocalID
are done on the side on which the sync function is executed, not the other side.
Step 7:- On the function, return the LocalID only, not the RemoteID or ID, the remote side already knows that ID because it is generated and sent to this side by that side.
Step 8:- Create the function that will be called in order to Sync in 1 place AKA just by calling it. In the Object Class, for eg, function SyncAbleObject:Sync() ... end
Step 9:- Now let’s fill this function.
Step 10:- In the function, Generate one object ID, and call the remote function with this argument. (See Step 2, This is that variable being passed there)
Step 11:- Call that RemoteFunction with this ID argument, It will return the ID generated on that side of the network, and store it in the variable named for eg, remoteID
Step 12:- Now as we did on that function, stored in the variable at indexes RemoteID
And LocalID
, Here, it would be just the opposite, LocalID is the ID generated on this side and the RemoteID is the ID generated on that side. Set LocalID
to “ID” and RemoteID
to the value returned by the function.
So, We have done it!
Here’s the basic ModuleScript if you had any confusions:
SyncAbleObject = {}
SyncAbleObject .__index = SyncAbleObject
if(_G.objects == nil) then
_G.objects = {}
end
local RemoteFunc = script.SyncFunction
function SyncAbleObject:Sync()
--Generate ID by adding objects to object list
table.insert(_G.objects, self)
local ID = #objects --Length of the list
local remoteID
if(game:GetService("RunService"):IsServer()) then
remoteID = RemoteFunc:InvokeClient(ID) --The ID on this side
else
remoteID = RemoteFunc:InvokeServer(ID) --The ID on this side
end
self.LocalID = ID
self.RemoteID = remoteID
end
function SyncSide2(ID)
--This is the other side of the network
--Create object
local self = SyncAbleObject.new()
--Generate ID by adding objects to object list
table.insert(_G.objects, self)
local localID = #objects --Length of the list
--Super Important note: The objects variable may look the same, but remember, we are on the other side and the table is not the same on this side!
self.LocalID = localID
self.RemoteID = ID
return localID
end
RemoteDunc.OnClientEvent = SyncSide2
RemoteDunc.OnServerEvent = SyncSide2
function SyncAbleObject.new()
local SAObject = {}
setmetatable(SAObject, SyncAbleObject )
return SAObject
end
Now objects are created on both sides and are In sync.
Note: This is just a basic implementation and can be customized according to your needs.
Please forgive me for any mistakes and if I have done any, kindly reply below.
Thanks.