Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how do you get an Instance.new("Part") to spawn in multiple areas?

local function fire()
    		 local p = Instance.new("Part")
    		 p.Position = chosencolor:GetChildren().Position
  			 p.Size = Vector3.new( 2 , 1 , 2 )
   			 p.BrickColor = BrickColor.New(105)
   			 p.BottomSurface = 0
   			 p.TopSurface = 0
     		 p.Name = "Brick"
    		 p.Parent = game.Workspace
		end

Okay, so how would i fix the p.Position of the Instance.new("Part") because I get this: Workspace.Mainscript:41: bad argument #3 to 'Position' (Vector3 expected, got nil) I want a "p" to spawn on each children of "chosencolor"

2 answers

✓ Accepted answer

Ok so the GetChildren method returns a table so you would be better off using a generic loop to do something like this

for i, v in pairs(chosencolor:Getchildren()) do -- use of a generic for loop
	local p = Instance.new("Part")
        p.Position = v.Position -- note the change here
        p.Size = Vector3.new( 2 , 1 , 2 )
        p.BrickColor = BrickColor.New(105)
        p.BottomSurface = 0
        p.TopSurface = 0
        p.Name = "Brick"
        p.Parent = game.Workspace
end

so basically what we did here is we used a loop to get the children of chosencolor and took their positions and made a new part with that position for the number of children.

hope this helped

Your problem is you're trying to spawn the one part to the position of an array. The method GetChildren returns an array of the children of the object you cast it on. You need to create a part for each individual child of the array. To do this, you can use pairs in a for loop.

local function fire()

	local p = Instance.new("Part")
	p.Size = Vector3.new( 2 , 1 , 2 )
	p.BrickColor = BrickColor.New(105)
	p.BottomSurface = 0
	p.TopSurface = 0
	p.Name = "Brick"
	
	for i,v in pairs(chosencolor:GetChildren()) do
	local clone = p:Clone
	clone.Position = v.Position
	clone.Parent = workspace
	end

end

This discussion is preserved as it was. New answers, comments, and votes are closed.