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

How do calls such as math.random(min, max) work without passing itself as first argument?

From my knowledge,

obj:method(...)

is syntax sugar (a nicer way to write something else) for this code:

obj.method(obj, ...)

So then how do function calls such as math.random(min, max) not fail even if math isn't passed as first argument?

And when calling random as a method it fails with the error:

wrong number of arguments

If someone can explain to me how it works it would be appreciated.

1 answer

✓ Accepted answer

##Question

So then how do function calls such as math.random(min, max) not fail even if math isn't passed as first argument?

##Whats happening. Because when you call the method, its passing self as the first argument to the random function in the math class. So you're essentially trying to call math.random(math, min) when you do math:random(min, max)

###Example

local a = {}

function a.b(x, y)
    print(x, y)
end

a.b(1, 2)
a:b(1, 2)

This will output 1 2 table: 0x199f490 1

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