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

How to set spawn locations in random spots within an area?

This is something new to me. This is the first time I really needed to do anything somewhat complicated with positioning.

What I'm trying to do:

I don't know how to approach this at all, but basically I'm trying to have something spawn at random locations within a specific area that I set.

What I've done so far is created a part to decide the area where I want things to spawn, but I'm unsure of how to make items spawn in random areas within that part; and I don't even know if I should or even can do it that way.

Any help would be appreciated; I know if I can get a good start I can figure it out.

Edit:

im specifically avoiding creating a bunch of bricks that'll spawn the item that i want in order to avoid lag in my game. im just having one script handle the spawning

2 answers

✓ Accepted answer

first you actually need to get the range of where the parts should spawn.. mainly the X and Z ranges.. then you can use math.random() to get random positions of where a part should spawn

you can get the position ranges by getting the size of the part that represents where other parts should spawn, and dividing the value of the corresponding axis by 2, and add the result(quotient) to the corresponding axis of the position of the part..

for instance:

local positionTemplate  = --the part that represents where the parts should spawn
local ZMax = positionTemplate.Position.Z + ((positionTemplate.Size.Z) / 2)
local ZMin = positionTemplate.Position.Z - ((positionTemplate.Size.Z) / 2)

local XMax = positionTemplate.Position.X + ((positionTemplate.Size.X) / 2)
local XMin = positionTemplate.Position.X - ((positionTemplate.Size.X) / 2)

local YPos = positionTemplate.Position.Y

for i = 0, 10, 1 do
	local x = math.random(XMin, XMax)
	local z  = math.random(ZMin, ZMax)

	local part = Instance.new("Part", workspace);
	part.Position = Vector.new(x, YPos, z)
end

this should hopefully work

1 comment

0

Worked. Thanks for the help and explanation!

Isn't a request site, however I'll try to give you some help.

Here's what I would do.

local tool = game:GetService("ServerStorage"):WaitForChild("Tool") --Tool we want to spawn
local spawnPart = script.Parent --The script is inside the part (that spawns tools)

while true do
	wait(5)
	local newTool = tool:Clone() --Clone tool
	newTool.Handle.CFrame = spawnPart.CFrame * CFrame.new(math.random(-spawnPart.Size.X/2, spawnPart.Size.X/2), 0, math.random(-spawnPart.Size.Z/2, spawnPart.Size.Z/2)
	newTool.Parent = workspace
end

It's hard to explain, but basically, we take the CFrame of the spawnPart and multiply it by a new CFrame which moves the handle of the cloned tool somewhere random inside the spawn part. When we take the CFrame, it is the middle of the part. So, we have to move the tool from the center of the part. Since the CFrame is in the middle, it means that to the right there's only HALF of the size of the part (and same with the left). That's why we divide by 2.

Sorry if the explanation isn't too good, but that's the best I could do ;-;

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