Script doesn't work, but there are no errors

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    A script that creates Rigs of random colors and in random positions.
  2. What is the issue? Include screenshots / videos if possible!
    The Rigs are being deleted.
  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Changing from MoveTo to PivotTo, yes.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
for i=1,Random.new():NextInteger(1,10),1 do
	local t=game:GetService("ServerStorage").Folder:GetChildren()
	local r=t[Random.new():NextInteger(1,#t)]
	local c=r:Clone()
	local int=10000
	local n_int=-10000
	local r2=Random.new():NextInteger(n_int,int)
	c.Parent=game.Workspace
	c:PivotTo(CFrame.new(Vector3.new(r2,r2,r2),Vector3.new(0,0,0)))
	task.wait()
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Have you checked if the model you are trying to clone is properly anchored?

It is, all parts are anchored.

The following line is the culprit:

Vector3.new(r2,r2,r2)

What this does is that it doesn’t only set the x and z axis to a random value, but also the y(height) axis. The possible values r2 can have are huge, ranging from -10000 to 10000. The default height at which parts get destroyed automatically is -500 and you can set it through workspace.FallenPartsDestroyHeight, this means that if an NPC spawns below -500 (which is expected to happen to 47.5% of the NPCs you try to spawn, or in this case 5), it will be automatically destroyed without any errors popping up.

Apart from this, your code may also not run due to the location of the code segment you provided, or because it runs in a pcall that doesn’t print the errors. Also the rest of the 5 NPCs that spawn in normal locations might be hard to notice due to being very spread out, however you should be able to see them by looking at the explorer.

I set it to -50000, still getting deleted.

Also, really REALLY sorry if i sound rude, but im not tryna make kind of like an RPG, i am in a phase where im in LOVE with algorithms and the concept of randomness and im trying to make a fully randomized game. Edit: I get embarrassed when people get mad at me when im rude sometimes

Where does the code that you provided run? In a local script or a server script? Under which service? Is that script enabled? Also is that part of code on the main part of the script, or inside a function or event?

ServerScriptService, Server Script. And its the main part. No functions or events. Enabled, RunContext is not Legacy, but Server.

Are you sure they don’t spawn at all? Meaning that the instances aren’t visible in the explorer under workspace? Or you just don’t see them in-game?

You could also set up a script that listens to workspace.ChildAdded to check if the script tries to add them in workspace at all.

Something else that comes to mind is that another unrelated script may be interfering. Look for calls of Destroy or mentions of AddItem in other scripts. It could also be that another script unanchors the NPCs.

No other scripts, also, the Models are cloned to the Workspace, but the Parts inside vanish.

Maybe the NPCs themselves are configured in a wrong way? How did you insert them? Do they have the required instances, like the Humanoid instance, in them?

Theyre just models with no Humanoids at all, its not for an RPG or anything, like I said before. Theyre just structural, really.

Did you set the Archivable property in any of the parts or the model to false?

Nope. All set to true. (charchar)

--ServerScript in ServerScriptService
local storage = game:GetService("ServerStorage"):WaitForChild("Folder")
local minX, maxX = -500, 500
local minY, maxY = 0, 50
local minZ, maxZ = -500, 500

for i = 1, Random.new():NextInteger(1,10) do
	local t = storage:GetChildren()
	local r = t[Random.new():NextInteger(1,#t)]
	local c = r:Clone()
	c.Parent = workspace
	c:PivotTo(CFrame.new(Vector3.new(
		Random.new():NextInteger(minX,maxX),
		Random.new():NextInteger(minY,maxY),
		Random.new():NextInteger(minZ,maxZ)
	))) 	task.wait()
end

You were using the same random value (r2) for X, Y, and Z, which can place some Rigs extremely high, underground, or overlapping with other objects. Roblox physics can then collide them with terrain or other parts, causing unexpected behavior.. Hard for the same number to be right for X, Y and Z when placing them. Deletion if parts fall below the Workspace boundaries.

… I set the Fallen Part thing to -50000. oh also i updated my script some time ago. here

for i=1,Random.new():NextInteger(1,Random.new():NextInteger(1,960)),1 do
	local t=game:GetService("ServerStorage").Folder:GetChildren()
	local r=t[Random.new():NextInteger(1,#t)]
	local c=r:Clone()
	local int=100
	local n_int=-100
	if c.Name=="Noob" then
		c.Parent=game.Workspace
		c:PivotTo(CFrame.new(Vector3.new(Random.new():NextInteger(n_int,int),Random.new():NextInteger(n_int,int),Random.new():NextInteger(n_int,int)),Vector3.new(Random.new():NextInteger(-180,180),Random.new():NextInteger(-180,180),Random.new():NextInteger(-180,180))))
		task.wait()
	end
	if c.Name=="Hexahedron" then
		c.Parent=game.Workspace
		c.CFrame=CFrame.new(Vector3.new(Random.new():NextInteger(n_int,int),Random.new():NextInteger(n_int,int),Random.new():NextInteger(n_int,int)),Vector3.new(Random.new():NextInteger(-180,180),Random.new():NextInteger(-180,180),Random.new():NextInteger(-180,180)))
		c.Size=Vector3.new(Random.new():NextInteger(1,Random.new():NextInteger(1,40)),Random.new():NextInteger(1,Random.new():NextInteger(1,40)),Random.new():NextInteger(1,Random.new():NextInteger(1,40)))
		c.BrickColor=BrickColor.random()
	end
	if c.Name=="Sphere" then
		c.Parent=game.Workspace
		c.CFrame=CFrame.new(Vector3.new(Random.new():NextInteger(n_int,int),Random.new():NextInteger(n_int,int),Random.new():NextInteger(n_int,int)),Vector3.new(Random.new():NextInteger(-180,180),Random.new():NextInteger(-180,180),Random.new():NextInteger(-180,180)))
		local size=Random.new():NextInteger(1,Random.new():NextInteger(1,40))
		c.Size=Vector3.new(size,size,size)
		c.BrickColor=BrickColor.random()
	end
end

Can you please try (temporarily) getting rid of the random components (such as position), and just replace the values with constants (e.g. 0). This is just to help narrow down where the issue could be.

(Also, I take it that the code is in a Server script?)

Sure! I’ve decided to replace the randoms with 1 instead.

for i=1,5,1 do
	local t=game:GetService("ServerStorage").Folder:GetChildren()
	local r=t[Random.new():NextInteger(1,#t)]
	local c=r:Clone()
	local int=100
	local n_int=-100
	if c.Name=="Noob" then
		c.Parent=game.Workspace
		c:PivotTo(CFrame.new(Vector3.new(1,1,1),Vector3.new(1,1,1)))
		task.wait()
	end
	if c.Name=="Hexahedron" then
		c.Parent=game.Workspace
		c.CFrame=CFrame.new(Vector3.new(1,1,1),Vector3.new(1,1,1))
		c.Size=Vector3.new(1,1,1)
		c.BrickColor=BrickColor.random()
	end
	if c.Name=="Sphere" then
		c.Parent=game.Workspace
		c.CFrame=CFrame.new(Vector3.new(1,1,1),Vector3.new(1,1,1))
		local size=Vector3.new(1,1,1)
		c.Size=Vector3.new(size,size,size)
		c.BrickColor=BrickColor.random()
	end
end

And the parts still are not creating?

And just to clarify - do they briefly exist, or just not exist at all

It seems that some, sometimes are created, but the majority arent created, with just a lone Model instance.