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

How To Make A Part Spawn In A Random Location Within A Certain Distance?

The question is kind of self explanatory, how would I do that? Is there a special version of math.random that can do that? Or is there a function that automatically does that for you? I am new to scripting, and don't know all the functions out there, so that sounds like it would make sense. How would I do approach that?

2 answers

local part = Instance.new("Part")
part.Parent = workspace
part.Position = Vector3.new( math.random(POS1, POS2), math.random(POS1, POS2), math.random(POS1, POS2) )

Define a position 1 and position 2 for each axis, math.random(number, number) finds a random number between the 2 numbers, so putting it into the position will get a random position.

EDIT: (sorry, I edited the post and forgot to put the code back in a codeblock)

local minYPos = 0
local minXPos = 0
local minZPos = 0

local maxYPos = 0
local maxXPos = 0
local maxZPos = 0

local function GeneratePosition()
	local xPos = math.random(minXPos, maxXPos)
	local yPos = math.random(minYPos, maxYPos)
	local zPos = math.random(minZPos, maxZPos)
	
	return Vector3.new(xPos, YPos, ZPos)
end

local Part = game.Workspace.Part
Part.Position = GeneratePosition()

//Sorry for making it a bit long, I'd figure id make it clear as possible to make it easier to understand.

2 comments

0

LOL you used // instead of -- :D I make that mistake all the time cause I do C++ 

0

O lol i just noticed.

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