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)
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.
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.
I have a really complex internal building systems. In short, it consists of the following:
Players join server
Their own islands load - players build them themselves.
While they load, objects load too. If they have some required to work functions, will be initialized and executed.
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.
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.
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