#how to reference a table using the colon syntax
120 messages · Page 1 of 1 (latest)
I could not find it either ig you can do smn like this (Not sure if this is good)
type tbl = {}
local testTable: tbl = {
-- Table ig?
};
yeah i mean i could just do :{} but i gotta insert inside it the stuff i want while i just want the argument to be a table, like, it can also be an empty table
like ```lua
type table = {any?}
still no, in the function, when i try getting something from that table i get a warn saying the item doesnt exist
also could u explain the "?"
ok
https://luau-lang.org/syntax#type-annotations
im currently reading this but i think im either dumb or theres nothing abt "free" tables
what do you mean "free" tables? empty tables?
yeah
like anything that is a table
no matter whats inside it
cuz i dont wanna type everything i need
type tableType = {any?}
local t : tableType = {121, 123, a = 2, "asd", c = 123} --these are just examples
local t2 : tableType = {}
``` this worked for me?
unless you are talking about something else
not sure, but heres the table type:
--defining
type table = {maxn: (t: table) -> number, find: (t: table, value: any, init: number?) -> number?, foreach: (t: table, f: (any, any) -> ()) -> (), foreachi: (t: table, f: (number, any) -> ()) -> (), getn: (t: table) -> number, concat: (t: table, sep: string?, i: number?, j: number?) -> string, insert: ((t: table, value: any) -> ()) & ((t: table, pos: number, value: any) -> ()), move: (a1: table, f: number, e: number, t: number, a2: table?) -> (), pack: (...any) -> {n: number, [number]: any}, remove: (t: table, pos: number?) -> any?, sort: (t: table, comp: ((any, any) -> boolean)?) -> (), unpack: (t: table, i: number?, j: number?) -> ...any, clear: (t: table) -> (), clone: (t: table) -> table, create: (count: number, value: any?) -> table, freeze: (t: table) -> table, isfrozen: (t: table) -> boolean}
u didnt write that right
ok nice
can I see your current table? not sure why it doesn't work
roblox doesnt consider "table" a type idk why
i had that issue before
but you can use
{any}
or, if you want to be more specific you can do:
type Something = {
[number]: any
}
i already tried using that, but when i try getting an item from given table it warns me that the items doenst exist in the table
before you specified that the type is any
and didnt specify any element
like
function something(param: {any})
print(param.item) --warns me
end
for instance, roblox does this automatic for you
local y: {any} --[[empty table, can contain value with any type]] = {
x = 3
}
-- roblox basically does this:
local y: {x: number} = { x = 3 }
are you using strict?
here the same error
you can fix it by doing
@subtle galleon
yeah ik that but isnt there a way to not having to reference all the keys i need in the table
whats stricy
and let luau do it for you
im not using it
are you on vscode?
no
it should not warn
wth
then just remove the type
weird...
just write it out
windows + shift + s
show me the function parameter
what
..
i would have not made this thread
if i were just gonna remove it
like i want to use it
but ig i will not
try pressing
ctrl + f and search for --!strict
thanks
ok i fucking did it it was really simple
type dictionary = {[any]: any}
Oh yeah forgot about that
You could also do ```lua
type Array<T> = {[number]: T}
local TestArray: Array<number> = {
1,2,3,4,56,6,7,8,67,745,6,34,53
};
table.foreach(TestArray, print)
If you want to be fancy >:)
nah im good
i have another question, if a paramater of a function has to be a model(or just any instance) and i want to get another instance from inside it, i get a warn saying the instance is not a key of that class, example:
function halflife(thing: Model)
thing.Humanoid.Health /= 2 --Key 'Humanoid' not found in class 'Model'
end
is there a way to fix this
I would also like to know
also fun fact when u reference what a function returns but it doesnt return anything in the function block it warns, even using the "?" operator after the type :
function check(x, y): boolean? --even using "?" it warns
if x == y then
return true
else
return false
end
end --theres a little warn here cuz nothing is actually returned inside the function block but only inside the if block
u can easily fix this by just adding an empty return at the end of the function:
function check(x, y): boolean?
if x == y then
return true
else
return false
end
return
end --doesnt warn anymore
can someone explain <> or send me a link to an explanation
it allows arguments to be sent thru
like
type hashtable <ARG> = {[ARG] : boolean}
ok i understand could u provide an example tho
