Spawn random part will only spawn 1

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

  1. What do you want to achieve? PartSpawn Spawn more 5-15 but not only 1

  2. What is the issue? script

local Dino= script.PartTest1
local Part = script.Parent

local function spawnDino (PartMesh, spawnArea)
	local newPart = PartMesh:Clone()
	PartMesh.Parent = workspace
	
	local spacing = 6
	local randomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / 6, -spawnArea.Size.X / 6 ), 2, math.random(-spawnArea.Size.Z / 6, -spawnArea.Size.Z / 6 )) 
	
	PartMesh.Position = randomPosition
end

	for _,  spawnArea in workspace.SpawnPart:GetChildren() do
		for i =  1, 20, 1 do
			spawnDino(Dino, spawnArea)
		end
	end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes me look but the theme was similar but not mine was the case
2 Likes

It’s because you’re changing the “PartMesh” parent not the “NewPart”

3 Likes

Sorry for waiting Its work but have new problem he spawn only in 1 place And they should spawn in other places and have new problem { PartTest1 is not a valid member of Script “ServerScriptService.Script”}

The signs used in math.Random need to be different, as you have set them all as negative so it only has a single point to select from.

Try this:

local randomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / 6, spawnArea.Size.X / 6 ), 2, math.random(-spawnArea.Size.Z / 6, spawnArea.Size.Z / 6 )) 

Your second issue sounds like it is a naming issue, double check the partMesh is parented as expected and that references to it are spelt correctly.

I need fix this can you help me?

It’s strange how everything should work, maybe I incorrectly indicated?

Screenshot_2

local Dino = script.PartTest1

In this line you need to use:

for _,  spawnArea in pairs(workspace.SpawnPart:GetChildren()) do

And also i think you can’t have part in ServerScriptService
Instead move part to ReplicatedStorage and use this:

local Dino = game:GetService("ReplicatedStorage"):WaitForChild("PartTest1")

Your problem is that you create local newPart at the start of the spawnDino function, but later you refer to PartMesh, which moves original part.
Try using this:

local Dino= script.PartTest1
local Part = script.Parent

local function spawnDino (PartMesh, spawnArea)
	local newPart = PartMesh:Clone()
	newPart.Parent = workspace

	local spacing = 6
	local randomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / 6, -spawnArea.Size.X / 6 ), 2, math.random(-spawnArea.Size.Z / 6, -spawnArea.Size.Z / 6 )) 

	newPart.Position = randomPosition
end

for _,  spawnArea in workspace.SpawnPart:GetChildren() do
	for i =  1, 20, 1 do
		spawnDino(Dino, spawnArea)
	end
end
1 Like

correct code!

local Dino= script.PartTest1
local Part = script.Parent

local function spawnDino (PartMesh, spawnArea)
	local newPart = PartMesh:Clone()
	newPart.Parent = workspace

	local spacing = 6
	local randomPosition = spawnArea.Position + Vector3.new(math.random(-spawnArea.Size.X / 3, spawnArea.Size.X /3 ), 3, math.random(-spawnArea.Size.Z / 3, spawnArea.Size.Z / 3 )) 

	newPart.Position = randomPosition
end

for _,  spawnArea in workspace.spawnArea:GetChildren() do
	for i =  1, 10, 1 do
		spawnDino(Dino, spawnArea)
	end
end

thx for help @Wigglyaa @SnooopD0ge @SIMONOFAMERICA @mpc19801981

If you are reading this, then I’m sorry that I did not answer was in the cinema

1 Like

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