Server stops replicating to client after moving part into and out of ReplicatedStorage with the PlayerAdded event

Background (You can skip)

In a simulator I am creating, each player will have a range to fire their cannon down. When they leave, I’m planning on moving the range into ReplicatedStorage to despawn it. As developing with everything already in ReplicatedStorage is a pain, I am leaving everything outside ReplicatedStorage to immediately be moved into ReplicatedStorage once the server loads. Then, once the player joins, one of the ranges will be moved back into the workspace. All of this happens within tenths of a second.

Problem

When I move a part into ReplicatedStorage, then out of Replicated storage using the PlayerAdded event, changes the server makes to the part no longer replicate to the client.

Here is the code, I added print commands to make sure everything runs in the proper order:

game.Players.PlayerAdded:Connect(function (player)
	print("2")
	game.ReplicatedStorage.Part.Parent = workspace
end)

print("1")
workspace.Part.Parent=game.ReplicatedStorage

wait(5)

workspace.Part.Transparency = 0
print("Changed transparency")

(Sorry, Imgur wrecked the quality)

Here are a few things I have tried (and failed :sweat_smile:) to gain an understanding:

  • I have verified that on the server the transparency is 0
  • Adding wait() before print("2") resolved the issue, and the transparency of the part changes to 1 on the client as it should. I still want to know what is going on, as I don’t know if this is a reliable fix.
  • It doesn’t matter if I put workspace.Part.Parent=game.ReplicatedStorage before or after I connect to the PlayerAdded event.
  • game.ReplicatedStorage.Part.Parent = workspace must be inside the PlayerAdded event in order for the bug to work. I tried changing it to a touch event, then touching the part after a few seconds, and the bug did not occur. If I immediately put the part into ReplicatedStorage and take it out, without having the PlayerAdded event the bug will not happen.
  • Having workspace.Part.Transparency = 0 is not necessary for the bug to happen. After moving the part into and out of replicated storage, most changes on the server do not replicate to the client.
  • If the part is anchored, then movement and position changes made on the server do not show up on the client. Appearance changes such as transparency, material, and color do still show up regardless of if the part is anchored or not.
  • I tested with the script inside Workspace and ServerScriptService and the result was the same.

What on Earth is going on?!?!
Thanks for any help!

3 Likes

Try moving it and storing it in ServerStorage instead.

1 Like

Once I get home I will try that!

What is the difference between ReplicatedStorage and ServerStorage?

1 Like

ReplicatedStorage and all of its descendants are cloned/seen by the client. ServerStorage and all its descendants inside it is securely only seen/recognized by server-scripts.

1 Like

This fixed it! I’m not going to mark it as the solution because I still want to know what was causing the bug in my original post. I’m also going to utilize ServerStorage more so I can have more security.

Thank you!

1 Like

ReplicatedStorage is for assets you need both the server and client to have access to. As your script is on the server, moving the part it into workspace will cause it to be replicated to the client (although the client can’t access it whilst it is in ServerStorage). For the purposes of your scenario, the client doesn’t need to have access to the part whilst not in workspace. Hopefully this clears it up for you, let me know if you need further clarification!

1 Like

I definitely understand that part, but what is confusing is in my scenario the part was moved out of ReplicatedStorage, then the part would stop updating on the client after a change on the server, even though it is in the workspace. I think this is more of a bug, so I might post it in the bugs forum.

1 Like

Have you tried setting it as a value? Also you parented it to ReplicatedStorage after parenting it to workspace.

image

Which causes it to fail to find or recognize the part. Try setting the part as a variable.

local Part= game.ReplicatedStorage.Part

game.Players.PlayerAdded:Connect(function(player)
	print("2")
	Part.Parent = workspace
end)

print("1")
Part.Parent=game.ReplicatedStorage

wait(5)

Part.Transparency = 0
print("Changed transparency")
1 Like

The part starts in workspace, so I edited it to the following:

part = workspace.Part

part.Parent = game.ReplicatedStorage

game.Players.PlayerAdded:Connect(function ()
	part.Parent = workspace
end)

wait(5)
part.Transparency = 0.5

It still didn’t work.

If I had to make a guess, I think when I immediately move the part to ReplicatedStorage, it takes time to replicate to the clients. I think I then move it back to workspace with the PlayerAdded event before it has completed replicating to the client, causing this bug. I think this is true because if I add wait() before moving the object in the PlayerAdded event, everything work fine. I think the best solution was to use SeverStorage.

Thank you for your help!

1 Like