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