Problem with script work after cloning its parent from Replicated Storage

Hello. I tried to make a custom battle creation system (I have soldiers in RS and buttons to summon them). Every soldier has its behavior script (simple target finding via magnitude).
But when I tried to clone the soldier from RS in Workspace, his script didn’t work. Rig isn’t anchored.
I tried to transfer the soldier from RS to Workspace without cloning before running the game. Everything works correct. But I need it while the game run. Maybe there are other ways to transfer things from RS to Workspace, if it is impossible by cloning?

Soldier’s target finding.

local myTorso = script.Parent.Torso
local humanoid = script.Parent.Ally

local function FindTaget()
	local target
	local agro = 100000
	for i, v in pairs(game.Workspace:GetChildren()) do
		--print("In function loop")
		local hum = v:FindFirstChild("Enemy")
		local torso = v:FindFirstChild("Torso")
		if hum and torso then
			--print("Found hum and torso")
			if (myTorso.Position - torso.Position).magnitude < agro then
				target = torso
				agro = (myTorso.Position - torso.Position).magnitude
				--print("Values changed")
			end	
		end
	end
	return target
end

while wait(1) do
	--print("In loop")
	local target
	if script.Parent.Parent == "Workspace" then
		target = FindTaget()
	--print("Got targert")
	if target then
		--print("Moving to...")
		humanoid:MoveTo(target.Position)
	else
		--print("Else moving to...")
		humanoid:MoveTo(Vector3.new(myTorso.Position.X + math.random(-50, 50), 0, myTorso.Position.Z + math.random(-50, 50)))	
	end
end

Button’s cloning script.

local RS = game:GetService("ReplicatedStorage")
local Btn = script.Parent

Btn.MouseButton1Click:Connect(function()
	local unit = RS:FindFirstChild("Soldier")
	local copy = unit:Clone()
	copy.Parent = game.Workspace
end)


Thank you in advance

Hey, I also have had this problem before. Although I can’t really offer a solution fully I can tell it’s probably because you are in RS and not server storage. That or you might need to use a module script. It has to do with client to server issue. Look up the difference between local scripts and module scripts.

In the end a proper looking system should have a main module script in script storage and then all the npcs you call out with remote events just have scripts that call commands from the module.

1 Like

Okay, thanks. I’ll try this ideas later.

Supposedly you can’t do this. You’ll need to move the clone to workspace on the server. Therefore you should use Remote Events.