Obscure but useful roblox luau features

There are some obscure features in roblox luau that can save lots of time, but they are barely known. Let’s change that.

Vector3

Do you ever find yourself always writing Vector3.new(0, 0, 0) or Vector3.new(5, 5, 5) And just wished there was an easier way to do this? Yes there is. Vector3.zero and Vector3.one exists.

local object: Part = workspace.Part
object.Size = Vector3.one * 50 -- Vector3.new(50, 50, 50) but shorter and more efficient.
object.Position = Vector3.zero -- Puts the part in Vector3.new(0, 0, 0)

Okay, I also wished there was an easier way to write Vector3’s like Vector3.new(20, 0, 20)
You are in luck. It also exists, Vector3.xAxis, Vector3.yAxis and Vector3.zAxis
Here is how I do it:

object.Position = (Vector3.xAxis + Vector3.zAxis) * 20
-- or
object.Position = (Vector3.xAxis + Vector3.yAxis) * 20
-- order dosent matter as these are vector3s
object.Position = (Vector3.yAxis + Vector3.xAxis) * 20

However, there are some cases where Vector3.new is cleaner, for example Vector3.new(50, 0, 20), these constants are not for ditching Vector3.new

Math

Lets talk about math.random. Ever find yourself tired from math.random(1, 5), math.random(1, 10) and so on?
You are in luck. math.random(n) picks a number from 1 to n

object.Position = Vector3.one * math.random(20)
-- another example
object.Position = (Vector3.xAxis + Vector3.zAxis) * math.random(20)

How clean! Without knowing this we would write Vector3.new(math.random(1, 20), math.random(1, 20), math.random(1, 20)). How ugly!

And that’s all I know for now. Hopefully this helped, even in the slightest.
Suggest more obscure features in the replies!

18 Likes

Wow these are actually really cool! But it does take up more space then simply writing Vector3.new(20,0,20)
Also make this into #resources:community-tutorials

5 Likes

Thats repetitive. We should avoid that. Even if that point was valid, We still have Vector3.new(math.random(1, 20), math.random(1, 20), math.random(1, 20))

This is not a tutorial

Feels more of a tutorial than resource. Your just telling us about an existing roblox feature, not making a thing entirely.

5 Likes

About the Vector3 part, It also works with Vector2, but without the zAxis thing

also you forgot to mention that if you only need X and/or Y, you can just put smth like this

local a = Vector3.new(1) -- just X
local b = Vector3.new(1, 2) -- just X and Y
local c = Vector3.new(1, 2, 3) -- all the values

also you inspired me to make an tutorial like this but for plugins

1 Like

I don’t agree that any of these arcane workarounds are at all useful or more efficient. For example, I don’t see that

Vector3.new(20, 0, 20)

is ‘ugly’, especially not compared to

(Vector3.xAxis + Vector3.zAxis) * 20

How is it in the wrong category

the OP mentioned that it isn’t a full replacement of Vector3.new

When did I say it is ugly?

Okayy so slight issue;

(Vector3.xAxis + Vector3.zAxis) * math.random(20)

this returns a Vector3 which X and Z values are always identical to each other
but in

Vector3.new(math.random(1, 20), 0, math.random(1, 20))

we get a Vector3 where X and Z can be different

so you cant really say your method is “cleaner” or “prettier” since it doesn’t do the same thing

1 Like

they’re both not the same

the first snippet is just

local r = math.random(1, 20)
Vector3.new(r, 0, r)

which uses the same value of r

while the second snippet uses different values like this

local r1 = math.random(1, 20)
local r2 = math.random(1, 20)
Vector3.new(r1, 0, r2)
1 Like

that’s exactly what I have said

my bad, I might have overfocused on the snippets lmao