Problem of instance not replicated to client yet (due to racing conditions)

Hi guys. I have began doing some coding, where I needed server to send Instance to client. Instance should be link to original. But I have run into the problem where it’s not yet replicated. How I can solve this problem?

Example:

--Server
local Part = Instance.new("Part")
Part.Parent = workspace
game:GetService("ReplicatedStorage").Remote:FireClient(Player, Part)
--Client
game:GetService("ReplicatedStorage").Remote.OnClientEvent:Connect(function(Part)
    Part.Color = Color3.fromRGB(255, 0, 0) --will error, cuz Part is not yet replicated.
end)

Just saying on the client-side script, is the :OnClientEvent causing the error or is it not and it is just a typo? It should be .OnClientEvent.

Sorry, that’s my typo on DevForum. Will fix that.

try this before changing the color
if not Part.Parent then Part.AncestryChanged:Wait()

The problem is that if part is sent this way, it will be sent to client as nil, so I can’t do anything over it.

Have you tried this yet or are you going for a different way to combat the problem?

--Client
game:GetService("ReplicatedStorage").Remote.OnClientEvent:Connect(function(Part)
	repeat
		task.wait()
	until Part
	
	Part.Color = Color3.fromRGB(255, 0, 0) --will error, cuz Part is not yet replicated.
end)

Won’t work, because as I said above, if part is not yet replicated to client, it will be received as nil, and Part won’t change to Instance, because replication won’t cover that - if it not received part it will keep it as nil.

then add a wait before firing the event

This’s not a solution sadly too. It will work only in perfect scenarios with 0 ping.
But ping is impossible to just guess. And ontop of that, process of replication itself takes some time too.
Only way it’s possible to do is to set wait time to smth big like 10-30, but player can just get bad internet connection and overlag that too.

then they can just get a better wifi

Why not just have a pre-created part in ReplicatedStorage?

you could also add the part to the replicatedstorage through the script

I have a really complex internal building systems. In short, it consists of the following:

  1. Players join server
  2. Their own islands load - players build them themselves.
  3. While they load, objects load too. If they have some required to work functions, will be initialized and executed.
  4. While doing that initialization, it may be necessary to switch work from server to client, or just fire signal that it should begin working. System which handles that consists of: Action, Object, Data - Action which should be performed, Object on which it should be performed, and necessary extra data.
  5. If that object is not yet replicated to client, then it will fail and error.

So, I have all that already preloaded on ReplicatedStorage, but that’s just models, because of this building system, I can’t preload script things, because player can place more than just 1 item of the same type, and I can only load items one-by one.

What’s the position of the part compared to the player? Assuming its just a streaming issue. Chances are if your part is created before the remote event is sent, its already replicated since if the client received the remote event signal, it should have also replicated the part… I really don’t know how remote events or replication works so I’m just going off what I can think of.

I have disabled streaming. The problem consists purely in “racing” of replicating and remote event.

Can you try parenting your part to a model? Fixed pretty much the same issue I was having with replication but client > server.

Wish I could help more, this is all I got :sob:

Islands are already models. Objects are models too.
Sadly, this problem is really complex, and I have spent myself over a 2 weeks trying to find solution, but sadly no result. =(

Probably because how instances and remote packets are sent are on a different order than each other.

You could just wait for 250ms before invoking the client, that’s plenty of time for the Instance to get replicated.

local part = Instance.new("Part", workspace)
task.delay(1 / 4, function()
    Remote:FireAllClients(part)
end)

Another fix would be to stop whatever you’re doing. (I mean it)
Try listening for when the model (or islands as you are referring to) gets added in the client via ChildAdded.

--Server
local Part = Instance.new("Part")
Part.Parent = workspace
local remoteFunction = game:GetService("ReplicatedStorage").Remote
if not remoteFunction:InvokeClient(Player, Part) then 
	repeat
		task.wait(2)
	until remoteFunction:InvokeClient(Player, Part)
end
--Client
game:GetService("ReplicatedStorage").Remote.OnClientInvoke:Connect(function(Part)
	if Part then
		Part.Color = Color3.fromRGB(255, 0, 0) 
		return true
	else
		return false
	end
end)

this code uses a remote function, but you can do the same thing with remote events