Changes to ObjectValues don't replicate correctly when using selective replication

Changing ObjectValue.Value to an instance all clients can see then changing it to a selectively replicated instance will cause a situation where clients that cannot see the selectively replicated instance will have an outdated value (the instance that all clients can see).

The correct behavior is that ObjectValue.Value should be nil if the current value is not known to the client. It should not keep an outdated value.

Reproduction:

  1. Create a place file with code that selectively replicates some object (e.g. a Part in ServerStorage) to one player. See below for some code which does this.
  2. Start a local server with two players (one called “Player Yes” who has the Part, and one who doesn’t called “Player No”).
  3. On the server, create an ObjectValue in Workspace and set its Value to the selectively replicated Part.
  4. Observe that Player Yes’s client can see ObjectValue.Value == Part and on Player No’s client they can see ObjectValue.Value == nil.
  5. Change ObjectValue.Value to some instance both clients can see, e.g. another part or model in workspace, let’s just say this is a Model.
  6. Observe that both players’ clients can see ObjectValue.Value == Model.
  7. Change ObjectValue.Value back to the selectively replicated Part.
  8. Observe that Player Yes’s client correctly sees ObjectValue.Value == Part, and Player No’s client incorrectly sees ObjectValue.Value == Model. The issue is that Player No is still seeing a wrong and outdated value when really they should be seeing nil.
Code

Server:

local P = Instance.new("Part")
P.Name = "SelectivePart"
P.Anchored = true
P.Parent = game.ServerStorage

local M = Instance.new("Model")
M.Parent = workspace

local R = Instance.new("RemoteEvent")
R.Parent = game.ReplicatedStorage

local O = Instance.new("ObjectValue")
O.Name = "ObjectValue"
O.Parent = workspace

local Switch = false
local function PlayerAdded(Player)
	Switch = not Switch
	Player:SetAttribute("Replicate", Switch)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
for _, Player in game.Players:GetPlayers() do
	PlayerAdded(Player)
end

R.OnServerEvent:Connect(function(Player)
	local PlayerGui = Player.PlayerGui
	local ScreenGui = Instance.new("ScreenGui")
	ScreenGui.ResetOnSpawn = false
	local OriginalParent = P.Parent
	P.Parent = ScreenGui
	ScreenGui.Parent = PlayerGui
	
	R:FireClient(Player, P)
	
	P.Parent = OriginalParent
	ScreenGui:Destroy()
end)

Client:

local O = workspace:WaitForChild("ObjectValue")
O:GetPropertyChangedSignal("Value"):Connect(function()
	print("ObjectValue changed to", if O.Value then O.Value:GetFullName() else nil)
end)

local R = game.ReplicatedStorage:WaitForChild("RemoteEvent")
R.OnClientEvent:Connect(function(P)
	P.Parent = workspace
end)

local Player = game.Players.LocalPlayer
while Player:GetAttribute("Replicate") == nil do task.wait() end
if Player:GetAttribute("Replicate") then
	R:FireServer()
end

We have a ticket in our system - looking for someone to take a look at this! Hang tight.

Hey Rio, thank you for the detailed report and sorry for the delayed reply! I understand this is inconvenient for your use-case, but we have no plans to change the current behavior.

For background, when clients receive updates about instances that they do not know about, we purposefully defer applying that update to better support updates arriving out of order, since the client doesn’t have a way to know if that instance will be replicated to it at some point in the future.

If you would like to see better support for this type of “selective” replication, please file a feature request!

Would a workaround be setting the value to nil then setting it to the selectively replicated instance? (On the same frame.) Or would other clients still see the original value?

A better workaround would be using a sentinel value. That is, assigning the value to something like workspace that is replicated and having the consuming code treat that as nil, or even assigning the value to some “empty” object of the type you’re working with if it’s a simple enough case where that works.

Alternatively if you really want to use nil you could send an explicit “clear” RemoteEvent to the clients that they respond to by locally clearing the value.