. . . and the object was created in a module script, would the changes I make to that object on the server be replicated to the client who now has that object?
Can you please elaborate this?
For example say I have a module script inside ServerStorage that is a Player class:
local Player = {}
-- Static variables
Player.PlayerList = {}
Player._index = Player
-- Constructor
function Player.new(PlayerID)
local self = setmetatable({}, Player)
self.PlayerID = PlayerID
table.insert(Player.players, self)
return self
end
-- Methods
function Player:KillCharacter()
self.IsAlive = false
end
Now letās say if I were to send a player a copy of their data to the client via a remote event:
-- Create a new player
local newPlayer = Player.new(player)
Player.PlayerList[player]:GetWorld().Parent = player.PlayerGui
Events.SendCopyEvent:FireClient(player, Player.PlayerList[player])
Would invoking the Player:KillCharacter()
on Player.PlayerList[player]
change the value on the client? Sorry, this is hard to explain.
Supposedly yes. ModuleScripts are ran respectively on the side of what required it. This means that if you require a module script on the client, the module script will also run on the client. Same thing applies to the server.
So the object reference from the server isnāt cloned and turned into its own copy when I send it to the client? That would mean that I can still change things of that object from the server, but clients cannot make changes correct?
Yes the object reference is created from Player.new(). If I pass that reference from server to client will changes from the server be updated on the client? ā Most simple way I can think of phrasing that.
Yes. Supposedly it should do that. Changes from the server will be replicated to all clients. (Correct me if Iām wrong!)
I will try this and let you know if it works. If it doesnāt work I have wasted so much time lol
When sending any table from the server to client or vice versa, the table is serialized into some form that can be sent to the client, and then the client deserializes it. You basically get a completely unique copy of the table which has no ties with the original table whatsoever. You can send the same exact table multiple times but you will always receive a brand new copy of it which will be unique. Making changes to any of these tables will make no changes in any of the others due to them being completely unique tables.
The client cannot make changes to the table on the server without a remote to do so, and the server cannot make changes to the table on the client without a remote to do so, they are completely independent of eachother.
ModuleScripts, when required, run individually in each environment. A ModuleScript required on the server will run and return something. A ModuleScript required on the client will run and return something. Between the server and the client there is nothing linking the results on the server to the results on the client or vice versa, they are completely unique.
Lastly, something I wanted to point out because it looks like you might be doing it, metatables (just like functions) are not (and canāt be) sent to the client, the new table you get on the client will be a completely brand new copy of the original table without any metatable on it at all. Whatever its real contents are are exactly what you will get.
P.s. happy cake day @Hollistic!
Thatās really useful! Thank you!
RIP my logic. If it was the way I thought it was, man my program would have been so robust. Now I gotta find a workaround. Thanks for the great information!
One workaround that might be helpful in your case is to simply tell the client when a value changes. You can do that with metatables. You can use an empty metatable and set its __index
to the target table and __newindex
to a function that updates the target table, then you will be able to use that to run some code when a value is written. That way you can sync updates to the client as they happen.
Iām kinda new to Metatables. How would I link Metatables between the server and the client? Ignore my auto correct
Well, you donāt really need to. Since you just want to tell the client when something changes on the server, you donāt need to bother with having the metatables match on both sides.
-- Server sided pseudo code
local targetTable = {data = 123}
sendTheFullTable(targetTable)
local listenerTable = setmetatable({}, {
__index = targetTable,
__newindex = function(self, index, value)
rawset(targetTable, index, value)
sendValueChange(index, value)
end
})
-- Client sided pseudo code
local targetTable = {}
-- When the table is recieved
targetTable = sentTable
-- When a property updates
targetTable[index] = value
TY. Probably just saved me a few hours of looking around.
Awesome! Thatās what scripting support is here for!