I would like to ‘undo’ the automatic replication of a part from the server to the client by turning the part invisible on the client.
Code below:
-- Server script
local undoReplication = game.ReplicatedStorage:WaitForChild("undoReplication")
game.Players.PlayerAdded:Connect(function(player)
local part = game.ServerStorage:WaitForChild("Part")
-- The part will be replicated to the client.
part:Clone().Parent = workspace
-- Make the part transparent on the client (effectively 'removing' it).
undoReplication:FireClient(player, workspace.Part)
end)
-- Local script
local undoReplication = game.ReplicatedStorage:WaitForChild("undoReplication")
undoReplication.OnClientEvent:Connect(function(part)
part.Transparency = 1 -- part is nil since it has not replicated yet...
print("undone")
end)
Could the part in the local script be nil if the part has not yet been replicated from the server to the client ?
EDIT: I’ve seen mentions in other posts about a custom replication system, but these parts are going to be moving a lot, and I don’t have the network capacity to send a remote updating their positions every frame so this is not viable for me.
local undoReplication = game.ReplicatedStorage:WaitForChild("undoReplication")
undoReplication.OnClientEvent:Connect(function(part)
if part then
part.Transparency = 1
else
repeat wait() until part ~= nil
part.Transparency = 1
end
end)
Try this:
Assuming you know the part should not be shown on the client, create a Model in Workspace where the “nonreplicated” parts will go into.
When the client joins, parent that Model into ReplicatedStorage or just :Destroy() it locally. This way, the part will exist on the server but will be invisible on the client!
Comes with the added benefit of not having to fire a remote for every part added
@Epic_Player16 There was a mistake in my code above, I needed to reference the part through the datamodel when passing it through the remote event ( I have edited the question above). Now the part is no longer nil in the local script and so appears to work.
I have a further question though, is it necessary to use the repeat until? I’m thinking that the part might always be replicated first before remote event is run?
I guess it’s best to be on the safe side so I’ll include it anyway. It’s probably worth adding a timeout though in case the part doesn’t replicate for whatever reason.
It seems a shame that there’s no solution using something like WaitForChild rather than this sort of polling approach. I guess if it’s functional it doesn’t matter that much.