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

Could anyone explain to me if there is a Random Function/Method ?

Asked by 10 years ago

Well I've finally got to a point in making my game and I need too know if there is a random Function/Method ? I don't recall learning it when learning lua. So in my game (One Piece) I want to make it so some parts (Devil Fruits) spawn randomly over the terrain.

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

Well, for your purposes math.random() should be fine.

math.random() takes two arguments, a starting number and an ending number, then proceeds get a random integer between those two numbers. If not given any arguments, it will return a random number between 0 and 1.

print(math.random(1,10))

Using this, you can proceed to get random x and z coordinates, then create you part in the location. For example,

while true do
wait(3)
local p = Instance.new("Part")
p.Size = Vector3.new(1,1,1)
p.Parent = workspace

local randomX = math.random(1,10)
local randomZ = math.random(1,10)
p.Position = Vector3.new(randomX, 0, randomZ)
end

What this does is position a part randomly in a square 10 x 10 studs big. Of course, all you do to make the square bigger is to make the arguments in math.random()different.

Ad

Answer this question