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

How would i make spawned items area based?

Hey there, I'm new to scripting and would want to understand the following:

Script:

while true do
	wait(1)
local partadded = Instance.new("Part")
partadded.Name = "Block"
partadded.Parent = game.Workspace
	local x = math.random(-25, 25)
	local z = math.random(-25, 25)
	partadded.Position = Vector3.new(x,0,z)
end

currently its spawning at a random spot that I've defined but I'm wanting to know how i can make some sort of Spawn pad with an Offset position for each newly added Part.

Thanks!

5 comments

0

So what you wanna do is spawn blocks in a certain area?

0

Yes

0

Added an edit onto the answer.

0

You should try using reigon3 or zone+ or raycasting for a certain area

0

Here is a gif using zone+ similar to what you wanna do https://i.imgur.com/ZUS5xhQ.gif

2 answers

There could be a lot of different ways to do this, but here's a simple one. I nested 2 for loops inside a while loop. So what this will do is create a part at (1,0,1), (1,0,2), (1,0,3), (2,0,1), etc. Until (3,0,3) and repeat. This is fairly flexible and very easy to understand.

I'd recommend adding a check to see if the previous block still exists in workplace though.

while true do
	for xx = 1, 3, 1 do
		for zz = 1, 3, 1 do
			wait(1)
			local partadded = Instance.new("Part")
			partadded.Name = "Block"
			partadded.Parent = game.Workspace
			partadded.Position = Vector3.new(xx,0,zz)
		end
	end
end

EDIT: This should do roughly what you're looking for.

local positions = {
	{['name'] = 'Part1', ['pos'] =  Vector3.new(1,0,1)},
	{['name'] = 'Part2', ['pos'] =  Vector3.new(1,0,2)},
	{['name'] = 'Part3', ['pos'] =  Vector3.new(3,0,3)},
	{['name'] = 'Part4', ['pos'] =  Vector3.new(4,0,2)},
	{['name'] = 'Part5', ['pos'] =  Vector3.new(5,0,3)},
}

while wait(3) do
	local rand = math.random(#positions)
	if not workspace:FindFirstChild(positions[rand]['name']) then
		local partadded = Instance.new("Part")
		partadded.Name = positions[rand]['name']
		partadded.Parent = workspace
		partadded.Position = positions[rand]['pos']
	end
end

3 comments

0

That is pretty close but id want it to be more random and unpredictable?

0

Hmm. You could place all possible positions into a table, random() one, check position, and create if it doesnt exist in the workspace?

0

How would i go about doing a "Table" as i'm very new and not sure i know enough about that yet?, Lets say i have 5 preset spawn position and wanted to randomly place blocks out of them 5 that kinda thing would be ideal.

local SPAWNPAD_POS = workspace.PARTNAMEHERE.Position
while true do
	wait(1)
	local partadded = Instance.new("Part")
	partadded.Name = "Block"
	partadded.Parent = game.Workspace
	local x = math.random(SPAWNPAD.POS.X - 25,SPAWNPAD.POS.X + 25)
	local y = math.random(SPAWNPAD.POS.Y,SPAWNPAD.POS.Y + 25)
	local z = math.random(SPAWNPAD.POS.Z - 25,SPAWNPAD.POS.Z + 25)
	partadded.Position = Vector3.new(x,y,z)
end

3 comments

0

Replace the PARTNAMEHERE with the name of the part you want the parts to spawn around in the workspace

0

Worked well, But what would i do if i wanted it to spawn at say 5 different parts randomly?

0

The other guy's answer has the solution. Just accept someone's answer already man, you can research the other holes in your script yourself

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