AI Spawning script not working

Pretty simple, spawn a clone inside a specific boundary called “boundary”. However, it’s not being copied as a proper rig. And the parts are being spawned separately, this is because it wouldn’t work spawning it as a whole model for some reason.



local workspace = game:GetService("Workspace")


local AI = workspace.R6
local boundary = workspace.Boundary 

if not AI or not boundary then
	print("Missing AI or boundary!")
	return
end

local function spawnAI()

	-- Create
	local clone = Instance.new("Model")


	local cloneParts = {}

	-- Clone each part
	for _, part in ipairs(AI:GetChildren()) do
		if part:IsA("BasePart") then
			local clonePart = part:Clone()
			clonePart.Parent = clone
			table.insert(cloneParts, clonePart)
		end
	end


	local xMin = boundary.Position.X
	local xMax = xMin + boundary.Size.X
	local zMin = boundary.Position.Z
	local zMax = zMin + boundary.Size.Z

	for _, part in ipairs(cloneParts) do
		local x = math.random(xMin, xMax)
		local z = math.random(zMin, zMax)
		part.Position = Vector3.new(x, boundary.Position.Y, z) 
	end


	clone.Parent = workspace

	print("Spawned AI")
end

spawnAI()

I’d really appreciate if you can help:

  • Make it spawn as a whole
  • Make it spawn as a proper rig.
1 Like

Not sure why you can’t get a model to clone as a whole.

Maybe selecting a PrimaryPart in the property window would help.

Here is the basic idea of cloning a model and putting it in the workspace:

local model = "YourModel" -- wherever your model is
local targetPart = "YourTargetPart" -- a location

local modelClone = model:Clone() -- clone the model
model.PrimaryPart.CFrame = targetPart.CFrame -- tell it where you want it
model.Parent = game.Workspace -- put it in the workspace
1 Like

Why can’t you just clone the AI?

Code:

local AI = workspace:FindFirstChild("R6")
local boundary = workspace:FindFirstChild("Boundary") 

if not AI or not boundary then
	print("Missing AI or boundary!")
	return
end

local function spawnAI()
	-- Create
	local clone = AI:Clone()

	local xMin = boundary.Position.X
	local xMax = xMin + boundary.Size.X
	local zMin = boundary.Position.Z
	local zMax = zMin + boundary.Size.Z

	local x = math.random(xMin, xMax)
	local z = math.random(zMin, zMax)

	clone:PivotTo(CFrame.new(x, boundary.Position.Y, z))
	clone.Parent = workspace

	print("Spawned AI")
end

spawnAI()
1 Like

Thank you so much! Not sure why I wasn’t able to just clone it before.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.