Cloned object from ReplicatedStorage to Workspace appears only in the server and not the client

Ey! :coefficients:

As of recent, I’ve been independently developing a puzzle-platformer game that utilizes the physics engine to create puzzles and contraptions. The game mechanics utilizes the very well-known btools to solve puzzles, alongside objects (i.e. Boxes), constraints, and platforms to complete puzzles. Now there is a lot more to this, but I don’t want to expose too much. Anyways, I’m trying to fix a “Respawn” system I created. Let me explain;

In the puzzles, if you accidentally mess up during a puzzle that utilizes a box for example to continue going forward. You can click on a part button (not a GUI interface), and it will teleport/“respawning”, right next to the button. I also have it set up that if the box doesn’t exist, say that a player accidentally deletes it. Not to worry because it will make a new copy of that object.


Before we get into issues and hopefully you understand everything, you should learn how this works inside Roblox Studio, and it is pretty simple; there are two scripts located in ServerScriptStorage named OnClickIFWorkspace and OnClickIFnil.

image

  • OnClickIFWorkspace responsibility is to find the said object in Workspace; if it finds the object, it will teleport it based on the coordinates provided in the script.

  • OnClickIFnil responsibility is to see if the object doesn’t exist, which if it doesn’t will create a new copy of the said object from ReplicatedStorage and put the clone in Workspace based on the coordinates provided in the script.

image

These two scripts are running at the same time, and are fired when the ClickDetector function is fired from clicking on RespawnBox1 which causes Box2 to teleport from the said coordinates in Workspace.

image

Here I will provide the code for both scripts:

OnClickIFWorkspace:

local FindBox = game.Workspace:FindFirstChild("Box2")
local Box = game.Workspace.Box2

function onClicked()
	if FindBox then
		Box.Position = Vector3.new(0, 4, 0)
		Box.Orientation = Vector3.new(0,0,0)
		if Box.Position == Vector3.new(0, 4, 0) and Box.Orientation == Vector3.new(0,0,0) then
			print("Objective Successful!")
		end
	end
end

OnClickIFnil:

local FindBox = game.Workspace:FindFirstChild("Box2")
local Box = game.Workspace.Box2
local StoredBox = game.ReplicatedStorage.Box2

function onClicked()
	if FindBox == nil then
		local ClonedBox = StoredBox:Clone()
		print("Was not able to detect Box in Workspace! Cloning from ReplicatedStorage...")
		ClonedBox.Parent = game.Workspace
		if ClonedBox.Parent == game.Workspace then
			Box.Position = Vector3.new(0, 4, 0) 
			Box.Orientation = Vector3.new(0,0,0)
			if Box.Position == Vector3.new(0, 4, 0) and Box.Orientation == Vector3.new(0,0,0) then
				print("Cloning Successful!")
			end
		end
	end
end

game.Workspace.RespawnBox1.ClickDetector.MouseClick:connect(onClicked)

This is where we get into issues now. Remember when I said that if the box doesn’t exist, it will make a new copy? For some reason, it doesn’t create a new copy in the client, but when we look at the server, it cloned.

Client:

Server:

Because the script still cloned the object into Workspace, there really shouldn’t be anything wrong as far as I’m aware. It could be something on Roblox’s end, but that can be argued by this error message:


I still do not understand a lot of the problem, and the fact that I am a beginner programmer makes it somewhat more of a challenge but fun. I tried to resolve my issue by searching on Google and searching the Developer Forum. It did not help.

You are setting the position & orientation of Box an not ClonedBox:

		ClonedBox.Parent = game.Workspace
		if ClonedBox.Parent == game.Workspace then
			ClonedBox.Position = Vector3.new(0, 4, 0) 
			ClonedBox.Orientation = Vector3.new(0,0,0)
			if ClonedBox.Position == Vector3.new(0, 4, 0) and ClonedBox.Orientation == Vector3.new(0,0,0) then
				print("Cloning Successful!")
			end
		end

One minor point is that it is good practice and possibly better performance wise to set all the parameters of a cloned part before parenting to the workspace.

3 Likes

I will admit this is good practice to learn from. However, it changes nothing about my issue because the two variables reference the same object, which includes the clone variable.

I’m unsure as to where the InsertService is applied in this code, but from the looks of things, it’s supposed to work as expected as you are not doing anything related to using a LocalScript. What I’d suggest is not to have FindBox outside of your function as this will be referencing the box before the click or the nonexisting box. The other thing would also be to use what @BadDad2004 suggested as this is the better way to work around with Cloned instances.

1 Like