Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
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)

Ad
Log in to vote
0
Answered by 5 years ago
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.

0
LOL you used // instead of -- :D I make that mistake all the time cause I do C++  OBenjOne 190 — 5y
0
O lol i just noticed. spinywind 1 — 5y

Answer this question