I need help with my custom datatype

Luau type hints and the Luau runtime are entirely separate. They’re hints, they don’t do anything other than inform developers what an object should be.

Here’s an example type:

type Point = {
    X: number,
    Y: number
}

This defines a table that should contain an X and Y property. However, its fundamental type is still a table. This is why when you run typeof on a table conforming to this interface, it still returns "table".

Here’s an example function:

function createPoint(X: number, Y: number): Point
    return {X = X, Y = Y}
end

This function returns a table. But you know that the table is guaranteed to conform to the Point interface. However, the runtime doesn’t know that. Only you know that.

What's the runtime?

The runtime is the thing that runs your code. Luau has to compile and then interpret your code in order for it to do anything. Otherwise, it’s just a meaningless text file.

Type information is (probably) lost at the compilation step since Luau doesn’t need type information to run your code.

2 Likes

That’s because typeof is roblox’s custom function meaning it’s only meant for roblox’s custom datatypes not yours. :face_with_raised_eyebrow:So make your own

1 Like

how is that possible?!

how can i do that?

Possibly by using the type() function from lua and making your own function for detecting if the table is your custom datatype. Also, why would you need custom datatypes.

Roblox currently doesn’t allows Developers (yet) to check the Custom Type of a Certain Object/Table. The only way you could do it is by creating a custom type function. I would recommend you creating a variable inside a certain Object’s metatable and name it __type, Then you can access it via getmetatable().

Here’s what it would look like:

-- Example:
local SomeTable = {}

-- Setting the SomeTable's metatable to this:
setmetatable(SomeTable, {
	__type = "SomeTable"
})

-- The custom type function:
local function GetType(Target)
	return getmetatable(Target).__type or typeof(Target)
end

-- Testing:
print(GetType(SomeTable)) -- Prints "SomeTable".
2 Likes

yes, thank you! but… this reply only solved 1 thing, the second thing is… how do i make it print Y,X,YD,XD And not

1 = Y
2 = X
3 = YD
4 = XD

Do NOT tell me to use table.concat(), because i also want the y and x and yd and xd able to access them.

What are you trying to do? Loop through the values and print them? Well, If it’s that, You can try doing:

for Index, Value in pairs(YourObject) do
	-- Should print out the order/index and then the Value.
	print(Index, "|", Value)
end

Make sure that you actually put the Values index as the Letters and not as a number.
Basically what i mean is this:

local SomeTable = {}
SomeTable.X = 1
SomeTable.Y = 2
SomeTable.XD = 3
SomeTable.YD = 4

output will say:

X = 1
Y = 2
XD = 3
YD = 4,

Now i want it to say 1,2,3,4

actually why i need that is not that just the output?

Then you can just print out only the value. Same thing goes for the Index/Position.

-- Only Index:
for Index, Value in pairs(YourObject) do
	print(Index)
end

-- Only Value:
for Index, Value in pairs(YourObject) do
	print(Value)
end
1 Like

why is thing giving me nil when i say “Y” Is it because metatable?




type MyType = {
	Y: number,
	X: number,
	YD: number,
	XD: number,
	

}


local MyType = {}

function MyType.new(Y,X,YD,XD): MyType
	local  new = {Y,X,YD,XD}
	setmetatable(new,{__type = "MyType"})
	
	
	return new
end

local function GetType(value: any):string
	return getmetatable(value).__type or typeof(value)
end




local d = MyType.new(1,3,4,5)
print(GetType(d))
print(d.Y) -- this prints nil
print(d)



You’re making an array.

It’s the same as:

local new = {
    [1] = Y,
    [2] = X,
    [3] = YD,
    [4] = XD
}

What you probably wanted was:

local new = {
    Y = Y,
    X = X,
    YD = YD,
    XD = XD
}
1 Like

i think this solves everything:


type MyType = {
	Y: number,
	X: number,
	YD: number,
	XD: number,


}


local MyType = {}

function MyType.new(Y,X,YD,XD): MyType
	local  new: MyType = {Y = Y, X = X, YD = Y,XD = XD}
	setmetatable(new,{__type = "MyType"})


	return new
end

local function GetType(value: any):string
	return getmetatable(value).__type or typeof(value)
end




local d = MyType.new(1,3,4,5)
print(GetType(d))
print(d.Y) 
print(d)

Ur still variable overshadowing name ur type somethin different than ur table